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

Recommended Answers

All 5 Replies

I won't write the program for you but here is how to declare the array.

char array[3][3];

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

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

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

Thanks guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.