need fully comments for this program, thanks #include <iostream> #include \"gues
ID: 3783571 • Letter: N
Question
need fully comments for this program, thanks
#include <iostream>
#include "guess.hpp"
using namespace std;
int main() {
Guess guess;
while (true) {
guess.ChooseMN();
while (true) {
bool flag = guess.UserChoose();
if (flag)
break;
}
cout << "Play again (Y/N)? ";
char ch;
cin >> ch;
if (ch == 'n' || ch == 'N')
break;
}
cout << "Good Bye!" << endl;
return 0;
}
#include <cstdlib>
#include <iostream>
#include "guess.hpp"
#include <time.h>
using namespace std;
void Guess::ChooseMN()
{
cout << "Enter range of the value: ";
cin >> m;
cout << "Enter the number you want to guess ? ";
cin >> n;
guess = new int[n];
foundGuess();
}
bool Guess::UserChoose()
{
int *temp = new int[n];
cout << "Enter your guesses for the " << n << " integers in the range from 1 to " << m << " that have been selected:" << endl;
for (int i = 0; i<n; i++)
{
cin >> temp[i];
}
bool *flag = new bool[n];
for (int i = 0; i<n; i++)
flag[i] = false;
int count = 0;
for (int i = 0; i<n; i++)
{
for (int j = 0; j<n; j++)
{
if (flag[j] == false && guess[i] == temp[j])
{
flag[j] = true;
count++;
break;
}
}
}
delete[] temp;
delete[] flag;
if (count == n)
{
cout << "You are correct! Guess again." << endl;
delete guess;
return true;
}
else
{
cout << count << " of your guesses are correct." << endl;
return false;
}
}
int Guess::RandomGuess()
{
return rand() % m + 1;
}
void Guess::foundGuess()
{
for (int i = 0; i<n; i++)
{
guess[i] = RandomGuess();
}
}
#ifndef guess_hpp
#define guess_hpp
#include <cstdlib>
class Guess {
public:
Guess()
{
n = 0;
m = 0;
}
void ChooseMN();
bool UserChoose();
private:
int n, m;
int *guess;
int RandomGuess();
void foundGuess();
};
#endif /* guess_hpp */
Explanation / Answer
I tried to comment the necessary steps, please have a look at it.
#include <iostream>
#include "guess.hpp"
using namespace std;
int main() {
Guess guess;
while (true) {
guess.ChooseMN();
while (true) {
//the below line will prompt user for choosing the flag value until user choose a true value
bool flag = guess.UserChoose();
//it breaks out from the loop whenever user enters the flag value as true
if (flag)
break;
}//end of inner while loop
//the below segments of line prompting user whether it wants to play or not
cout << "Play again (Y/N)? ";
char ch;
cin >> ch;
if (ch == 'n' || ch == 'N')
break;
}//end of outer while loop
cout << "Good Bye!" << endl;
return 0;
}//end of main function
#include <cstdlib>
#include <iostream>
#include "guess.hpp"
#include <time.h>
using namespace std;
void Guess::ChooseMN()
{
cout << "Enter range of the value: ";
cin >> m;
cout << "Enter the number you want to guess ? ";
cin >> n;
//defining a new dynamic array
guess = new int[n];
foundGuess(); //calling function
}
bool Guess::UserChoose()
{
int *temp = new int[n];
cout << "Enter your guesses for the " << n << " integers in the range from 1 to " << m << " that have been selected:" << endl;
for (int i = 0; i<n; i++)
{
cin >> temp[i];
}
//initializing the boolean dynamic flag array
bool *flag = new bool[n];
for (int i = 0; i<n; i++)
flag[i] = false;
int count = 0;
for (int i = 0; i<n; i++)
{
for (int j = 0; j<n; j++)
{
//whenever the value matches make flag true and come out of loop
if (flag[j] == false && guess[i] == temp[j])
{
flag[j] = true;
count++;
break;
}
}
}//end of outer for loop
//deleting the dynamically created object
delete[] temp;
delete[] flag;
//this segment gives the count of correct no. of guesses
if (count == n)
{
cout << "You are correct! Guess again." << endl;
delete guess;
return true;
}
else
{
cout << count << " of your guesses are correct." << endl;
return false;
}
}
//return randome guess values
int Guess::RandomGuess()
{
return rand() % m + 1;
}
//calculate the guess values randomly
void Guess::foundGuess()
{
for (int i = 0; i<n; i++)
{
guess[i] = RandomGuess();
}
}
#ifndef guess_hpp
#define guess_hpp
#include <cstdlib>
class Guess {
public:
Guess()
{
n = 0;
m = 0;
}
void ChooseMN();
bool UserChoose();
private:
int n, m;
int *guess;
int RandomGuess();
void foundGuess();
};
#endif /* guess_hpp */
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.