How To Create One Two And Multi Dimensional Array In C++
Rate this article
0 out of 5
In this tutorial we will show you how to create a one two and multi dimensional array in c++. After viewing the video tutorial, download the source code and try to modify the code so as to get a feel of what is learned in this video tutorial.


Program

#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 teh 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;

}


Source Code

About The Author
KTS Training

KTS InfoTech Training division offers various short-term programming courses for Students and Professionals who are looking for a bright career in the field of Software development.

The programming video tutorials presented in this web site are taken from some of the training sessions conducted by KTS InfoTech.

You can also learn the Programming packages through Internet directly form our English Speaking Technical experts at an affordable rate.

For more details click here and submit your training requirements.



Number Of articles Posted
60
Number Of Messages Posted
3
Joined Date
14/11/2012
   
Author
Messages
Posted:
Post Your Comments
Name (Max 50 Chars)
Comments

 
   
TekTipsDownload
GateExam
Academic Projects
TekTipsExperts
Android