1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
#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 has set integers 1-100
void InitArray(/*OUT*/ int twoDArray[][NUM_COLS], /*IN*/ int rowSize);
//Displays the array & set the int to constant so you cannot change it
//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 of array in proper 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 mus be less than or equal to NUM_COLS
// numGuessed contains valid integer
//Post: Return ture if guessNum is in array as well as set rowLoc and colLoc coordinates
// Return false if guessNum is not found and set rowLoc and colLoc coordinates
bool FindNumber (/*IN*/ const int twoDArray[][NUM_COLS], /*IN*/ int rowSize,
/*IN*/ int guessNum, /*OUT*/ int &rowLoc, /*OUT*/ int &colLoc);
int main(){
int rowLoc = 0;
int colLoc = 0;
int guessNum;
int twoDArray [NUM_ROWS] [NUM_COLS];
int found;
int x, y;
//srand(time(0));
//srand (time(nullptr)); this will get you genuine random numbers.
InitArray(twoDArray, NUM_COLS);
PrintArray(twoDArray, NUM_COLS);
// Do/While loop to loop at least once and then continue to loop until the
// number is found.
do{
//prompt for user to guessNum
cout << "Please guess a number from 1 - 100 only: " << endl;
cin >> guessNum;
found = FindNumber (twoDArray, NUM_ROWS, guessNum, rowLoc, colLoc);
cout << FindNumber(twoDArray, NUM_ROWS, guessNum, rowLoc, colLoc) << endl;
//if/else statement that will check if the number was guessed correctly or not.
if (!found) {
cout << guessNum << "Number not found! " << endl;
} else
cout << "Congratulations, the number you guessed was " << guessNum << endl;
} while (!found );
do{
//Prompt for location (x and y)
cout << "Guess the coordinates of the number by row then column. " << endl;
cin >> x >> y;
//test x against rowLoc and y against colLoc
if (x == rowLoc && y == colLoc){
cout << "Yay, you found the number. The number you guessed was "
<< guessNum << " and was located in row: " << rowLoc
<< " and in column: "<< colLoc << ". You have now graduated to a worm."
<<"Thanks for playing. How about another game, worm? "<< endl;
}else if (x < rowLoc && y < colLoc) {
cout << "You suck!!" << "x is lower! and y is lower!"
<< "Try again maggot!"<< endl;
} else if (x > rowLoc && y > colLoc){
cout << "You suck!!" << "x is higher! and y is higher!"
<< "Try again maggot!"<< endl;
} else if (x < rowLoc && y > colLoc){
cout << "You suck!!" << "x is lower! and y is higher!"
<< "Try again maggot!"<< endl;
} else if (x > rowLoc && y < colLoc){
cout << "You suck!!" << "x is higher! and y is lower!"
<< "Try again maggot!"<< endl;
} else if (x == rowLoc && y > colLoc){
cout << "You suck!!" << "x is correct! and y is higher!"
<< "Try again maggot!"<< endl;
} else if (x == rowLoc && y < colLoc){
cout << "You suck!!" << "x is correct! and y is higher!"
<< "Try again maggot!"<< endl;
} else if (x > rowLoc && y == colLoc){
cout << "You suck!!" << "x is higher! and y is correct!"
<< "Try again maggot!"<< endl;
}else if (x < rowLoc && y == colLoc){
cout << "You suck!!" << "x is lower! and y is correct!"
<< "Try again maggot!"<< endl;
}
} while (!(x == rowLoc && y == colLoc));
//PrintArray(twoDArray, NUM_ROWS);
return 0;
}
void InitArray(/*OUT*/ int twoDArray[][NUM_COLS], /*IN*/ int rowSize) {
//cout <<"init\n";
//Loop through each row
for (int row = 0; row < 10; row++) {
//loop through each column
for (int col = 0; col < 10; col++) {
//Rand generates only one number. because it is within a four loop it generates 100 random numbers.
twoDArray [row][col] = rand() % 100 + 1;
//cout << twoDArray [row][col] << " ";
}
}
}
void PrintArray( /*IN*/ const int twoDArray[][NUM_COLS], /*IN*/ int rowSize) {
//cout << "Print \n";
//Loop through each row
for (int row = 0; row < NUM_ROWS; row++) {
//loop through each column
for (int col = 0; col < NUM_COLS; col++) {
//Rand generates only one number. because it is within a four loop it generates 100 random numbers.
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 mus be less than or equal to NUM_COLS
// numGuessed contains valid integer
//Post: Return true if guessNum is in array as well as set rowLoc and colLoc coordinates
// Return false if guessNum is not found and set rowLoc and colLoc coordinates
bool FindNumber (/*IN*/ const int twoDArray[][NUM_COLS], /*IN*/ int rowSize,
/*IN*/ int guessNum, /*OUT*/ int &rowLoc, /*OUT*/ int &colLoc) {
//loop through loops and columns and return true is found or false
for (int rowThing = 0; rowThing < NUM_ROWS; rowThing++){
//loop through each column
for (int colThing = 0; colThing < NUM_COLS; colThing++)
// check if value in array == numguessed
if (twoDArray [rowThing] [colThing] == guessNum ) {
rowLoc = rowThing +1;
colLoc = colThing +1;
return true;
}else return false;
}
}
|