C++/How to display a message and numbers not present into a matrix
Expert: Zlatko - 8/13/2010
QuestionHi Zlatko, What I really want is a display like this:
ROWS (0) (1) (2) (3) (4)
-----------------------------------------------------
0) 1 13 23 45 50
1) 5 12 34 36 51
2) 2 23 25 46 54
3) 2 7 35 39 48
4) 34 5 26 39 49
Enter the number you are searching for in column (0): 2
NUMBER 2 in COLUMN (0) in 5 ROWS
===============================================
ROWS (0) (1) (2) (3) (4)
-----------------------------------------------------
2) 2 23 25 46 54
3) 2 7 35 39 48
Enter the number one (1): 1
NUMBERS THAT ARE NOT IN THE MATRIX
BETWEEN THE NUMBERS 1 AND 56
============================================
1 3 4 5 6 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 24 26 27
28 29 30 31 32 33 34 36 37 38 40 41 42 43 44 45 47 49 50 51 52
53 55 56
If you see, I displayed all the numbers between 1 and 56 not present in the
following matrix:
NUMBER 2 in COLUMN (0) in 5 ROWS
ROWS (0) (1) (2) (3) (4)
-----------------------------------------------------
2) 2 23 25 46 54
3) 2 7 35 39 48
This is the code I used to get that:
if (foundInFirstCol)
{
cout<<" Enter the number one (1): ";
cin>>Number;
cout<<setw(70)<<"NUMBERS THAT ARE NOT IN THE MATRIX \n";
cout<<setw(66)<<"BETWEEN THE NUMBERS 1 AND 56\n";
cout<<setw(75)<<"===================================
=========\n\n";
do{
bool foundInMatrix = false;
for(int i=0; i< rows && !foundInMatrix; i++)
{
for(int j = 0; j < columns && !foundInMatrix; ++j)
{
if(array[2][j]==Number|| // I used this code just to show you what
array[3][j]==Number) // I really want to display
{
foundInMatrix = true;
}
}
}
if (! foundInMatrix)
{
cout<<" "<<Number;
}
Number++;
}while(Number<=56);
}
THANKS.
AnswerHello Raul. I think I understand your question now.
Step 1) The program asks for a number. Then it returns all the rows that have that number in column 0.
Step 2) The program prints out all numbers between 1 and 56 which are not in the rows returned in step 1.
A simple and efficient way of doing this is to make an array of 57 integers. Go through the rows returned from step 1, and for each number (i), set array[i] = 1. In the end, if element i is not 0, it means that i is in the matrix. Then go through the array and print out all i where array[i] is 0.
The code below does it. I hope it is clear enough.
#include <iostream>
#include <iomanip>
int rows=5;
int columns=5;
int minWishedNumber=1;
int maxWishedNumber=56;
int wishedNumber;
using namespace std;
int getWishedNumber();
void displayRows(int array[], int index);
void printHeading();
int main()
{
cout<<endl;
char choice;
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}};
printHeading();
for(int i=0; i<rows; i++)
{
cout<<setw(19)<< i <<")";
for(int j=0; j<columns; j++)
{
cout<<setw(8)<<array[i][j];
}
cout<<endl;
}
do
{
getWishedNumber();
cout<<endl<<endl;
cout<<setw(33)<<"NUMBER "<<wishedNumber<<" in COLUMN (0) in " <<rows<<" ROWS."<<endl;
printHeading();
cout<<endl;
/*
The foundList will record which numbers are in the matrix.
If foundList[i] is not 0, it means that i is in the matrix.
*/
int foundList[57];
memset(foundList, 0, sizeof(foundList));
bool foundInFirstCol = false;
/* Check if the wished number is in the first column */
for(int i=0; i< rows; i++)
{
if(array[i][0]==wishedNumber)
{
cout<<setw(19)<< i <<")";
displayRows(array[i], columns);
foundInFirstCol = true;
/*
For the row that has the wishedNumber in the first column,
examine all the elements in that row and record them in the foundList
*/
for(int col = 0; col < columns; ++col)
{
foundList[array[i][col]] = 1;
}
}
}
if (foundInFirstCol)
{
/* Print out the elements not in the foundList */
cout << "NUMBERS THAT ARE NOT IN THE RETURNED MATRIX\n"
"BETWEEN THE NUMBERS 1 AND 56\n"
"============================================\n";
int count = 0;
for(int i = 1; i < 57; ++i)
{
if (foundList[i] == 0)
{
cout << setw(3) << i;
/* print out 10 to a line */
if (++count % 10 == 0) cout << endl;
}
}
cout << endl;
}
else
{
cout << "NUMBER " << wishedNumber << " is not in the first column\n";
}
cout<<endl;
cout<<"\n Would you like to look up another ";
cout<<"number in column (0)? (y/n): ";
cin >> choice;
} while (choice == 'y'|| choice == 'Y');
cout<<endl;
return 0;
}
int getWishedNumber()
{
cout<<"\n Enter the number you are searching for in column (0): ";
cin >> wishedNumber;
while (wishedNumber < minWishedNumber || wishedNumber >
maxWishedNumber)
{
cout<<"\n I'm sorry, enter a number in the range of " <<
minWishedNumber;
cout <<" through " << maxWishedNumber << ": ";
cin >> wishedNumber;
cout<<endl;
}
return wishedNumber;
}
void displayRows(int array[], int index)
{
for( int i=0; i < columns; i++)
{
cout << setw(8) << array[i];
}
cout << endl;
}
void printHeading()
{
cout<<setw(68)<<"=====================================================\n\n";
cout<<setw(61)<<"ROWS (0) (1) (2) (3) (4)"<<endl<<endl;
}