You are here:

C++/How to get the correct rows from a second array

Advertisement


Question
Hello Zlatko, I have been trying to create a simple program on how to obtain certain rows from a matrix of numbers, which is created from a matrix data. This matrix that is formed by the matrix data has a peculiarity that is generated by a specific number that is selected by the user that is found in the first column of the matrix data, i.e. the column zero (0).
  What I've done so far is to create two boolean arrays ( reported[i] and reported2[i] ) where the first array ( reported[i] ) is used to obtain the matrix that is created for a number specific in the column (0) and the second array ( reported2[i] ) will be used so that this will allows me to obtain the rows I need, and here lies my problem, in as the array ( reported2[i] ) make the correct selection of rows I need, I think that in order that the array ( reported2[i] ) do its work, will depend on a function that indicates how to make the correct selection of rows I have need.
  I believe that if it is necessary to create such a function its argument would be : index of the rows that i need, the array where it is going to find these rows and the beginning and end of this search within the array. I have come to this conclusion by a function that you created in a past issue, which is usually to find a number in a particular place within an array, this is the function:
bool isInArray(int number, int theArray[][columns],
         int startingRow, int endingRow)
{
   for(int row = startingRow; row <= endingRow; ++row)
   {
       if (startingRow < 0) cout << "Error: startingRow < 0
";
       if (endingRow < 0) cout << "Error: endingRow < 0
";
       if (endingRow < startingRow) cout << "Error: endingRow < startingRow < 0
";

       for(int col = 0; col < columns; ++col)
       {
         if (theArray[row][col] == number) return true;
       }
   }
   return false;
}

If you run the following program please select the number 2 as the number you are searching in column (0):


#include <iostream>
#include <iomanip>
#include <sstream>

const int rows= 17;
const int columns= 5;

using namespace std;

void printHeading();
void displayRowMatch(int*, int );

         
int main()
{
  cout<<endl;
  int numberOfRow;
  int numberOfRow2;
  bool foundTheNumber;
  bool foundTheNumber2;
  char choice;
  bool reported[rows];
  memset(reported, 0, sizeof(reported));
  bool reported2[rows];
  memset(reported2, 0, sizeof(reported2));
  int wishedNumber;
  int countingRows= 0;
  
  int array[rows][columns] ={ {2, 10, 11, 23, 54},
         {7, 11, 14, 15, 34},
         {7, 12, 13, 27, 51},
         {1, 31, 33, 34, 50},
         {8, 18, 45, 47, 50},
         {1, 10, 12, 32, 36},
         {4, 12, 13, 15, 50 },
         {2, 12, 14, 23, 51},
         {2, 10, 11, 23, 54},
         {3, 4, 15, 17, 20},
         {1, 2, 13, 19, 27},
         {12, 17, 21, 23, 30},
         {11, 36, 37, 41, 55},
         {4, 12, 15, 15, 50 },
         {2, 16, 21, 43, 54},
         {2, 17, 18, 20, 53},
         {2, 18, 19, 20, 52}};
      printHeading();

      for(int i=0; i< rows; ++i)
      {
         cout<<setw(30)<< i <<")  |";
         
         for(int j=0; j< columns; ++j)
         {
         cout<<setw(8)<<array[i][j];
         }
         cout<<endl;
     }
     cout<<endl<<endl;
   do
   {
       memset(reported, 0, sizeof(reported));
       memset(reported2, 0, sizeof(reported2));
       foundTheNumber = false;
       foundTheNumber2 = false;
       
       cout<<"    Enter in how many rows would you like to search for a number : ";
       cin>> numberOfRow;
   do
   {
       countingRows = 0;
       cout<<"    Enter the number you are searching for in column (0) "
         "of the matrix ("<< numberOfRow <<" x 5): ";
       cin >> wishedNumber;
       cout<<endl<<endl;
       cout<<setw(55)<<"Rows with the number "<<wishedNumber<<" in column (0)
";
         
       cout<<setw(85)<<"----------------------------------------"
         "-------------------------
";
       printHeading();
       
       for(int i = 0; i< numberOfRow; i++)
       {
         if(!reported[i])
         {
         if(array[i][0] == wishedNumber)
         {          
         foundTheNumber = true;
         if (! reported[i])
         {
         cout<<setw(30)<< i <<")  |";
         reported[i] = true;
         ++countingRows;
         }          
         if (reported[i])
         {
         displayRowMatch(array[i], columns);
         }
         cout<<endl;
         }
         }
      }
      cout<<endl<<endl;
      
      cout<<"    Enter how many rows you will need
";
      cout<<"    from the matrix ("<< countingRows <<" x "<< columns <<")";
      cout<<" that you just created: ";
      cin>> numberOfRow2;
      cout<<endl<<endl;
      cout<<setw(37)<<" These are the "<< numberOfRow2 <<" rows I was needing from ";
      cout<<"the matrix ("<< countingRows <<" x "<< columns <<")
";
      cout<<setw(90)<<"-------------------------------------------------"
         "-------------------------
";          
       printHeading();
       
       if( foundTheNumber )
       {
         for(int i = 0; i <  numberOfRow; ++i)
         {
         if(reported[i])
         {
         if( !reported2[i] )
         {
         if( array[i][0] == wishedNumber ) // This if() is useless
         {
         foundTheNumber2 = true;
         if( !reported2[i] )
         {          
         cout<<setw(30)<< i <<")  |";
         reported2[i] = true;
         }          
         if( reported2[i] )
         {
         displayRowMatch(array[i], columns);
         }
         cout<<endl;
         }
         }          
         }
         }
      }      
      cout<<endl<<endl;
         
       cout<<"    Would you like to look up another "
         "number in column (0) (y/n)? : ";
       cin >> choice;
   }   while (choice == 'y'|| choice == 'Y');
   
       cout<<"    Would you like to do a search "
         "for a number in another size of a matrix (y/n)? : ";
       cin >> choice;
   }   while (choice == 'y'|| choice == 'Y');

     system("PAUSE");
     return 0;
}

