954,487 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Two-dimensional char array

What is a two-dimensional char array with 3 rows and 3 columns. Can give me an example of a program. Thanks

pixrix
Newbie Poster
18 posts since Jun 2007
Reputation Points: 28
Solved Threads: 0
 

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
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I also know how to wirte it.. but wat is it use for.. can at least show me some example..

pixrix
Newbie Poster
18 posts since Jun 2007
Reputation Points: 28
Solved Threads: 0
 

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
Administrator
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
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Thanks guys

pixrix
Newbie Poster
18 posts since Jun 2007
Reputation Points: 28
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You