944,161 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 11078
  • C++ RSS
Oct 12th, 2007
0

C++ Multi dimensional arrays

Expand Post »
I am very new to C++ and one of my homework assignments involvemulti dimensional arrays.

I was able to initialize and input and display the array (4x4) but the last step is to swap the data of the row to that of the column. Tried different ways and unable to figure out the code. Can you help?

CPP Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<iomanip>
  3. using namespace std;
  4. int main()
  5. {
  6. const int NO_OF_ROWS = 4;
  7. const int NO_OF_COLUMNS =4;
  8. int matrix[NO_OF_ROWS][NO_OF_COLUMNS];
  9. int copyArray(int row,int col,int arraysize);
  10.  
  11. int row;
  12. int col;
  13.  
  14. //initialize
  15.  
  16. for (col = 0; col < NO_OF_COLUMNS; col++)
  17. matrix[NO_OF_ROWS][NO_OF_COLUMNS] = 0;
  18. for (row = 0; row < NO_OF_ROWS; row++)
  19. matrix[NO_OF_ROWS][NO_OF_COLUMNS] = 0;
  20.  
  21. // print
  22.  
  23. //Input
  24. cout << "Enter the 16 integers for data. " << endl;
  25. for (col = 0; col < NO_OF_COLUMNS; col++)
  26.  
  27. for (row = 0; row < NO_OF_ROWS; row++)
  28.  
  29. cin >> matrix[row][col];
  30.  
  31.  
  32. //print
  33. for (row = 0; row < NO_OF_ROWS; row++)
  34. {
  35. for (col = 0; col < NO_OF_COLUMNS; col++)
  36. cout << setw(5) << matrix[row][col] << " ";
  37. cout << endl;
  38. }
  39.  
  40. // swap contents of row to the column
  41.  
  42. for(row = 0; row < NO_OF_ROWS-1; row--)
  43.  
  44.  
  45.  
  46.  
  47. // print
  48. int copyArray(int row,int col,int arraysize);
  49.  
  50.  
  51.  
  52.  
  53. {
  54. for (col = 0; col < NO_OF_COLUMNS; col++)
  55. cout << setw(5) << matrix[row][col] << " ";
  56. cout << endl;
  57. }
  58. //interchange rows and columns
  59.  
  60. return 0;
  61. }
Last edited by WolfPack; Oct 12th, 2007 at 11:41 pm. Reason: Add Code Tags next time you post.
Reputation Points: 4
Solved Threads: 0
Newbie Poster
Linda1 is offline Offline
17 posts
since Oct 2007
Oct 12th, 2007
-1

Re: C++ Multi dimensional arrays

The code in lines 16-20 is all wrong. you need nested loops to do that, and array indices are the values of the loop counters, like this:
C++ Syntax (Toggle Plain Text)
  1. array[row][col] = 0;

Or even easier -- you don't have to do the above at all if you initialize the array when it is declared
C++ Syntax (Toggle Plain Text)
  1. int matrix[NO_OF_ROWS][NO_OF_COLUMNS] = {0};
Last edited by Ancient Dragon; Oct 12th, 2007 at 11:59 pm.
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
Oct 13th, 2007
0

Re: C++ Multi dimensional arrays

The compiler builds it and the output is ok. My problem is swapping the data from the column to the rows and displaying the new output. I've been working on this and still could not get it right.
Reputation Points: 4
Solved Threads: 0
Newbie Poster
Linda1 is offline Offline
17 posts
since Oct 2007
Oct 13th, 2007
0

Re: C++ Multi dimensional arrays

>>The compiler builds it and the output is ok
Yes the compiler will build it, but its not ok. What you have is called buffer overrun. Look at what you posted and THINK about it for a couple minutes. Why do I say buffer overrun -- because array element at indices matrix[NO_OF_ROWS][NO_OF_COLUMNS] does not exist. Your program is scribbling all over memory outside he boundries of the array.
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
Oct 13th, 2007
0

Re: C++ Multi dimensional arrays

I managed to get it!! Anyway, here's the code just in case someone comes along with the same question.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
const int NO_OF_ROWS = 4;
const int NO_OF_COLUMNS =4;
int matrix[NO_OF_ROWS][NO_OF_COLUMNS];
int copyArray(int row,int col,int arraysize);
const int END_OF_ROW = 4;
const int END_OF_COL =4;
int row;
int col;

//initialize

for (col = 0; col < NO_OF_COLUMNS; col++)
matrix[NO_OF_ROWS][NO_OF_COLUMNS] = 0;
for (row = 0; row < NO_OF_ROWS; row++)
matrix[NO_OF_ROWS][NO_OF_COLUMNS] = 0;

// print

//Input
cout << "Enter the 16 integers for data. " << endl;
for (col = 0; col < NO_OF_COLUMNS; col++)

for (row = 0; row < NO_OF_ROWS; row++)

cin >> matrix[row][col];


//print
for (row = 0; row < NO_OF_ROWS; row++)
{
for (col = 0; col < NO_OF_COLUMNS; col++)
cout << setw(5) << matrix[row][col] << " ";
cout << endl;
}
for (col = 0; col < NO_OF_COLUMNS ; col++)
{
for (row = 0; row < NO_OF_ROWS ; row++)

;}
cout << endl;

for (row = 0; row < NO_OF_ROWS; row++)
{
for (col = 0; col < NO_OF_COLUMNS; col++)
cout << setw(5) << matrix[col][row] << " ";
cout << endl;
}



// print

return 0;





}
Reputation Points: 4
Solved Threads: 0
Newbie Poster
Linda1 is offline Offline
17 posts
since Oct 2007
Oct 13th, 2007
0

Re: C++ Multi dimensional arrays

Thanks for your help Ancient Dragon. I managed to solve it myself though.
Reputation Points: 4
Solved Threads: 0
Newbie Poster
Linda1 is offline Offline
17 posts
since Oct 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: Xcode
Next Thread in C++ Forum Timeline: Access violation error in borland c++





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


Follow us on Twitter


© 2011 DaniWeb® LLC