#include<iostream> #include<ctime> #include<cstdlib> using namespace std; int ma
ID: 3568825 • Letter: #
Question
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
srand(time(0));
int number = rand() % 101;
cout << " guess a magic number between 0 and 100";
int guess = -1;
while (guess != number)
{
cout <<" Enter your guess:";
cin >> guess;
if(guess == number)
cout << "yes, the number is " << number << endl;
else if(guess > number)
cout << "your guess is too high" << endl;
else
cout << "your guess is too low " << endl;
}
return 0;
}
I know what the program supposed to do. It creates some random number that are less than 101. and it asks to guess what random number the program picked. my question is what the hell , int number = random() % 101, is doing there? I think the percent sign,%, work for some remainder thing, am I right? what about ,int guess = -1, what is that for? any help will be appreciated!
thanks
Explanation / Answer
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
srand(time(0));
int number = rand() % 101;
cout << " guess a magic number between 0 and 100";
int guess = -1;
while (guess != number)
{
cout <<" Enter your guess:";
cin >> guess;
if(guess == number)
cout << "yes, the number is " << number << endl;
else if(guess > number)
cout << "your guess is too high" << endl;
else
cout << "your guess is too low " << endl;
}
return 0;
}
int number = rand() % 101, is doing there? I think the percent sign,%, work for some remainder thing, am I right?
rand() function generates a random number between 0 and RAND_MAX.
rand()%101 will divide that number with 101 and give reminder between 0 and 100 (modulus operator).
so number will be in between 0 and 100.
int guess = -1, what is that for? any help will be appreciated!
to start the loop i.e since number is a positive number guess initialized with -1 and value other than between 0 and 100 will work out.
ie. guess can be any initialized with <0 or >100
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.