void printHeading()
{
   cout<<endl;
   cout<<setw(76)<<"  Rows        (0)     (1)     (2)     (3)     (4)
";
   
   cout<<setw(82)<<"-----------------------------------------------------------
";
         
}

void displayRowMatch(int* array, int index)
{
  for( int i=0; i < index; i++)
  {
      cout << setw(8) << array[i];
  }
}

THANKS.

Answer
Hello Raul

I was a little confused by your question so I may not understand completely what your are trying to do, but I read through your program and it became more clear. Here is what I think.

It seems that you have a large int array of rows x columns. Lets call it A. From A you want to examine numberOfRow rows looking for rows that have the wishedNumber in column 0. These rows you print out as a second matrix. Lets call it B. Then you want the user to specify how many rows from B to print out and display those as a third matrix C.

The problem I see is that when matrix C is printed out, it is the same as matrix B, and not a subset of it.

If I input 17 for numberOfRow, then 2 for wishedNumber, I get a 6x5 matrix. Then if I input 3 for numberOfRow2, I get the same 6x5 matrix, instead of a 3x5.

I think your reported array is useful. It records the rows which make up the middle matrix B.
When printing matrix C, you need a single loop which goes through all the rows of the original matrix A, print out the rows where reported[i] is true, and increment a counter. When the counter reaches numberOfRow2 you are done.

When printing matrix C, you do not need to check for the wishedNumber again because you already know that the wishedNumber is present if reported[i] is true, and you do not need a reported2 array at all.

Here is the loop I think you need to display matrix C

         if( foundTheNumber )
         {
         int displayed = 0;
         for(int i = 0; i <  numberOfRow && displayed < numberOfRow2; ++i)
         {
         if(reported[i])
         {
         cout<<setw(30)<< i <<")  |";
         displayRowMatch(array[i], columns);
         cout<<endl;
         ++displayed;
         }
         }
         }

Try that and tell me if it gives you what you expect.

The variable foundTheNumber2 is only ever written to. It is never read from, so you can safely delete it. I don't think you need the isInArray function because you are interested only in column 0, and that function searches all columns from a group of rows.

I think you need to move
memset(reported, 0, sizeof(reported));
into your inner do/while loop. As it stands now, the user cannot choose the same wished number again, and when choosing a new wished number the rows reported from the previous wished number will interfere with the display of matrix C.
Good luck. Let me know how it goes.

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.

©2016 About.com. All rights reserved.