By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,991 Members | 1,887 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,991 IT Pros & Developers. It's quick & easy.

array inside an array in C++

P: 60
Hi, what I am trying to do in this program is to save the numbers enter by the user into the array data but with any positive result. This is what I got so far. Thanks for any help.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. void printHeading();
  7.  
  8. int main()
  9. {
  10.     cout<<endl;
  11.     char choice;
  12.     int rows=5;
  13.     int columns=5;
  14.     int array[5][5]={{2,13,23,45,50},
  15.                              {5,12,34,36,51},
  16.                              {2,23,25,46,54},
  17.                              {2,7,35,39,48},
  18.                              {3,5,26,39,49}};
  19.  
  20.     int newArray[5];
  21.     printHeading();
  22.  
  23.     for(int i=0; i<rows; i++)
  24.     {
  25.         cout<<setw(33)<< i <<")";
  26.         for(int j=0; j<columns; j++)
  27.         {
  28.             cout<<setw(8)<<array[i][j];
  29.         }
  30.         cout<<endl;
  31.     }
  32.     do
  33.     {
  34.         cout<<"\n    Enter 5 numbers: ";
  35.         for(int i=0; i< 5; i++)
  36.         {
  37.             cin>>newArray[i];
  38.         }    
  39.         cout<<endl;
  40.         printHeading();
  41.         for(int i=0; i<rows; i++)
  42.         {
  43.             cout<<setw(33)<< i <<")";
  44.             for(int j=0; j<columns; j++)
  45.             {
  46.                 newArray[ array[i][j] ]=array[i][j]; // saving the array data into the newArray
  47.                 cout<<setw(8)<<newArray[ array[i][j] ];             
  48.             }
  49.             cout<<endl;
  50.          }
  51.  
  52.     cout<<"\n\n    Would you like to try again? (y/n): ";
  53.     cin >> choice;
  54.  
  55.     }  while (choice == 'y'|| choice == 'Y');
  56.     cout<<endl;
  57.     return 0;
  58. }
  59.  
  60. void printHeading()
  61. {
  62.     cout<<setw(82)<<"=====================================================\n\n";
  63.     cout<<setw(76)<<"ROWS     (0)     (1)     (2)     (3)     (4)\n";
  64.     cout<<setw(81)<<"-----------------------------------------------------\n";
  65. }
Aug 28 '10 #1
Share this Question
Share on Google+
8 Replies


100+
P: 542
Use your j for loop to overwrite the 2d data under l 28 and do away with newArray[]
Aug 28 '10 #2

P: 60
Hi whodgson, I think I did what you told me and it works, but not the way I want because the five numbers I entered replaced the first row of the array data and what I want is add it to the array data. This is what I got so far:


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. const int rows=5;
  5. const int columns=5;
  6.  
  7. using namespace std;
  8.  
  9. void printHeading();
  10.  
  11. int main()
  12. {
  13.     cout<<endl;
  14.     char choice;
  15.     int array[rows][columns]={{2,13,23,45,50},
  16.                               {5,12,34,36,51},
  17.                               {2,23,25,46,54},
  18.                               {2,7,35,39,48},
  19.                               {3,5,26,39,49}};
  20.  
  21.     printHeading();
  22.  
  23.     for(int i=0; i<rows; i++)
  24.     {
  25.         cout<<setw(33)<< i <<")";
  26.         for(int j=0; j<columns; j++)
  27.         {
  28.             cout<<setw(8)<<array[i][j];
  29.         }
  30.         cout<<endl;
  31.     }
  32.     do
  33.     {
  34.         cout<<"\n    Enter 5 numbers: ";
  35.  
  36.         for(int i=0; i<1; i++)
  37.         {
  38.             for(int j=0; j<columns; j++)
  39.             {
  40.                cin>>array[i][j];
  41.             }
  42.         }
  43.         cout<<endl;
  44.         printHeading();
  45.         for(int i=0; i<rows; i++)
  46.         {
  47.             cout<<setw(33)<< i <<")";
  48.             for(int j=0; j<columns; j++)
  49.             {
  50.                 cout<<setw(8)<<array[i][j];
  51.             }
  52.             cout<<endl;
  53.          }
  54.     cout<<"\n\n    Would you like to try again? (y/n): ";
  55.     cin >> choice;
  56.  
  57.     }  while (choice == 'y'|| choice == 'Y');
  58.     cout<<endl;
  59.     return 0;
  60. }
  61.  
  62. void printHeading()
  63. {
  64.     cout<<setw(82)<<"=====================================================\n\n";
  65.     cout<<setw(76)<<"ROWS     (0)     (1)     (2)     (3)     (4)\n";
  66.     cout<<setw(81)<<"-----------------------------------------------------\n";
  67. }
  68.  
Can you help me. THANKS
Aug 29 '10 #3

100+
P: 542
That was probably because of line 40.
Use an if statement/s in the body of the j loop like:
Expand|Select|Wrap|Line Numbers
  1. cin>>num;//get a number from user
  2. if(i==0 && j==2)array[i][j]=num;//overwrite with  num in 1st row and 3rd column.
Repeat for remaining input.
Aug 30 '10 #4

P: 60
I am not sure if you understood my problem. This is my output from the last code:


ROWS (0) (1) (2) (3) (4)
-----------------------------------------------------
0) 2 13 23 45 50
1) 5 12 34 36 51
2) 2 23 25 46 54
3) 2 7 35 39 48
4) 3 5 26 39 49

Enter a numbers: 1 1 1 1 1

================================================== ===

ROWS (0) (1) (2) (3) (4)
-----------------------------------------------------
0) 1 1 1 1 1
1) 5 12 34 36 51
2) 2 23 25 46 54
3) 2 7 35 39 48
4) 3 5 26 39 49

First row was replaced by the numbers entered by the user.
But if you understood what it really happen, then is me who can not understand what you trying to explain me . Can you tell me what happen. Once again THANKS
Aug 30 '10 #5

P: 60
Is there anybody can help me out to understand what is wrong in my program. Thanks
Aug 30 '10 #6

Expert 100+
P: 2,396
When the user enters a set of 5 numbers, do you want that set to replace one of the rows in the array or do you want that set to be appended to the end of the array (changing the array dimension from [5][5] to [6][5])?
Aug 30 '10 #7

P: 60
Hi donbock, thanks for your question.
What I want is the set of 5 numbers entered by the user be appended to the beginning of the array and therefore this is going to change the array dimension from [5][5] to [6][5]
Tell me if I wrong, because the size of the array is going to change is desirable the user determine at runtime the size of the array. To accomplish this I need to declare the array using dynamic memory allocation, if so, I tried but I did not know how to code it.
Aug 30 '10 #8

Expert 100+
P: 2,396
Refer to Arrays Revealed for advice on how to dynamically allocate two-dimensional arrays.

Are you sure a two-dimensional array is needed? My inclination would be to define a structure for each set of 5 values, dynamically allocate a structure each time a new set is introduced, and link the structures together:
Expand|Select|Wrap|Line Numbers
  1. struct set {
  2.     struct set *next, *prior;
  3.     int values[5];
  4.     };
(You may not need the prior pointer.)
Aug 30 '10 #9

Post your reply

Sign in to post your reply or Sign up for a free account.