// Enter your name as a comment for program identification // Program assignment Prize.cpp // Enter your class section, and time /* The program Prize.cpp simulates a radio station that asks the caller to guess a number. Using a binary search, the number is compared against an ordered list of numbers between 1 and 10000 inclusive. The contest is held until a number has been matched or a value of -1 is entered. A message is displayed containing the winning number, the location in the list of numbers, the number of calls made, and the amount of the prize. Additionally, the number of comparisons made to search the list will be displayed. */ /* An input file PrizeList.txt is used to enter the data. */ /* A message is displayed containing the winning number, the location in the list of numbers, the number of calls made, and the amount of the prize. */ //header files /* use the correct preprocessor directives for input/output and file stream */ #include #include #include #include using namespace std; // function prototypes /* The function instruct describes the use and purpose of the program. */ void instruct(); /* The function openFile opens the data file */ void openFile(ifstream &); /* The function buildList reads the file and assigns the values to an array. */ void buildList(ifstream &, int[], string[], int); // main function int main() { // declare variables /* an ifstream variable inData to read in the list of numbers, a constant integer listSize initialized 20, an integer array prizeNumbers of size listSize, an integer guess intialized to 1 to hold the user's guess, an integer variable numberGuesses initialized to 0 to count the number of guesses made, an integer variable comparisons to count the number of comparisons to find the number in the list, and a Boolean variable found initialized to false to determine if the number is found on the list. */ ifstream inData; const int listSize = 500; int prizeNumbers[listSize]; string prizeTypes[listSize]; // call the function instruct instruct(); // call the function openFile openFile(inData); // call the function buildList buildList(inData, prizeNumbers, prizeTypes, listSize); // close the input data file inData.close(); // part 1, create a function to perform a selection sort. // you should pass in both the prizeNumbers and prizeTypes arrays // after returning from the sort, display the parallel arrays to show they are sorted // part 2, implement the radio call in show simulation // in a loop, ask the caller to guess a number, // then, call a binary search routine to find out if that number exists in the prizeNumbers array // if found, the caller has won a prize and you are done // if not found, take the next caller and ask for their guess system("pause"); return 0; } /* The function instruct describes the use and purpose of the program. */ void instruct() { cout << "This program simulates a radio station " << "that asks the caller to guess a number.\n" << "The number is compared using a binary search " << "against an ordered list of 20\nnumbers between " << "1 and 500 inclusive.\nThe contest is held " << "until a number has been matched or a value of " << "-1 is entered.\nA message is displayed " << "containing the winning number, the location " << "in the list\nof numbers, the number of calls " << "made, the number of comparisons, and the amount " << "of the prize.\n\n"; } /* The function openFile opens the data file */ void openFile(ifstream &inData) { // open the file inData.open("PrizeList.txt"); // check to see if the file opened if (!inData) { cout <<"Cannot open input file. " <<"The program terminates." << endl; system("pause"); exit(1); } } // call the function buildList void buildList(ifstream &inData, int prizeNumbers[], string prizeTypes[], int listSize) { /* loop until all values in the list have been read */ for (int count = 0; count < listSize; count++) { inData >> prizeNumbers[count]; getline(inData, prizeTypes[count]); } }