Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

All the code needed is given below. You are to write a program in C++ to play a

ID: 3678647 • Letter: A

Question

All the code needed is given below.

You are to write a program in C++ to play a guessing game. In this program, you'll need 2 arrays (one array of ints of size 30 in main, one array of ints of size 10 in the game function) and you'll write to a text file (declare the file

variable in main).
In main, initialize the current index to 0, try to open an input file USING THE openInputFile function given in the HW6_CodeFile. If it doesn't open, display an error message and end the program. If it opens, display info for the user

(see test runs), then in a while loop:
** while the current index < 30 AND reading an int from the input file is successful...
assign the input int into the current location in the main array (element at the current index)
call a function (that you write) to play the guessing game (one game) (see details in A. below)
increment the current index
repeat to **

After the main loop, close the input file and call the selectionSort function EXACTLY AS IT'S WRITTEN IN THE TEXTBOOK (you're NOT allowed to change it) so it sorts the main array. Then call another function to write the array to a text

file, and you MUST pass the current index (indicating how many elements were assigned) as well as the main array (see details in C. below).
You MUST declare as external consts 100 and 30 and 10, and use them in the program (NEVER USE ANY LITERAL IN YOUR PROGRAM OTHER THAN 0 OR 1, other than in the const declarations!
Function details:
A. In the function to play one guessing game, you MUST have an int "value" parameter for the answer number (will be the current main number from main, but this function doesn't need to "know" that). Inside the function declare an array

of 10 ints. You'll be counting how many in-range and non-duplicate guesses the user made, so you'll need to declare a local counter and initialize it to 0. Be sure to do the following:
***call a function to have the user guess a number (must not be guessed already) (see B. below) The guess will be in the guessArray, but you are to also assign it to a local int.
check if the guess equals the game number (int parameter), is higher than the game number, or lower than the game number (display a message to the screen indicating the results of the comparison)
repeat to *** if the user didn't guess the game number and the counter (of how many guesses) < 10 (remember to use the const!)
After the game loop, if the user guessed the correct answer (parameter), display a message like "You guessed it in " then how many guesses the user made by displaying the local counter, and also display the answer. If the user didn't

guess the correct answer , display a message like "Sorry, you ran out of guesses" and display the correct answer.
B. This function is to get the user's guess and RETURN IT IN A RETURN STATEMENT, but you'll be updating the guessArray and the counter (so it MUST be a "reference parameter") AND you must do the following:
Prompt the user and read the user's guess.
If the guess < 1 or > 100 (YOU MUST USE THE CONST HERE), display an error message.
If the guess is 1-100 and if the user already guessed that number (CALLING THE SEQUENTIAL SEARCH EXACTLY AS WRITTEN IN THE TEXTBOOK), display a message that it was already guessed.
Loop back to the prompt and read if there was an input error (out of range or already guessed)
Set the guessArray element at the index of the counter to the user's guess
Increment the counter
Return the user's guess in a return statement
C. In the function to write to an output file, the main mainArray which MUST be a parameter ALONG WITH how many elements were assigned (current index). Call the openOutputFile function (given in the HW6_CodeFile). If it opens, write the

array, then close the file. See the test runs for how to display it (labels and format). You MUST use a for loop which iterates ONLY for how many elements were assigned to the array.
DO NOT USE ANY EXTERNAL (global) VARIABLES! (External const is OK, they're not variables). Include in your programs: (ID FUNCTION IS OPTIONAL THIS TIME!) functions (one per "paragraph" described above AND THE LINEAR SEARCH + SELECTION

SORT FUNCTION EXACTLY AS SHOWN IN THE TEXTBOOK called searchList , same as in the HW6_CodeFile)! (DON'T CHANGE, KEEP THE SAME NAME OF THE FUNCTIONs, ITS PARAMETERS AND LOCAL VARIABLES!)
output file and function for opening an output file (see HW6_CodeFile)
input file and function for opening an input file (see HW6_CodeFile) arrays of ints (NEVER ALLOW THE PROGRAM TO GO OUTSIDE THE ARRAY)
NO LITERALS in the executable code (except 0 or 1)
Comments (program header & variable) and indentation as usual

**********************************************************************************************************************
**********************************************************************************************************************
HW#6 Code File

USE THE FOLLOWING FUNCTIONS. DO NOT CHANGE ANY OF THESE FUNCTIONS!!! You will just CALL them in your program!

//USE THIS LINEAR SEARCH FUNCTION (from Chapter 8, program 8-1)
int searchList(const 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
}

//USE THIS SELECTION SORT FUNCTION (from Chapter 8, program 8-5)
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;
   }
}

//USE THE FOLLOWING for opening the input file:
bool openInputFile(ifstream &infile)
{
string filename;

cout << "Enter the input filename: ";
getline(cin, filename);

// IF USING A MAC, ADD CODE SHOWN BELOW***

infile.open(filename.c_str());
return infile.is_open();
}

/***ADD THIS CODE IF USING A MAC because the Mac will include

if( isspace(filename[filename.length()-1]) )
  filename.pop_back();

NOTE: you'll need to #include <cctype> to use isspace
*/

//USE THE FOLLOWING for opening the output file:
bool openOutputFile(ofstream &outfile)
{
    string filename;

    cout<<"Enter output filename: ";
    getline(cin, filename);

   // IF USING A MAC, ADD CODE SHOWN ABOVE***

    outfile.open(filename.c_str());
    if( outfile.fail() ){
        cout<<"Unable to open output file ";
        return false;
    }
    return true;
} // end openOutputFile


// OPTIONAL, BUT WOULD BE GOOD TO USE IN THE USER INPUT FUNCTION:
int getInteger(string prompt,
  int minInt,
  int maxInt)
{
int localInt;

cout << prompt ;
cin >> localInt;
while( localInt < minInt || localInt > maxInt )
{
  cout << "Invalid input, must be >= " << minInt
   << " and <= " << maxInt << endl;
  cout << prompt;
  cin >> localInt;
} // end while
return localInt;
} // end getInteger


*********************************************************************************************************************
*********************************************************************************************************************
This is the code from the book (Must not be change)

//**************************************************************
// Definition of function selectionSort.                       *
// This function performs an ascending order selection sort on *
// array. size is the number of elements in the array.         *
//**************************************************************

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;
   }
}


