DESIGN THIS FIRST --- Design Hierarchy chart, pseudocode for main, module specs
ID: 3851027 • Letter: D
Question
DESIGN THIS FIRST --- Design Hierarchy chart, pseudocode for main, module specs for EACH function
Write both C++ and pseudocode separately!
Write a program in C++ to allow the user to play a one-user simplified Keno game. The program will contain the following:
- A one-dim. array of ints of size 20 for the official Keno game numbers (like a lottery game)
- A one-dim. array of ints of size 15 for the user's numbers
- (OPTIONAL: A one-dim. array of ints of size 15 for the matched numbers) You must allow the user to play more than one game using a loop. It's up to you how to ask the user to play again and how to repeat if you try this option. In main:
Open the input file and output file in main to allow reading more than one game from the input file (even if you don't use the option to play more than one game). DO NOT CHANGE ANYTHING GIVEN BELOW
CALL the openInputFile function:
If the file doesn't open, display an error message and return -1 from main:
If the file doesn't open, display an error message and return -2 from main:
Then in main, call each of the following (EACH of the following paragraphs MUST be a SEPARATE FUNCTION, except the selectionSort function is the same one CALLED 2 TIMES)
o **Call a function (that you write) to read from the input into the Keno game array (pass as an argument) (see details in A. below)
o If the function (A) returns true, continue into the loop...
o Call a function (that you write) to read into the user's guess array and find how many the user guessed (see details in B. below)
o Sort the Keno game array by calling the SELECTION SORT function (given below).
void selectionSort(int array[], int size)
{
int startScan, minIndex, minValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = array[startScan];
for(int index = startScan + 1; index < size; index++)
{
if (array[index] < minValue)
{
minValue = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
}
o Sort the user's array by calling the SELECTION SORT function
o Call a function (that you write, see C. below) that finds how many numbers the user's guess array matched the Keno game array, and assign its return value to a main int variable (I'll call numMatched)
o Call a function (that you write, see D. below) that writes both arrays and how many matched (must use the format shown in the test runs!)
o Ask the user if he/she wants to try another, and repeat to ** if the user wants to repeat (up to you to determine how)
A. Read from the input file 20 integers into the Keno game array (parameter). However, when reading in the loop, check if the index/counter is inside the array bounds AND YOU MUST STOP READING IF THERE ARE NO MORE NUMBERS IN THE FILE. If the index or counter is 0, return false, otherwise return true.
B. Ask the user and read how many numbers the user wants to guess (minimum 1 and maximum 15), but YOU MUST CALL THE getInteger FUNCTION GIVEN IN BELOW (pass "Enter how many numbers to choose: ", MIN_GUESSES, MAX_GUESSES)! Then read that many numbers from the user into the user's onedim. array of ints ALSO USING THE getInteger FUNCTION GIVEN BELOW for reading one number at a time passing "Enter a number: ", 1 and 80 to the getInteger function, then checking for duplicate numbers (CALL the LINEAR SEARCH function given in the textbook which MUST BE CALLED HERE, not imbedded in the function) before assigning to an array element! Assign how many the user chose to a reference parameter for it OR return it in a return statement. (That means that how many the user chose MUST be a variable in main!)
int getInteger(string prompt,
int minInt,
int maxInt)
{
int inputInt;
cout << prompt;
cin >> inputInt;
while( inputInt < minInt || inputInt > maxInt )
{
cout << "Invalid input: must be >= " << minInt << " and <= "
<< maxInt << endl;
cout << "Enter again: ";
cin >> inputInt;
}
return inputInt;
} // end getInteger
C. For each element in the user's number array, search the Keno game array (call the LINEAR SEARCH function given in BELOW which MUST BE CALLED HERE, not imbedded in the function, and NOT using your own search). If it's found, add 1 to the matched counter (optional: put the number into the matched array parameter). The user's numbers for loop MUST use the variable read in the first function for how many numbers the user guessed (so it must be a parameter). RETURN IN A RETURN STATEMENT, how many numbers in the user's array are in the Keno game array.
int searchList(int list[], int numElems, int value)
{
int index = 0; // Used as a subscript to search array
int position = -1; // To record position of search value
bool found = false; // Flag to indicate if the value was found
while (index < numElems && !found)
{
if (list[index] == value) // If the value is found
{
found = true; // Set the flag
position = index; // Record the value's subscript
}
index++; // Go to the next element
}
return position; // Return the position, or -1
}
D. Display to the screen AND write to the output file (reference parameter) the Keno game numbers, the user's numbers, and the number of matches with a label (see test output file for format). The user's numbers for loop MUST use the variable read in the first function for how many numbers the user guessed. Optional: write the numbers that matched in the matched array.
EACH OF THE ABOVE PARAGRAPHS (A. THROUGH F.) MUST BE A FUNCTION! When calling function A., you MUST assign the return value (or reference parameter) for the how many the user chose to a main variable. In functions D., E., and F., the main variable for how many the user chose MUST be passed as an argument to those functions!
Each of the numbers in bold above MUST be an external const! DO NOT USE ANY EXTERNAL (global) VARIABLES (variables declared outside of main or outside of any function)!! (external const declarations are OK.)
Include in your program:
-Arrays (one-dim. arrays as specified above) (NEVER ALLOW THE PROG. TO OUTSIDE THE ARRAY or POINTS WILL BE DEDUCTED)
- for loops for traversing the array(s), WHICH MUST ITERATE ONLY FOR HOW MANY ELEMENTS ARE USED IN THE ARRAY (so how many used, NOT necessarily the physical size, MUST be a parameter in the functions that traverse the array)
- function that returns a value (indicated above) AND functions (with array parameters as spec. above)
- Error checking specified above - input text file (ALWAYS CHECK IF IT OPENS OK BEFORE READING FROM IT) - output text file (ALWAYS CHECK IF IT OPENS OK BEFORE WRITING TO IT)
- Comments for program header, and variables, as usual
- Comment each function in the source file
HW6-Testrun1 Show BELOW
//USE THE FOLLOWING IN YOUR cpp FILE for opening the input file bool open InputFile (ifstream &ifs;) string filename; cout Enter the input filename: getline (cin, filename) ifs. open filename c str return ifs open() bool openoutput File (ofstream &ifs;) string filename; cout Enter the output filename: getline (cin, filename) ifs. open filename c str return ifs.is open()Explanation / Answer
#include <iostream>
#include <algorithm>
#include <ctime>
#include <windows.h>
using namespace std;
struct coord
{
int X;
int Y;
int number;
bool selected;
coord(){selected = false;}
};
class keno
{
public:
keno();
void display_board();
void display_numbers();
void get_picks();
void computer_draw();
void calc_winnings();
void reset();
void set_console_size(int x, int y);
void gotoxy(int x, int y);
void clear_screen();
private:
int turn_counter;
int picks;
int money;
int hits;
int random_draws[20];
int user_picks[10];
int computer_picks[20];
coord num_coords[80];
};
int main()
{
keno mykeno;
while(true)
{
mykeno.display_board();
mykeno.display_numbers();
mykeno.get_picks();
mykeno.computer_draw();
mykeno.calc_winnings();
mykeno.reset();
}
return 0;
}
keno::keno()
{
turn_counter = 1;
picks = 0;
money = 500;
hits = 0;
srand(time(NULL));
SetConsoleTitle("Keno!");
set_console_size(58,37);
for(int i=0, x=8, y=10; i<80; i++, x+=5)
{
num_coords[i].X = x;
num_coords[i].Y = y;
num_coords[i].number = i+1;
if(x>=53)
{
x=3;
y+=3;
}
}
}
void keno::display_board()
{
cout << " --------------------------------------------------- "
<< " |\ \ "
<< " | \ \ "
<< " | |---------------------------------------------------| ";
for(int i=0; i<31; i++)
{
cout << " | | | ";
}
cout << " \ | | "
<< " \ --------------------------------------------------- ";
gotoxy(8,5); cout << "Game #" << turn_counter;
gotoxy(40,5); cout << "Total: $" << money;
gotoxy(40,35); cout << "$20 per play.";
}
void keno::display_numbers()
{
int randoms[80];
int snooze = 30;
int r=0;
for(int i=0; i<80; i++)
{
randoms[i] = i;
}
for(int i=0; i<160; i++)
{
swap(randoms[rand()%80], randoms[rand()%80]);
}
for(int i=0; i<80; i++)
{
r = randoms[i];
gotoxy(num_coords[r].X, num_coords[r].Y);
cout << num_coords[r].number;
Sleep(snooze);
}
}
void keno::get_picks()
{
int number = 0;
int i=0;
while(picks<10)
{
gotoxy(8,35); cout << " ";
gotoxy(8,35); cout << "Enter number: ";
cin >> number;
number--;
user_picks[i] = number;
gotoxy(num_coords[number].X-1, num_coords[number].Y); cout << '[';
if(number<9)
{
gotoxy(num_coords[number].X+1, num_coords[number].Y);
}
else
{
gotoxy(num_coords[number].X+2, num_coords[number].Y);
}
num_coords[number].selected = true;
cout << ']';
picks++;
gotoxy(40,35); cout << "Pick " << picks << " of 10 ";
}
gotoxy(8,35); cout << " ";
}
void keno::computer_draw()
{
int randoms[80];
int snooze = 30;
int cp = 0;
money -= 20;
gotoxy(8,7); cout << "Draw: ";
gotoxy(40,7); cout << "Hits: ";
for(int i=0; i<80; i++)
{
randoms[i] = i;
}
for(int i=0; i<160; i++)
{
swap(randoms[rand()%80], randoms[rand()%80]);
}
for(int i=0; i<20; i++)
{
computer_picks[i] = randoms[i];
}
for(int i=0; i<20; i++)
{
cp = computer_picks[i];
if(num_coords[cp].selected)
{
hits++;
gotoxy(num_coords[cp].X-1, num_coords[cp].Y); cout << "HIT!";
gotoxy(18,7); cout << "HIT!";
gotoxy(47,7); cout << hits;
}
else
{
gotoxy(num_coords[cp].X-1, num_coords[cp].Y); cout << '*';
if(cp<9)
{
gotoxy(num_coords[cp].X+1, num_coords[cp].Y); cout << '*';
}
else
{
gotoxy(num_coords[cp].X+2, num_coords[cp].Y); cout << '*';
}
}
gotoxy(14,7); cout << cp+1;
Sleep(1000);
gotoxy(18,7); cout << " ";
}
gotoxy(8,7); cout << " ";
turn_counter++;
}
void keno::calc_winnings()
{
gotoxy(20,6); cout << "You Win ";
switch(hits)
{
case 1: money += 10; cout << "$10!"; break;
case 2: money += 20; cout << "$20!"; break;
case 3: money += 50; cout << "$50!"; break;
case 4: money += 100; cout << "$100!"; break;
case 5: money += 200; cout << "$200!"; break;
case 6: money += 500; cout << "$500!"; break;
case 7: money += 1000; cout << "$1,000!"; break;
case 8: money += 2000; cout << "$2,000!"; break;
case 9: money += 5000; cout << "$5,000!"; break;
case 10: money += 10000; cout << "$10,000!"; break;
default: cout << "Nothing!";
}
cin.ignore();
cin.get();
Sleep(3000);
}
void keno::reset()
{
picks = 0;
hits = 0;
clear_screen();
for(int i=0; i<80; i++)
{
num_coords[i].selected = false;
}
}
void keno::gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void keno::clear_screen()
{
DWORD n;
DWORD size;
COORD coord = {0};
CONSOLE_SCREEN_BUFFER_INFO csbi;
HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
GetConsoleScreenBufferInfo ( h, &csbi );
size = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
GetConsoleScreenBufferInfo ( h, &csbi );
FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );
SetConsoleCursorPosition ( h, coord );
}
void keno::set_console_size(int x, int y)
{
HANDLE hOut;
SMALL_RECT DisplayArea = {0, 0, 0, 0};
COORD c = {0,0};
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DisplayArea.Right = x;
DisplayArea.Bottom = y;
c.X = x+1;
c.Y = y+1;
SetConsoleWindowInfo(hOut, TRUE, &DisplayArea);
SetConsoleScreenBufferSize(hOut, c);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.