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

Hi, For this assignment I have to use vector and I am struggling on where possib

ID: 667715 • Letter: H

Question

Hi,

For this assignment I have to use vector and I am struggling on where possibly I could apply that in this following code.

USING VECTOR FIELD

USING VECCTORS

IN C++ For your assignment you are writing a number guessing game; with a twist. In your game, they are calculations where the user must guess the missing number(s). The Computer guesses a random calculation "Value1 Operator Value2 = Answer." The computer randomly displays two of the four objects. The Player must guess the remaining two objects. If the player gets one right then the computer says "one is right." The game should continue until the user has provided all the correct calculations.

For Example:

The Computer decides on 12 + 34 = 46. The computer/game then randomly displays Operator is + and Value 2 is 34. The player should now begin guessing numbers. If the player guesses 12 and 20 The computer will respond "one is right" indicating either 12 or 20 are correct.

The game stops when the player guesses 12 and 46 thus giving the random calculation 12 + 34 = 46.

Comment

Expert Answer

Anonymous answered this 15 minutes laterWas this answer helpful?

0

0

24 answers

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()

{

srand(static_cast<unsigned int>(time(0))); //Random Number Generator

double value1 = rand() % 50 + 1; //Random number between 1 and 50

double value2 = rand() % 50 + 1;

int operatorDefine = rand() % 4 + 1; // random number between 1 and 4 to determine random operator

char operator1;

double answer;

int defineObject1 = rand() % 3 + 1;

int defineObject2 = rand() % 3 + 1;

double defineObject3;

int guess;

if (operatorDefine == 1) // if-else to decide random operator

{

operator1 = '+';

}

else if (operatorDefine == 2)

{

operator1 = '-';

}

else if (operatorDefine == 3)

{

operator1 = '*';

}

else if (operatorDefine == 4)

{

operator1 = '/';

}

if (operator1 == '+') // if-else to solve the random equation

{

(answer = value1 + value2);

}

else if (operator1 == '-')

{

(answer = value1 - value2);

}

else if (operator1 == '*')

{

(answer = value1 * value2);

}

else if (operator1 == '/')

{

(answer = value1 / value2);

}

cout << "Welcome to Guess My Number, with a twist! ";

if (defineObject1 == 1) //if else to display the first piece of equation

{

cout << "Value 1 is " << value1 << endl;

}

else if (defineObject1 == 2)

{

cout << "Value 2 is " << value2 << endl;

}

else if (defineObject1 == 3)

{

cout << "The answer is " << answer << endl;

}

if (defineObject2 == 1) //if else to display the second piece of the equation

{

cout << "Value 1 is " << value1 << endl;

}

else if (defineObject2 == 2)

{

cout << "Value 2 is " << value2 << endl;

}

else if (defineObject2 == 3)

{

cout << "The answer is " << answer << endl;

}

if (defineObject1 == 1 && defineObject2 == 2) //if else to determine what the player needs to guess

{

(defineObject3 = answer);

}

else if (defineObject1 == 2 && defineObject2 == 1)

{

(defineObject3 = answer);

}

else if (defineObject1 == 1 && defineObject2 == 3)

{

(defineObject3 = value2);

}

else if (defineObject1 == 3 && defineObject2 == 1)

{

(defineObject3 = value2);

}

else if (defineObject1 == 2 && defineObject2 == 3)

{

(defineObject3 = value1);

}

else if (defineObject1 == 3 && defineObject2 == 2)

{

(defineObject3 = value1);

}

cout << "The operation is " << operator1 << endl;

cout << "Input the missing piece of the equation." << endl; //Retrieve player's guess

cin >> guess;

while (guess != defineObject3) // loop to determine if the player was correct

{

cout << "Sorry, but that is incorrect. Please try again." << endl;

cin >> guess;

}

else

{

cout << "Good Job, that is CORRECT!" << endl;

}

system("PAUSE");

return EXIT_SUCCESS;

}

int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}

Explanation / Answer

In the following given code, just follow the given below steps to include vector concept in the program :

Step 1: Firstly, Add header file #include <vector>.

Step 2: Secondly, Declare a vector of integers std::vector<int> v; .

Step 3: Thirdly, If you want to use namespace then you can write code as:

#include <vector>

using namespace std;

//.................

vector<int> v;

After you include std once then you no need to write the std again n again....

The answer given by expert is helpfull to you. It also depends on the values which you will be going to give as your input after the execution of program. Vector concept is just used as an array concept works where number of values are to be included. After the inputted value, just follow the working of code.

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