User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,545 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,363 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 3238 | Replies: 5 | Solved
Reply
Join Date: Oct 2007
Posts: 16
Reputation: Linda1 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Linda1 Linda1 is offline Offline
Newbie Poster

Tutorial C++ Multi dimensional arrays

  #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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: C++ Multi dimensional arrays

  #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:
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
int matrix[NO_OF_ROWS][NO_OF_COLUMNS] = {0};
Last edited by Ancient Dragon : Oct 12th, 2007 at 11:59 pm.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Oct 2007
Posts: 16
Reputation: Linda1 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Linda1 Linda1 is offline Offline
Newbie Poster

Re: C++ Multi dimensional arrays

  #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  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: C++ Multi dimensional arrays

  #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.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Oct 2007
Posts: 16
Reputation: Linda1 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Linda1 Linda1 is offline Offline
Newbie Poster

Re: C++ Multi dimensional arrays

  #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  
Join Date: Oct 2007
Posts: 16
Reputation: Linda1 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Linda1 Linda1 is offline Offline
Newbie Poster

Re: C++ Multi dimensional arrays

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

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 5:05 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC