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;
}