You are here:

C++/An array inside a 2D array

Advertisement


Question

      Hi Zlatko, I added some code to the program in topic " An array inside a 2D array ". After the
user enter five numbers and this numbers are added to the beginning of a matrix, I wanted to know
if this five numbers match with the numbers inside the matrix or not. To do that I use an idea you
taught in other topic. I created an array to hold the array that is initialized in the program with a
size ( 5 x 5 ) and compare it with the numbers entered by the user.   
      The problem is when the user enter the second row of numbers the size of the matrix becomes
in ( 6 x 5 ) and what I want is the size of the matrix to compare with the numbers enter by the user
always be ( 5 x 5 ) This the code a go so far:


#include <iostream>
#include <iomanip>

const int maxRows = 10; // This is the maximum number of rows
int rows=5; // this is the number of rows present
const int columns=5;
bool foundTheNumber = false;

using namespace std;

void printHeading();

void shiftDownOneRow(int theArray[][columns])
{
  for(int shiftRow = rows-1; shiftRow >= 0; --shiftRow)
  {
      for(int col = 0; col < columns; ++col)
      {
          theArray[shiftRow + 1][col] = theArray[shiftRow][col];
      }
  }
  ++rows;
}

int main()
{
  cout<<endl;
  int x=0;
  int y=0;
  char choice;
  int array[maxRows][columns]={ // change to maxRows. The first 5 rows are initialized
                                                    {6,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 foundList[57];
      memset(foundList, 0, sizeof(foundList));
      bool foundTheNumber = false;
      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];
              foundList[ array[i][j] ]=array[i][j];
              cout<<setw(8)<<foundList[ array[i][j] ];
          }
          cout<<endl;
      }
      do
      {
          // shift down the rows of the array
          shiftDownOneRow(array);

          cout<<"\n    Enter 5 numbers: ";
          x=x-x;
          y=y-y;
          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<<endl;
          for(int i=0; i<1; i++)
          {
              for(int j=0; j<columns; j++)
              {
                  if(array[0][j] == foundList[ array[i][j] ])
                  {
                      foundTheNumber = true;
                      x++;
                      cout<<setw(47)<<array[0][j]<<"  IN the matrix \n";
                  }
                  else if(array[0][j] != foundList[ array[i][j] ])
                  {
                      foundTheNumber = true;
                      y++;
                      cout<<setw(47)<<array[0][j]<<"  NOT in matrix \n";
                  }
              }
          }
          cout<<endl;
          if( foundTheNumber )
          {
             cout<<setw(40)<<x<<"  numbers IN matrix"<<endl;
             cout<<setw(40)<<y<<"  numbers NOT in matrix \n\n";
          }     
          cout<<endl;
          for(int i=0; i<rows; i++)
          {
              cout<<setw(33)<< i <<")";
              for(int j=0; j<columns; j++)
              {
                  foundList[ array[i][j] ]=array[i][j]; // I assigned the new matrix values to foundList
                                                                    // but I do not know how to keep this matrix with
                                                                    // the size ( 5 x 5 ) That is my problem.
                  cout<<setw(8)<<foundList[ array[i][j] ];
              }
              cout<<endl;
          }
          cout<<endl;
          // don't allow array to overflow
          if (rows < maxRows)
          {
              cout<<"\n    Would you like to try again? (y/n): ";
              cin >> choice;
          }
          else
          {
              break; // exit the loop
          }
      }  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";
}
            THIS IS THE OUTPUT OF THIS PROGRAM:
 
                      =======================

                        ROWS     (0)     (1)     (2)     (3)     (4)
                      ------------------------------
                               0)       6      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 5 numbers: 1 2 3 4 5

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

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

                                             1  NOT in matrix
                                             2  IN the matrix
                                             3  IN the matrix
                                             4  NOT in matrix
                                             5  IN the matrix

                                      3  numbers IN matrix
                                      2  numbers NOT in matrix

                               0)       1       2       3       4       5
                               1)       6      13      23      45      50
                               2)       5      12      34      36      51
                               3)       2      23      25      46      54
                               4)       2       7      35      39      48
                               5)       3       5      26      39      49   // This is the line I do not need because I
                                                                                     // want to keep the size of the matrix ( 5 X 5 )

   Would you like to try again? (y/n):  

Answer
Hello Raul.

If you don't need to keep the last row, then you can go back to an array of 5 rows as you had in your original code. You can get rid of the maxRows idea. Just change the shifting code to shift numbers from the second last row to the last row, and then go backwards that way until it shifts numbers from the first row to the second. Then the first row will be available to hold new numbers.

void shiftDownOneRow(int theArray[][columns])
{
   /* start at second last row, and shift it to the last
   then keep going backwards until the first row is shifted to the
   second row
   */
   for(int shiftRow = rows-2; shiftRow >= 0; --shiftRow)
   {
       for(int col = 0; col < columns; ++col)
       {
           theArray[shiftRow+1][col] = theArray[shiftRow][col];
       }
   }
}

