C++/How to display a message and numbers not present into a matrix
Expert: Zlatko - 8/9/2010
QuestionHi Zlatko
What I want from the following program is to get the rows with a desired
number in column (0) and if this number is not in column (0), then display a
message (This number is not found in column (0)) and after that, obtain the
numbers not presents in the whole matrix between 1 and 56 that I got when I
entered a desired number.
I could create the code for the first part but I do not know how to display
a message if the number is not in column (0), besides, I do not know how to
obtain the numbers not presents in the whole matrix that I got when I entered
a desired number. Could you help me. Thanks.
This is what I got so far:
#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;
for(int i=0; i< rows; i++)
{
{
for(int j=0; j< 1; j++)
if(array[i][0]==wishedNumber)
{
cout<<setw(19)<< i <<")";
displayRows(array[i], columns);
}
}
}
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;
}
AnswerHello Raul, I had a little trouble understanding what you want, but I think you want to display if a number is in the first column, and if not, then go on to check if it is in the rest of the matrix.
Below is the code for that. Between the lines
/* BEGIN addition to code */
and
/* END addition to code */
is code that I added. I think you should be able to understand it.
Let me know if you need more help.
Best regards
Zlatko
#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;
/* Check if the wished number is in the first column */
bool foundInFirstCol = false; // ZM added this
for(int i=0; i< rows; i++)
{
if(array[i][0]==wishedNumber)
{
foundInFirstCol = true; // ZM added this
cout<<setw(19)<< i <<")";
displayRows(array[i], columns);
}
}
/* BEGIN addition to code */
/* if the number is not in the first column, see if it is in the rest of the matrix */
if (! foundInFirstCol)
{
cout << "NUMBER " << wishedNumber << " is not in the first column\n";
bool foundInMatrix = false;
for(int i=0; i< rows && !foundInMatrix; i++)
{
for (int j = 0; j < columns && !foundInMatrix; ++j)
{
if(array[i][j]==wishedNumber)
{
foundInMatrix = true;
}
}
}
if (! foundInMatrix)
{
cout << "NUMBER " << wishedNumber << " is not in the matrix\n";
}
else
{
cout << "NUMBER " << wishedNumber << " is in the matrix\n";
}
}
/* END addition to code */
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;
}