944,184 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 26316
  • C++ RSS
Jul 3rd, 2007
0

Two-dimensional char array

Expand Post »
What is a two-dimensional char array with 3 rows and 3 columns. Can give me an example of a program. Thanks
Similar Threads
Reputation Points: 28
Solved Threads: 0
Newbie Poster
pixrix is offline Offline
18 posts
since Jun 2007
Jul 3rd, 2007
0

Re: Two-dimensional char array

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

C++ Syntax (Toggle Plain Text)
  1. char array[3][3];
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Jul 3rd, 2007
0

Re: Two-dimensional char array

I also know how to wirte it.. but wat is it use for.. can at least show me some example..
Reputation Points: 28
Solved Threads: 0
Newbie Poster
pixrix is offline Offline
18 posts
since Jun 2007
Jul 3rd, 2007
0

Re: Two-dimensional char array

A real world use of two dimensional arrays is for representing matrices. Here is an example of 3 x 3 identity matrix:
C++ Syntax (Toggle Plain Text)
  1. int matrix[3][3] = {0};
  2. for(int i = 0; i < 3; ++i)
  3. {
  4. for(int j = 0; j < 3; ++j)
  5. {
  6. if(i == j)
  7. matrix[i][i] = 1;
  8. }
  9. }
  10.  
  11. //For printing this matrix
  12. for(int i = 0; i < 3; ++i)
  13. {
  14. for(int j = 0; j < 3; ++j)
  15. {
  16. cout << " " << matrix[i][j]) << " ";
  17. }
  18. putchar('\n');
  19. }

The output after printing this matrix would be:
C++ Syntax (Toggle Plain Text)
  1. 1 0 0
  2. 0 1 0
  3. 0 0 1
Last edited by ~s.o.s~; Jul 3rd, 2007 at 1:33 pm.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Jul 3rd, 2007
0

Re: Two-dimensional char array

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)
  1. char names[3][21];
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Jul 5th, 2007
0

Re: Two-dimensional char array

Thanks guys
Reputation Points: 28
Solved Threads: 0
Newbie Poster
pixrix is offline Offline
18 posts
since Jun 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Insertion Sort
Next Thread in C++ Forum Timeline: Bug in the function





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC