I won't write the program for you but here is how to declare the array.
char array[3][3];
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
A real world use of two dimensional arrays is for representing matrices. Here is an example of 3 x 3 identity matrix:
int matrix[3][3] = {0};
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 3; ++j)
{
if(i == j)
matrix[i][i] = 1;
}
}
//For printing this matrix
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 3; ++j)
{
cout << " " << matrix[i][j]) << " ";
}
putchar('\n');
}
The output after printing this matrix would be:
1 0 0
0 1 0
0 0 1
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
2 dimensional character arrays can be used to store any list of text items, such as a list of people's names, a list of addresses, telephone numbers, email addresses, etc. If you want a list of 3 names and each name can be a maximum of 20 characters then you would declare the array like below -- since c style strings must be terminated with a NULL character -- '\0' -- you must add one extra byte to the maximum width you want, in this example declare the width as 21 characters.
char names[3][21];
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343