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

I need c++ code written for the following assignment. Write a program that gener

ID: 664196 • Letter: I

Question

I need c++ code written for the following assignment.

Write a program that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program must use a loop that repeats until the user correctly guesses the random number or has made 10 guesses. The program needs to keep track of the number of guesses the user makes. At the end the program will display one of the messages in the table below based on the number of guesses the user took. The program must validate the user’s guess by making sure the value entered is between 1 and 100. If the value is not between 1 and 100, the user should be told to make another guess. The invalid input should not count as one of the 10 guesses the user is allowed.

Number of Guesses by the User

Output Message

Less than 5 guesses

"Either you know the secret or you got lucky!"

5-7 guesses

"You're pretty good at this!"

8-10 guesses

"You'll do better next time."

If guess 10 is not correct

"Sorry - You have taken too many guesses."

Assignment 2: Working with files – You will need the text file Random.txt that can be found in the Content area of the course under Week 5. The file contains a list of random numbers. Write a program that opens the file and reads all the numbers from the file, and calculate/determine the following:

Counts how many numbers were read from the file

The sum of all the numbers in the file (a running total)

The average of all the numbers in the file

Determine the lowest and highest values in the file

The program must validate that the file was opened before reading from the file.

If file does not exist display a message telling the user that the file could not be opened and exit the program.

Number of Guesses by the User

Output Message

Less than 5 guesses

"Either you know the secret or you got lucky!"

5-7 guesses

"You're pretty good at this!"

8-10 guesses

"You'll do better next time."

If guess 10 is not correct

"Sorry - You have taken too many guesses."

Explanation / Answer

Assignment 1

#include<iostream>
#include<cstdlib>
using namespace std;

int generateRandomNumber(){
    int min=1,max=100;
    return (rand()%(max-min))+min;
}

int main(){
    int count=1,guess;
    bool correctGuess=false;
    int num=generateRandomNumber();
    while(count<=10){
        cout<<" Enter your guess: ";
        cin>>guess;
        if(guess<1||guess>100)
        {
            cout<<" Wrong input";
            continue;
        }
        if(guess>num)
            cout<<" Too high, try again";
        else if(guess<num)
            cout<<" Too low, try again";
        else{
            correctGuess=true;
            break;
        }
        count++;
    }
    if(!correctGuess&&count==10)
        cout<<" Sorry - You have taken too many guesses.";
    else if(count>=8 && count<=10)
        cout<<" You'll do better next time.";
    else if(count>=5&&count<=7)
        cout<<" You're pretty good at this!";
    else
        cout<<"Either you know the secret or you got lucky!";
}

Assignment 2

#include<iostream>
#include<fstream>
#include<iomanip>

using namespace std;

int main(){
    ifstream myfile("d:\Random.txt");
    int num,count=0,sum=0,lowest=0,highest=0;
    double avg=0.0;
    string line;
    if (myfile.is_open())
    {
        while ( !myfile.eof() )
        {
            count+=1;
            myfile>>num;
            sum+=num;
            if(count==1)
                lowest=num;
            else if(num<lowest)
                lowest=num;
            if(num>highest)
                highest=num;
        }
        avg=sum/count;
        cout<<"Total numbers read from file:"<<count;
        cout<<" Sum of all the number:"<<sum;
        cout<<" Average of all the numbers:"<<setprecision(6)<<avg;
        cout<<" Lowest number:"<<lowest;
        cout<<" Highest number:"<<highest;
        myfile.close();
    }
    else cout << "Unable to open file";
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote