Working with C++ Multi Dimensional Arrays
Rate this article
5.00 out of 5
It is a common observation that multi - dimensional array manipulation is always going to be difficult for any beginner in C++.  This article will guide you on some of the multi dimensional array aspects like initializing a one and two dimensional array, how to allocate memory in the heap for a 1 and two dimensional array using the new operator and how to free the dynamically allocated memory using the delete operator etc. 

The youtube video tutorial and the accompanying source code will be guide you on how to play around with C++ Multidimensional arrays. Watch the video below and play around with the source code. Please post your questions if any in the feedback section of this article.




Also see the main source code snippet below from the VC++ 2005 solution workspace..

#include <stdio.h>
#include <string.h>

int main(int argc, char* argv[])
{
    printf("Muti Dimensional C++ Array Example... ");

    //Initializing a static one dimensional integer array..
    int StaticOneDimensionalArray[5]={1,2,3,4,5};

    printf("StaticOneDimensionalArray Results.. ");
   
    for(int i=0;i<5;i++)
    {
        printf("%d",StaticOneDimensionalArray[i]);
    }

    printf(" ");


    //Initializing a static two dimensional array..

    int StaticTwoDimensionalArray[2][5]={{1,2,3,4,5},{6,7,8,9,10}};

    //Another type of initialization..

    int StaticTwoDimensionalArrayEx[2][5]={1,2,3,4,5,6,7,8,9,10};

    printf("StaticTwoDimensionalArray Results.. ");

    for(int i=0;i<2;i++)
    {
        for(int j=0;j<5;j++)
        {
            printf("%d",StaticTwoDimensionalArray[i][j]);
        }
    }

    printf(" ");

    printf("StaticTwoDimensionalArrayEx Results.. ");

    for(int i=0;i<2;i++)
    {
        for(int j=0;j<5;j++)
        {
            printf("%d",StaticTwoDimensionalArrayEx[i][j]);
        }
    }
    printf(" ");


    //Initializing a dynamic 1 dimensional array..
    //Allocate memory dynamically

    int iSize=5;
    int* DynamicOneDimensionalArray= new int[iSize];

    //Initialize values
    for(int i=0;i<iSize;i++)
    {
        DynamicOneDimensionalArray[i]=i*2; //Multiply index by two as the initialized value
    }

    printf("DynamicOneDimensionalArray Results.. ");
   
    for(int i=0;i<iSize;i++)
    {
        printf("%d",DynamicOneDimensionalArray[i]);
    }

    printf(" ");


    //Initializing a dynamic 2 dimensional array..
    //Allocate memory dynamically
    int iNoOfRows=2;
    int iNoOfCols=5;
    int** DynamicTwoDimensionalArray= new int*[iNoOfRows]; //Create two rows

    //Now create 5 columns for each rows
    for(int i=0;i<iNoOfRows;i++)
    {
        DynamicTwoDimensionalArray[i]= new int[iNoOfCols];
    }

    for(int i=0;i<iNoOfRows;i++)
    {
        for(int j=0;j<iNoOfCols;j++)
        {
            DynamicTwoDimensionalArray[i][j]=i*j; //Initialize the values as i* j
        }
    }


    printf("DynamicTwoDimensionalArray Results.. ");

    for(int i=0;i<iNoOfRows;i++)
    {
        for(int j=0;j<iNoOfCols;j++)
        {
            printf("%d",DynamicTwoDimensionalArray[i][j]);
        }
    }

    printf(" ");


    //Delete the dynamic one dimesional array memory (memory allocated in the heap)

    delete [] DynamicOneDimensionalArray;
    DynamicOneDimensionalArray=NULL; //good Habbit to assign NULL pointer after use
    //delete the dynamic two dimensional array memory

    //Delete first the column memory
    for(int i=0;i<iNoOfRows;i++)
    {
        delete [] DynamicTwoDimensionalArray[i];
    }
    //Now delete the rows..
    delete [] DynamicTwoDimensionalArray;

    DynamicTwoDimensionalArray=NULL; //good Habbit to assign NULL pointer after use


    //Similarly for other types and user defined data types..
    //for example take the case of a user defined data type..

    class Person
    {
    public:

        char Name[20];
        int iAge;
        Person()
        {
            iAge=10;
        }
    };

    Person PersonStaticArray[10];
    Person* PersonDynamicArray= new Person[10];
   
    strcpy(PersonDynamicArray[0].Name,"Justin Thomas");
    PersonDynamicArray[0].iAge=30;
   
    printf("PersonDynamicArray First Index vales.. ");
    printf("First Persons Name and Age is:-> %s, %d ",PersonDynamicArray[0].Name,PersonDynamicArray[0].iAge);
    //Etc etc..
   
    //visit www.tektips.in for more details..


    return 0;

}



Download Full Source Code

About The Author
Tom Thomas
Mr. Tom is the Co-Founder and CTO of  KTS InfoTech.  He started his career as a C/C++ programmer in the late 90's and has been very active and passionate in programming since then. He had worked both in start-ups ( Infortech software Pvt Ltd) as well as in CMM Level 5 companies (NeST and Wipro Technologies) for clients like General Electric, Agilent, Hitachi, Toshiba, Fujitsu, Alcatel, Insurance Service Corporation etc. His experience as an Engineer, Architect, Project Manager, Chief Technical Officer and as a Teacher makes him ideal for any type of jobs related to Information Technology.

His role with his present employer includes exploring new business opportunities and partnerships, Developing software product frameworks, developing and executing marketing strategies for company products etc.  

He holds Masters degree in Physics and Computer Science form CUSAT , one of the premier Science and Technology Institutions in India .

His major interests are in Optical Networking, Robotics, Device drivers, Database, Graphics, web applications, Software product Engineering, Open source Technologies and Sports physics.

He resides in Kerala ,India, with his mother, wife and his little angel.

He can be reached on Skype at this ID thomas_tom99 or at his E Mail address tom.thomas (at)ktsinfotech.com

         
Number Of articles Posted
9
Number Of Messages Posted
207
Joined Date
12/08/2011
   
Author
Messages
Posted:
Post Your Comments
Name (Max 50 Chars)
Comments

 
TekTipsDownload
GateExam
Academic Projects
TekTipsExperts
Android