In: Computer Science
The source code I have is what i'm trying to fix for the assignment at the bottom.
Source Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
const int NUM_ROWS = 10;
const int NUM_COLS = 10;
// Setting values in a 10 by 10 array of random integers (1 -
100)
// Pre: twoDArray has been declared with row and column size of
NUM_COLS
// Must have constant integer NUM_COLS declared
// rowSize must be less than or equal to NUM_COLS
// Post: All twoDArray values set to integers 1 - 100
void InitArray(/* OUT */ int twoDArray[][NUM_COLS], /* IN */ int
rowSize);
// Display the array
// Pre: twoDArray contains integers with row and column size of
NUM_COLS
// Must have constant integer NUM_COLS declared
// rowSize must be less than or equal to NUM_COLS
// Post: Display all values in array in pretty format
void PrintArray(/* IN */ const int twoDArray[][NUM_COLS], /* IN */
int rowSize);
// Accepts an integer as a parameter and returns its first
location in the array
// Pre: twoDArray contains integers with row and column size of
NUM_COLS
// Must have constant integer NUM_COLS declared
// rowSize must be less than or equal to NUM_COLS
// numGuessed contains valid integer
// Post: Return true if numGuessed is in array as well as set
rowLoc and colLoc coordinates
// Return false if numGuessed not found and set rowLoc and colLoc
to -1
bool FindNumber(/* IN */ const int twoDArray[][NUM_COLS], /* IN */
int rowSize,
/* IN */ int numGuessed, /* OUT */ int &rowLoc, /* OUT */ int
&colLoc);
//Setting values in a 10 by 10 array of random integers (1 -
100)
// Pre: twoDArray has been declared with row and column size of
NUM_COLS
// Must have constant integer NUM_COLS declared
// rowSize must be less than or equal to NUM_COLS
// Post: All twoDArray values set to integers 1 - 100
void InitArray(/* OUT */ int twoDArray[][NUM_COLS], /* IN */ int
rowSize)
{
cout << "init\n";
// Loop through each row
for (int row = 0; row < rowSize; row++)
{
// Loop through each column
for (int col = 0; col < rowSize; col++)
{
// printing values from array
twoDArray[row][col] = rand() % 100 + 1;
cout << twoDArray[row][col] << ' ';
}
cout<<endl;
}
}
void PrintArray(/* OUT */ const int twoDArray[][NUM_COLS], /* IN
*/ int rowSize)
{
cout << "print\n";
// Loop through each row
for (int row = 0; row < rowSize; row++)
{
// Loop through each column
for (int col = 0; col < rowSize; col++)
{
// Printing values from array
cout << setw(4) << twoDArray[row][col] << '
';
}
cout << endl;
}
}
// Accepts an integer as a parameter and returns its first
location in the array
// Pre: twoDArray contains integers with row and column size of
NUM_COLS
// Must have constant integer NUM_COLS declared
// rowSize must be less than or equal to NUM_COLS
// numGuessed contains valid integer
// Post: Return true if numGuessed is in array as well as set
rowLoc and colLoc coordinates
// Return false if numGuessed not found and set rowLoc and colLoc
to -1
bool FindNumber(/* IN */ const int twoDArray[][NUM_COLS], /* IN */
int rowSize,
/* IN */ int numGuessed, /* OUT */ int &rowLoc, /* OUT */ int
&colLoc)
{
// Loop to iterate over rows
for(int i=0;i<rowSize;i++)
{
// Loop to iterate over rows
for(int j=0;j<rowSize;j++)
{
// If element at row i and column j is equal to numGuessed
if(twoDArray[i][j]==numGuessed)
{
// Assigning the row and column values of the numGuessed to rowLoc
and colLoc
rowLoc = i;
colLoc = j;
// Returning true
return true;
}
}
}
// If the whole array is traversed and numGuessed is not found,
false is returned
return false;
}
int main()
{
// Defining an 2D Array of size NUM_ROWS x NUM_COLS
int twoDArray[NUM_ROWS][NUM_COLS];
// Initialising the array
InitArray(twoDArray, NUM_ROWS);
// This variable is used to keep track of whether the number is
found or not
bool found;
// Start of do while loop
do
{
// Defining the variable to store the number entered by the
user
int guessNum;
// Prompting the user to enter the guessed number
cout << "Guess a number:";
// Reading the number
cin >> guessNum;
// Creating two variables to store the row and column indexes of
guessNum in twoDArray
int rolLoc = -1;
int colLoc = -1;
// Calling the function FindNumber and storing the result in the
variable found
found = FindNumber(twoDArray, NUM_COLS, guessNum, rolLoc,
colLoc);
// If guessNum is found in twoDarray
if(found)
{
// Print the row and column indexes
cout <<rolLoc << " " << colLoc <<
endl;
}
// Else
else
{
// Print a message to user stating that the guessNum is not found
in the twoDArray
cout << guessNum << " is not one of the numbers!"
<< endl;
}
// End of do while loop
} while(!found);
// Printing the array
PrintArray(twoDArray, NUM_COLS);
return 0;
}
Assignment details:
Pick a number between 1 and 100: 75 Number not found! Pick a number between 1 and 100: 45 Guess location (x y): 5 5 x is correct! and y too high! Guess location (x y): 5 4 x is correct! and y too high! Guess location (x y): 5 3 x is correct! and y too high! Guess location (x y): 5 2 x is correct! and y too high! Guess location (x y): 5 1 Found!
Submit: Documented source code in a zip file
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
const int NUM_ROWS = 10;
const int NUM_COLS = 10;
// Setting values in a 10 by 10 array of random integers (1 -
100)
// Pre: twoDArray has been declared with row and column size of
NUM_COLS
// Must have constant integer NUM_COLS declared
// rowSize must be less than or equal to NUM_COLS
// Post: All twoDArray values set to integers 1 - 100
void InitArray(/* OUT */ int twoDArray[][NUM_COLS], /* IN */ int
rowSize);
// Display the array
// Pre: twoDArray contains integers with row and column size of
NUM_COLS
// Must have constant integer NUM_COLS declared
// rowSize must be less than or equal to NUM_COLS
// Post: Display all values in array in pretty format
void PrintArray(/* IN */ const int twoDArray[][NUM_COLS], /* IN */
int rowSize);
// Accepts an integer as a parameter and returns its first
location in the array
// Pre: twoDArray contains integers with row and column size of
NUM_COLS
// Must have constant integer NUM_COLS declared
// rowSize must be less than or equal to NUM_COLS
// numGuessed contains valid integer
// Post: Return true if numGuessed is in array as well as set
rowLoc and colLoc coordinates
// Return false if numGuessed not found and set rowLoc and colLoc
to -1
bool FindNumber(/* IN */ const int twoDArray[][NUM_COLS], /* IN */
int rowSize,
/* IN */ int numGuessed, /* OUT */ int &rowLoc, /* OUT */ int
&colLoc);
//Setting values in a 10 by 10 array of random integers (1 -
100)
// Pre: twoDArray has been declared with row and column size of
NUM_COLS
// Must have constant integer NUM_COLS declared
// rowSize must be less than or equal to NUM_COLS
// Post: All twoDArray values set to integers 1 - 100
void InitArray(/* OUT */ int twoDArray[][NUM_COLS], /* IN */ int
rowSize)
{
cout << "init\n";
// Loop through each row
for (int row = 0; row < rowSize; row++)
{
// Loop through each column
for (int col = 0; col < rowSize; col++)
{
// printing values from array
twoDArray[row][col] = rand() % 100 + 1;
cout << twoDArray[row][col] << ' ';
}
cout<<endl;
}
}
void PrintArray(/* OUT */ const int twoDArray[][NUM_COLS], /* IN
*/ int rowSize)
{
cout << "print\n";
// Loop through each row
for (int row = 0; row < rowSize; row++)
{
// Loop through each column
for (int col = 0; col < rowSize; col++)
{
// Printing values from array
cout << setw(4) << twoDArray[row][col] << '
';
}
cout << endl;
}
}
// Accepts an integer as a parameter and returns its first
location in the array
// Pre: twoDArray contains integers with row and column size of
NUM_COLS
// Must have constant integer NUM_COLS declared
// rowSize must be less than or equal to NUM_COLS
// numGuessed contains valid integer
// Post: Return true if numGuessed is in array as well as set
rowLoc and colLoc coordinates
// Return false if numGuessed not found and set rowLoc and colLoc
to -1
bool FindNumber(/* IN */ const int twoDArray[][NUM_COLS], /* IN */
int rowSize,
/* IN */ int numGuessed, /* OUT */ int &rowLoc, /* OUT */ int
&colLoc)
{
// Loop to iterate over rows
for(int i=0;i<rowSize;i++)
{
// Loop to iterate over rows
for(int j=0;j<rowSize;j++)
{
// If element at row i and column j is equal to numGuessed
if(twoDArray[i][j]==numGuessed)
{
// Assigning the row and column values of the numGuessed to rowLoc
and colLoc
rowLoc = i;
colLoc = j;
// Returning true
return true;
}
}
}
// If the whole array is traversed and numGuessed is not found,
false is returned
return false;
}
int main()
{
// Defining an 2D Array of size NUM_ROWS x NUM_COLS
int twoDArray[NUM_ROWS][NUM_COLS];
// Initialising the array
InitArray(twoDArray, NUM_ROWS);
// This variable is used to keep track of whether the number is
found or not
bool found;
// Start of do while loop
do
{
// Defining the variable to store the number entered by the
user
int guessNum;
// Prompting the user to enter the guessed number
//cout << "Guess a number:";
cout << "Pick a number between 1 and 100: ";
// Reading the number
cin >> guessNum;
// Creating two variables to store the row and column indexes of
guessNum in twoDArray
int rolLoc = -1;
int colLoc = -1;
// Calling the function FindNumber and storing the result in the
variable found
found = FindNumber(twoDArray, NUM_COLS, guessNum, rolLoc,
colLoc);
// If guessNum is found in twoDarray
if(found)
{
// Print the row and column indexes
cout <<rolLoc << " " << colLoc << endl;
//
// ***************** logic update starts
********************************
int rolLocGuess;
int colLocGuess;
bool xFound = false;
bool yFound = false;
bool bothFound = false;
do{
//
cout <<endl<<"Guess location (x y): ";
cin>>rolLocGuess>>colLocGuess;
//
// logical operators returning boolean
xFound = rolLocGuess==rolLoc; // logical EQUIVALENT
yFound = colLocGuess==colLoc; // logical EQUIVALENT
bothFound = xFound&yFound; // logical AND
//
// only x is correct
if(xFound && !yFound){
cout<<endl<<"x is correct! and ";
if(colLocGuess>colLoc){
cout<<"y is too high!";
}else{
cout<<"y is too low!";
}
//
// only y is correct
}else if(!xFound && yFound){
cout<<endl<<"y is correct! and ";
if(rolLocGuess>rolLoc){
cout<<"x is too high!";
}else{
cout<<"x is too low!";
}
//
// both x and y are wrong
}else{
if(rolLocGuess>rolLoc){
cout<<endl<<"x is too high! and ";
}else if(rolLocGuess<rolLoc){
cout<<endl<<"x is too low! and ";
}
if(colLocGuess>colLoc){
cout<<"y is too high!";
}else if(colLocGuess<colLoc){
cout<<"y is too low!";
}
}
//
}while(!bothFound);
cout<<endl<<"Found!"<<endl; // finally, both x
and y are correct
}
// ***************** logic update ends
************************************
//
// Else
else
{
// Print a message to user stating that the guessNum is not found
in the twoDArray
//cout << guessNum << " is not one of the numbers!"
<< endl;
cout <<endl<<"Number not found!" << endl;
}
// End of do while loop
} while(!found);
// Printing the array
PrintArray(twoDArray, NUM_COLS);
return 0;
}
------------------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERY RELATED TO THIS ANSWER,
IF YOU'RE SATISFIED, GIVE A THUMBS UP
~yc~