*******************************************************************************************************************
*******************************************************************************************************************
TESTINPUT file

19 83 57 28


******************************************************************************************************************
******************************************************************************************************************
TESTOUTPUT file

The Guessing Game Numbers in Sorted Order:
   19
   28
   57
   83

*******************************************************************************************************************
*******************************************************************************************************************

program example


You are playing a guessing game and will be guessing several
numbers from a file until the input file is empty. For each number,
you will have up to 10 guesses to guess a number between 1 and 100
Enter the input filename: HW6 Test Input1.txt
Enter your guess: 50
That's too high
Enter your guess: 25
That's too high
Enter your guess: 12
That's too low
Enter your guess: 18
That's too low
Enter your guess: 19
You guessed 19 in 5 guesses!
Congratulations!
Enter your guess: 50
That's too low
Enter your guess: 101
Out of range! Guess must be >= 1 and <= 100
Enter your guess: 100
That's too high
Enter your guess: 90
That's too high
Enter your guess: 100
You already guessed that!
Enter your guess: 50
You already guessed that!
Enter your guess: 102
Out of range! Guess must be >= 1 and <= 100
Enter your guess: 0
Out of range! Guess must be >= 1 and <= 100
Enter your guess: 1
That's too low
Enter your guess: 89
That's too high
Enter your guess: 88
That's too high
Enter your guess: 87
That's too high
Enter your guess: 86
That's too high
Enter your guess: 85
That's too high
Enter your guess: 84
That's too high
Sorry, you ran out of guesses :( , the answer was: 83
Enter your guess: 50
That's too low
Enter your guess: 0
Out of range! Guess must be >= 1 and <= 100
Enter your guess: -1
Out of range! Guess must be >= 1 and <= 100
Enter your guess: 100
That's too high
Enter your guess: 75
That's too high
Enter your guess: 102
Out of range! Guess must be >= 1 and <= 100
Enter your guess: 75
You already guessed that!
Enter your guess: 100
You already guessed that!
Enter your guess: 50
You already guessed that!
Enter your guess: 101
Out of range! Guess must be >= 1 and <= 100
Enter your guess: 67
That's too high
Enter your guess: 61
That's too high
Enter your guess: 57
You guessed 57 in 6 guesses!
Congratulations!
Enter your guess: 50
That's too high
Enter your guess: 0
Out of range! Guess must be >= 1 and <= 100
Enter your guess: -1
Out of range! Guess must be >= 1 and <= 100
Enter your guess: 1
That's too low
Enter your guess: 2
That's too low
Enter your guess: 3
That's too low
Enter your guess: 4
That's too low
Enter your guess: 5
That's too low
Enter your guess: 6
That's too low
Enter your guess: 7
That's too low
Enter your guess: 8
That's too low
Enter your guess: 1
You already guessed that!
Enter your guess: 2
You already guessed that!
Enter your guess: 3
You already guessed that!
Enter your guess: 4
You already guessed that!
Enter your guess: 5
You already guessed that!
Enter your guess: 0
Out of range! Guess must be >= 1 and <= 100
Enter your guess: 3
You already guessed that!
Enter your guess: 4
You already guessed that!
Enter your guess: -1
Out of range! Guess must be >= 1 and <= 100
Enter your guess: 0
Out of range! Guess must be >= 1 and <= 100
Enter your guess: 1
You already guessed that!
Enter your guess: -3
Out of range! Guess must be >= 1 and <= 100
Enter your guess: 3
You already guessed that!
Enter your guess: 10
That's too low
Sorry, you ran out of guesses :( , the answer was: 28
No more number to play or maximum number of numbers reached
Enter output filename: HW6 Test Output1.txt
Process returned 0 (0x0) execution time : 278.137 s
Press any key to continue.

****************************************************************************************************************
*****************************************************************************************************************

Explanation / Answer

public class GuessingGame
{
public static void main(String[] args)
{

     Random generator = new Random();
     int numberToGuess = 1 + generator.nextInt(100);
     int numberOfTries = 0;

     Scanner input = new Scanner (System.in);
     int guess;
     boolean win = false;


     System.out.println("You have 10 tries to guess a number between 1 and 100");

     while(!win && numberOfTries < 10)
     {
     System.out.println("Guess a number: ");
     guess = input.nextInt();

     if (guess < 1 || guess > 100)
     {
     System.out.println("Guess is out of range!");
     continue;
     }


     if (guess == numberToGuess)
     {
         win=true;
     }

     else if (guess < numberToGuess)
             {
             System.out.println("Your guess is too low");
             }
     else if (guess > numberToGuess)
             {
             System.out.println("Your guess is too high");
             }

     }

     System.out.println("You win!");
     System.out.println("The number was " + numberToGuess);
     System.out.println("It took you " + numberOfTries + " tries." );


}
}