C++ Multi dimensional arrays

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2007
Posts: 17
Reputation: Linda1 has a little shameless behaviour in the past 
Solved Threads: 0
Linda1 Linda1 is offline Offline
Newbie Poster

C++ Multi dimensional arrays

 
0
  #1
Oct 12th, 2007
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?

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ Multi dimensional arrays

 
0
  #2
Oct 12th, 2007
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:
  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
  1. int matrix[NO_OF_ROWS][NO_OF_COLUMNS] = {0};
Last edited by Ancient Dragon; Oct 12th, 2007 at 11:59 pm.
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 17
Reputation: Linda1 has a little shameless behaviour in the past 
Solved Threads: 0
Linda1 Linda1 is offline Offline
Newbie Poster

Re: C++ Multi dimensional arrays

 
0
  #3
Oct 13th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ Multi dimensional arrays

 
0
  #4
Oct 13th, 2007
>>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.
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 17
Reputation: Linda1 has a little shameless behaviour in the past 
Solved Threads: 0
Linda1 Linda1 is offline Offline
Newbie Poster

Re: C++ Multi dimensional arrays

 
0
  #5
Oct 13th, 2007
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;





}
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 17
Reputation: Linda1 has a little shameless behaviour in the past 
Solved Threads: 0
Linda1 Linda1 is offline Offline
Newbie Poster

Re: C++ Multi dimensional arrays

 
0
  #6
Oct 13th, 2007
Thanks for your help Ancient Dragon. I managed to solve it myself though.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 7506 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC