Question

In: Computer Science

The source code I have is what i'm trying to fix for the assignment at the...

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:

  • Change the logic so that after user has chosen a number, and the program has verified that it's in the array, the program goes into a loop and lets the user guess the number's coordinates, returning a message stating higher or lower for the x and y coordinates, until they guess the number's location. A program run might look something like
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

Solutions

Expert Solution

#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~


Related Solutions

I'm getting an error message with this code and I don't know how to fix it...
I'm getting an error message with this code and I don't know how to fix it The ones highlighted give me error message both having to deal Scanner input string being converted to an int. I tried changing the input variable to inputText because the user will input a number and not a character or any words. So what can I do to deal with this import java.util.Scanner; public class Project4 { /** * @param args the command line arguments...
Hello I have this error in the code, I do not know how to fix it....
Hello I have this error in the code, I do not know how to fix it. It is written in C++ using a Eclipse IDE Error: libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string bus.h =========== #pragma once #include using namespace std; class Bus { private:    string BusId; // bus ID    string Manufacturer; // manufacturer of the bus    int BusCapacity; // bus capacity    int Mileage; // mileage of bus    char Status; // current status...
I already have the code of this program, I just want to know how to fix...
I already have the code of this program, I just want to know how to fix the code to Implement the 5th function (System.nanotime() and System.currentTimeMillis() methods) What Code does: Introduction Searching is a fundamental operation of computer applications and can be performed using either the inefficient linear search algorithm with a time complexity of O (n) or by using the more efficient binary search algorithm with a time complexity of O (log n). Task Requirements In this lab, you...
I have a lab assignment that I'm not sure how to do. The experiment is a...
I have a lab assignment that I'm not sure how to do. The experiment is a cart moving 60cm distance and there is a fan on top of it making it have a mass of .56kg. Every trial there is 100g added to the cart. For this part, the time is kept the same. 1. If the force provided by the fan was the same for each run and we have chosen the same time interval, how does the impulse...
I'm in a computer science course and I have an assignment that asks me to implement...
I'm in a computer science course and I have an assignment that asks me to implement a CPU scheduler in Java. Here are some details for the task: The program should ask for the number of processes. Then the program will ask for burst and arrival times for each process. Using that information, the program should schedule the processes using the following scheduling algorithms: First Come First Serve (FCFS) Shortest Jobs First (SJF) Shortest Remaining Time First (SRTF) The output...
I'm new in MATLAB and I have to write a code in MATLAB which converts a...
I'm new in MATLAB and I have to write a code in MATLAB which converts a number from one base to another without using base2base, etc
I'm having trouble understanding a CS assignment. I would appreciate it if you all code do...
I'm having trouble understanding a CS assignment. I would appreciate it if you all code do this for me. The template for the lab is below which you must use. You're only supposed to create/edit the product function. The assignment has to be written in asm(Mips) You will need to create a NOS table and use the structure below and write a function called product. The structure used in this program is: struct Values { short left; short right; int...
I have to code the assignment below. I cannot get the program to work and I...
I have to code the assignment below. I cannot get the program to work and I am not sure what i am missing to get the code to work past the input of the two numbers from the user. My code is listed under the assignment details. Please help! Write a Java program that displays the prime numbers between A and B. Inputs: Prompt the user for the values A and B, which should be integers with B greater than...
Hi, I'm trying to rewrite the code below (code #1) by changing delay() to millis(). void...
Hi, I'm trying to rewrite the code below (code #1) by changing delay() to millis(). void loop() { // Print the value inside of myBPM. Serial.begin(9600); int myBPM = pulseSensor.getBeatsPerMinute(); // Calls function on our pulseSensor object that returns BPM as an "int". // "myBPM" hold this BPM value now. if (pulseSensor.sawStartOfBeat()) { // Constantly test to see if "a beat happened". Serial.println("♥ A HeartBeat Happened ! "); // If test is "true", print a message "a heartbeat happened". Serial.print("BPM:...
***The code is provided below*** When trying to compile the code below, I'm receiving three errors....
***The code is provided below*** When trying to compile the code below, I'm receiving three errors. Can I get some assistance on correcting the issues? I removed the code because I thought I corrected my problem. I used #define to get rid of the CRT errors, and included an int at main(). The code compiles but still does not run properly. When entering the insertion prompt for the call details, after entering the phone number, the program just continuously runs,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT