array inside an array in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • capablanca
    New Member
    • May 2010
    • 60

    array inside an array in C++

    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.

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    void printHeading();
    
    int main()
    {
        cout<<endl;
        char choice;
        int rows=5;
        int columns=5;
        int array[5][5]={{2,13,23,45,50},
                                 {5,12,34,36,51},
                                 {2,23,25,46,54},
                                 {2,7,35,39,48},
                                 {3,5,26,39,49}};
    
        int newArray[5];
        printHeading();
    
        for(int i=0; i<rows; i++)
        {
            cout<<setw(33)<< i <<")";
    		for(int j=0; j<columns; j++)
            {
                cout<<setw(8)<<array[i][j];
            }
            cout<<endl;
        }
        do
        {
            cout<<"\n    Enter 5 numbers: ";
            for(int i=0; i< 5; i++)
            {
                cin>>newArray[i];
            }    
            cout<<endl;
            printHeading();
            for(int i=0; i<rows; i++)
            {
                cout<<setw(33)<< i <<")";
                for(int j=0; j<columns; j++)
                {
                    newArray[ array[i][j] ]=array[i][j]; // saving the array data into the newArray
                    cout<<setw(8)<<newArray[ array[i][j] ];             
                }
                cout<<endl;
             }
    
        cout<<"\n\n    Would you like to try again? (y/n): ";
        cin >> choice;
    
        }  while (choice == 'y'|| choice == 'Y');
        cout<<endl;
        return 0;
    }
    
    void printHeading()
    {
        cout<<setw(82)<<"=====================================================\n\n";
        cout<<setw(76)<<"ROWS     (0)     (1)     (2)     (3)     (4)\n";
        cout<<setw(81)<<"-----------------------------------------------------\n";
    }
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    Use your j for loop to overwrite the 2d data under l 28 and do away with newArray[]

    Comment

    • capablanca
      New Member
      • May 2010
      • 60

      #3
      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:


      Code:
      #include <iostream>
      #include <iomanip>
      
      const int rows=5;
      const int columns=5;
      
      using namespace std;
      
      void printHeading();
      
      int main()
      {
          cout<<endl;
          char choice;
          int array[rows][columns]={{2,13,23,45,50},
                                    {5,12,34,36,51},
                                    {2,23,25,46,54},
                                    {2,7,35,39,48},
                                    {3,5,26,39,49}};
      
          printHeading();
      
          for(int i=0; i<rows; i++)
          {
              cout<<setw(33)<< i <<")";
      		for(int j=0; j<columns; j++)
              {
                  cout<<setw(8)<<array[i][j];
              }
              cout<<endl;
          }
          do
          {
              cout<<"\n    Enter 5 numbers: ";
      
              for(int i=0; i<1; i++)
              {
                  for(int j=0; j<columns; j++)
                  {
                     cin>>array[i][j];
                  }
              }
              cout<<endl;
              printHeading();
              for(int i=0; i<rows; i++)
              {
                  cout<<setw(33)<< i <<")";
                  for(int j=0; j<columns; j++)
                  {
                      cout<<setw(8)<<array[i][j];
                  }
                  cout<<endl;
               }
          cout<<"\n\n    Would you like to try again? (y/n): ";
          cin >> choice;
      
          }  while (choice == 'y'|| choice == 'Y');
          cout<<endl;
          return 0;
      }
      
      void printHeading()
      {
          cout<<setw(82)<<"=====================================================\n\n";
          cout<<setw(76)<<"ROWS     (0)     (1)     (2)     (3)     (4)\n";
          cout<<setw(81)<<"-----------------------------------------------------\n";
      }
      Can you help me. THANKS

      Comment

      • whodgson
        Contributor
        • Jan 2007
        • 542

        #4
        That was probably because of line 40.
        Use an if statement/s in the body of the j loop like:
        Code:
        cin>>num;//get a number from user
        if(i==0 && j==2)array[i][j]=num;//overwrite with  num in 1st row and 3rd column.
        Repeat for remaining input.

        Comment

        • capablanca
          New Member
          • May 2010
          • 60

          #5
          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

          Comment

          • capablanca
            New Member
            • May 2010
            • 60

            #6
            Is there anybody can help me out to understand what is wrong in my program. Thanks

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              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])?

              Comment

              • capablanca
                New Member
                • May 2010
                • 60

                #8
                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.

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  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:
                  Code:
                  struct set {
                      struct set *next, *prior;
                      int values[5];
                      };
                  (You may not need the prior pointer.)

                  Comment

                  Working...