| | |
Two-dimensional char array
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
I won't write the program for you but here is how to declare the array.
C++ Syntax (Toggle Plain Text)
char array[3][3];
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
A real world use of two dimensional arrays is for representing matrices. Here is an example of 3 x 3 identity matrix:
The output after printing this matrix would be:
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
1 0 0 0 1 0 0 0 1
Last edited by ~s.o.s~; Jul 3rd, 2007 at 1:33 pm.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
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.
C++ Syntax (Toggle Plain Text)
char names[3][21];
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- One line of code copy char array using pointers (C++)
- Simple char array question (C)
- ArrayList to multi-dimensional string array (C#)
- How to assign my own numeric values to a char array? (C++)
- adding data into an char array (C++)
- two-dimensional char array (Java)
Other Threads in the C++ Forum
- Previous Thread: Insertion Sort
- Next Thread: Bug in the function
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