Your next objective is to see if the new numbers match numbers in any of the other rows. The foundList idea here is not so good because as the user enters rows, you really have no control of what values the user enters. If the user enters 100, or 1000, the foundList would have to accommodate that, but your foundList only goes to 57. The foundList idea is a fast method but not flexible. I suggest you go with a slower, but more flexible method of having a function to check if a number is in the array. I think you don't want to check the first row, so we can make the function flexible by specifying which row range to search.

/*
Search an array from the startingRow to the endingRow for a number. Return true
if the number is found, and false otherwise
*/
bool isInArray(int number, int theArray[][columns], int startingRow, int endingRow)
{
   for(int row = startingRow; row <= endingRow; ++row)
   {
       for(int col = 0; col < columns; ++col)
       {
           if (theArray[row][col] == number) return true;
       }
   }
   return false;
}


Perhaps I misunderstood, and you want to search the original matrix, before inserting the 5 new numbers, if so, then do this:
1) place the 5 original numbers into a temporary array
2) search the original matrix
3) shift down
5) insert the 5 numbers into the matrix

Anyway, here is a program that does something. Maybe it does what you want, but if not, I'm sure you can change it. I have commented-out all the code that uses the foundList.

Best Regards
Zlatko

#include <iostream>
#include <iomanip>

const int rows=5; // this is the number of rows present
const int columns=5;
bool foundTheNumber = false;

using namespace std;

void printHeading();

/*
start at second last row, and shift it to the last
then keep going backwards until the first row is shifted to the
second row
*/
void shiftDownOneRow(int theArray[][columns])
{
   for(int shiftRow = rows-2; shiftRow >= 0; --shiftRow)
   {
       for(int col = 0; col < columns; ++col)
       {
           theArray[shiftRow+1][col] = theArray[shiftRow][col];
       }
   }
}

/*
Search an array from the startingRow to the endingRow for a number. Return true
if the number is found, and false otherwise
*/
bool isInArray(int number, int theArray[][columns], int startingRow, int endingRow)
{
   for(int row = startingRow; row <= endingRow; ++row)
   {
       for(int col = 0; col < columns; ++col)
       {
           if (theArray[row][col] == number) return true;
       }
   }
   return false;
}

int main()
{
   cout<<endl;
   int x=0; // you should make these names more meaningful, like foundCount and notFoundCount
   int y=0;
   char choice;
   int array[rows][columns]={ // change to maxRows. The first 5 rows are initialized
       {6,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 foundList[57];
       //memset(foundList, 0, sizeof(foundList));
       //bool foundTheNumber = false;
       printHeading();

       for(int i=0; i<rows; i++)
       {
           cout<<setw(33)<< i <<")";
           for(int j=0; j<columns; j++)
           {
               //foundList[ array[i][j] ]=array[i][j];
               //cout<<setw(8)<<foundList[ array[i][j] ];
               cout<<setw(8)<< array[i][j];
           }
           cout<<endl;
       }

       do
       {
           // shift down the rows of the array
           shiftDownOneRow(array);

           cout<<"\n    Enter 5 numbers: ";
           x = 0;
           y = 0; // just set it to 0, no need to do y-y or x-x
           // for(int i=0; i<1; i++) There is no point to this loop if the action is only done once
           //{
               for(int j=0; j<columns; j++)
               {
                   cin>>array[0][j]; // just use 0 for the row
               }
           //}
           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<<endl;
           //for(int i=0; i<1; i++)
           //{
           //    for(int j=0; j<columns; j++)
           //    {
           //        if(array[0][j] == foundList[ array[0][j] ])
           //        {
           //            foundTheNumber = true;
           //            x++;
           //            cout<<setw(47)<<array[0][j]<<"  IN the matrix \n";
           //        }
           //        else if(array[0][j] != foundList[ array[0][j] ])
           //        {
           //            foundTheNumber = true;
           //            y++;
           //            cout<<setw(47)<<array[0][j]<<"  NOT in matrix \n";
           //        }
           //    }
           //}
           for(int j=0; j<columns; j++)
           {
               // Remember, rows is the count of the number of rows
               // the last valid row index is rows-1, because indexes start at 0
               if (isInArray(array[0][j], array, 1, rows-1))
               {
                   x++;
                   cout<<setw(47)<<array[0][j]<<"  IN the matrix \n";
               }
               else
               {
                   y++;
                   cout<<setw(47)<<array[0][j]<<"  NOT in matrix \n";
               }
           }

           cout<<endl;
           //if( foundTheNumber )
           {
               cout<<setw(40)<<x<<"  numbers IN matrix"<<endl;
               cout<<setw(40)<<y<<"  numbers NOT in matrix \n\n";
           }     
           cout<<endl;
           //for(int i=0; i<rows; i++)
           //{
           //    cout<<setw(33)<< i <<")";
           //    for(int j=0; j<columns; j++)
           //    {
           //        foundList[ array[i][j] ]=array[i][j]; // I assigned the new matrix values to foundList
           //        // but I do not know how to keep this matrix with
           //        // the size ( 5 x 5 ) That is my problem.
           //        cout<<setw(8)<<foundList[ array[i][j] ];
           //    }
           //    cout<<endl;
           //}
           cout<<endl;
           cout<<"\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";
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.