Question

Use C++. In this project you will be tasked with writing a program to play a...

Use C++.

In this project you will be tasked with writing a program to play a guessing game between another player and your program. Your program will start by initializing an array to 5 fixed values in the ranges of 1-1000 in the array. These values should also be in order (sorted).

Your program will then begin by asking the player to make a guess from 1-1000. For the first guess of the user, your program will only tell the player if they guessed correctly or not. However, the program will keep track of how close they are to the closest element in the array. The program will then go into a loop, asking the player for a new guess each time. If at any time, their new guess is farther away from the closest number than their previous guess, the program will respond with “Getting colder!”. If they get closer to a correct guess, the program should respond with “Getting warmer!”. This should continue until the player guesses one of the numbers in the list. Your program should then output “Success!” and then end.

Example:

Hello, please try and guess one of my numbers between 1and 1000.

Please make a guess: 75

I am sorry, that is incorrect!

Please make a guess: 80

Getting warmer!

Please make a guess: 85

Getting warmer!

Please make a guess: 90

Getting colder!

Please make a guess: 87

Success!

Your program should contain the following function:

int distanceToClosest(const int solutions[],int SIZE, int guess);

This function should take in a sorted array and find the smallest absolute difference between the parameter guess and any member of the solutions array.

Error conditions: Your program should make sure the guesses are between 1 and 1000. Guesses outside of the valid range should result in an error message. You can assume the responses are integers.

Standards: Make sure to pay attention to proper programming style. Check the programming style supplement before beginning on this project.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//CODE

#include <iostream>

#include <cmath>

using namespace std;

int distanceToClosest(const int solutions[],int SIZE, int guess){

//takes a sorted array and finds the smallest distance between guessed number and all the numbers

//initialize to a high number

int dist = 100000;

for(int i = 0; i < SIZE; i++)

{

//calculate distance of guess with every number

//abs is used to avoid negative and finds absolute distance

int diff = abs(solutions[i] - guess);

if(diff < dist){

//updating the dist

dist = diff;

}

}

return dist;

}

int main(){

cout<<"Hello, please try and guess one of my numbers between 1 and 1000."<<endl;

int compare_dist = 100000;

int arr[5] = {123, 234, 532, 788, 967};

int g;

//input initial guess

cout<<"Please make a guess: ";

cin>>g;

int distance = distanceToClosest(arr,5, g);

//initial comparison, distance will return 0 if the guess matches with any of the numbers

if(distance == 0){

cout<<"Success!"<<endl;

//end the program

return 0;

}

else{

cout<<"I am sorry, that is incorrect!"<<endl;

}

//update the distance

compare_dist = distance;

while(true){

//break out of this loop when user guesses any of the numbers correctly

int guess;

cout<<"Please make a guess: ";

cin>>guess;

//error handling

if(guess < 1 || guess > 1000){

cout<<"Error! Please make a guess between 1 and 1000"<<endl;

continue;

}

//should break when the dist is 0

// should print getting warmer if dist is less than previous dist

// should print getting colder if dist get more than previous dist

int dist = distanceToClosest(arr, 5, guess);

if(dist == 0){

//it means it matched to any of the 5 numbers so distance will return 0

cout<<"Success!"<<endl;

break;

}

else if(dist < compare_dist){

cout<<"Getting warmer!"<<endl;

}

else

cout<<"Getting colder!"<<endl;

//updating the compare distance to the new distance

compare_dist = dist;

}

return 0;

}

//OUTPUT

sameer@sameer-4540s:~/Desktop/HomeworkLib 163x20 sameer@sameer-4540s:~/Desktop/HomeworkLib$ ./guess Hello, please try and guess one of my

Add a comment
Know the answer?
Add Answer to:
Use C++. In this project you will be tasked with writing a program to play a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that allows a user to play a guessing game. Pick a random number...

    Write a program that allows a user to play a guessing game. Pick a random number in between 1 and 100, and then prompt the user for a guess. For their first guess, if it’s not correct, respond to the user that their guess was “hot.” For all subsequent guesses, respond that the user was “hot”if their new guess is strictly closer to the secret number than their old guess and respond with“cold”, otherwise. Continue getting guesses from the user...

  • Please help with this Intro to programming in C assignment! Intro to Programming in C-Large Program...

    Please help with this Intro to programming in C assignment! Intro to Programming in C-Large Program 3 - Hangman Game Assignment purpose: User defined functions, character arrays, c style string member functions Write an interactive program that will allow a user to play the game of Hangman. You will need to: e You will use four character arrays: o one for the word to be guessed (solution) o one for the word in progress (starword) o one for all of...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

  • This is for C programming: You will be given files in the following format: n word1...

    This is for C programming: You will be given files in the following format: n word1 word2 word3 word4 The first line of the file will be a single integer value n. The integer n will denote the number of words contained within the file. Use this number to initialize an array. Each word will then appear on the next n lines. You can assume that no word is longer than 30 characters. The game will use these words as...

  • USE JAVA Problem 1: Guess a Number You are writing a program for a number guessing...

    USE JAVA Problem 1: Guess a Number You are writing a program for a number guessing game. For playing "Guess a Number", the program will generate a number for player to guess from within a range specified by the player. The program will take as input the lowest number in the range and the highest number in the range from the player, and then a series of guesses of the form: <n,>n, = n, where n is the number guessed....

  • Please write a C# program that where a player will play a guessing game with the...

    Please write a C# program that where a player will play a guessing game with the range of 1-100. Each time the play guesses a number, and it is not correct the program should indicate if the number is more or less than what the player guessed. The play should be able to guess until the correct number has been guessed. If an invalid number is entered, such as: 1.24 (real number), or asd (string). The program should tell user...

  • For this lab you will write a Java program using a loop that will play a...

    For this lab you will write a Java program using a loop that will play a simple Guess The Number game. Th gener e program will randomly loop where it prompt the user for a ate an integer between 1 and 200 (including both 1 and 200 as possible choices) and will enter a r has guessed the correct number, the program will end with a message indicating how many guesses it took to get the right answer and a...

  • C++ Hangman (game) In this project, you are required to implement a program that can play...

    C++ Hangman (game) In this project, you are required to implement a program that can play the hangman game with the user. The hangman game is described in https://en.wikipedia.org/wiki/Hangman_(game) . Your program should support the following functionality: - Randomly select a word from a dictionary -- this dictionary should be stored in an ASCII text file (the format is up to you). The program then provides the information about the number of letters in this word for the user to...

  • How to play Bulls and Cows between you (the host) and your computer (the player) When...

    How to play Bulls and Cows between you (the host) and your computer (the player) When completed, your program allows you to host the game (i.e., you create a secret word and score every guess) and it makes guesses until it wins. During the development or testing of your program, you may set a condition (e.g., the total count of guesses is already 10) to allow your program to request to reveal the secret word and quit the game. In...

  • Use visual studio C# Tasks You are going to build a program that generates a random...

    Use visual studio C# Tasks You are going to build a program that generates a random integer between 1 and 10, then prompts user to make several guesses of the number until user gets the right number - For each guess, tell user if the guess is higher or lower than the number - Handle the potential exception with user guess. Sample Output DAC programPrajects ExameExam1bin,DebugExarm1.exe Welcome! Guess the number I'm thinking of . It is between 1 and 1e...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT