The user is prompted to enter number of times to repeat the generation of the two numbers.Thetworandomnumbersarebetween0and100. Thethreepossiblescenarios are: the first number could be greater than the second number, less than the second number or equal to the second number. The percentage of occurrences for the three described scenarios are computed and displayed with 2 decimal places. The formula used is : x/count * 100 where x is the total number of times the first number is greater than/less than/equal to the second number. count is the number of times to repeat the loop.
The final output screen will be: (random numbers)
Enter the number of times to play: 100
First Number > Second Number percentage score: 44.00
First Number < Second Number percentage score: 54.00
First Number equal Second Number percentage score: 2.00
Press any key to continue...
Here is my code, and i think error starts from for loop but i dunno how to correct them... Pls help me!
#include <iostream>
#include <iomanip>
usingnamespace std;
void main()
{
// declare all variables
int times, num1, num2, reply, count, percent1, percent2, percent3;
// Prompt user to enter the number of times to play
cout<<"Enter the number of times to play: ";
cin>>times;
// read times using for loop
for (int count=0; count<times; count++)
{
num1 = rand()%100+1; // generate a number between 1 and 100
num2 = rand()%100+1;
}
if (num1>num2) // print percentage score if num1>num2
{
percent1 = times / count * 100; → I don’t know how to compute the given formula x/count*100
cout<<"First number > Second Number percentage score: "<< percent1 <<endl;
cout<<fixed<<setprecision(2)
}
elseif (num1<num2) // print percentage score if num1<num2
{
percent2 = times / count * 100;
cout<<"First number < Second Number percentage score: "<< percent2 <<endl;
cout<<fixed<<setprecision(2)
}
else // print percentage score if num1=num2
{
percent3 = times / count * 100;
cout<<"First number equal Second Number percentage score: "<< percent3 <<endl;
cout<<fixed<<setprecision(2)
}
// print 'press any key to continue'
cout<<"Press any key to continue..."<<endl;
cin<<reply;
}
cin.ignore();
cin.ignore();
}
I think there are more problems than that... The main problem I'm having is i dunno how to indicate the "x" given in the question --> the total number of times the first number is greater than/less than/equal to the second number...
// there is still something wrong
#include <iostream>
#include <iomanip>
#include <cstdlib>
usingnamespace std;
int main()
{
// declare all variables
int times, num1, num2, reply, count, percent1, percent2, percent3;
// Prompt user to enter the number of times to play
cout<<"Enter the number of times to play: ";
cin>>times;
// read times using for loop
for (int count=0; count<times; count++)
{
num1 = rand()%100+1; // generate a number between 1 and 100
num2 = rand()%100+1;
}
if (num1>num2) // print percentage score if num1>num2
{
percent1 = times / count * 100; // I don’t know how to compute the given formula x/count*100
cout<<"First number > Second Number percentage score: "<< percent1 <<endl;
cout<<fixed<<setprecision(2);
}
elseif (num1<num2) // print percentage score if num1<num2
{
percent2 = times / count * 100;
cout<<"First number < Second Number percentage score: "<< percent2 <<endl;
cout<<fixed<<setprecision(2);
}
else // print percentage score if num1=num2
{
percent3 = times / count * 100;
cout<<"First number equal Second Number percentage score: "<< percent3 <<endl;
cout<<fixed<<setprecision(2);
}
// print 'press any key to continue'
cout<<"Press any key to continue..."<<endl;
cin>>reply;
}
You're attempting to use the same variable for all three outcomes. You also need to increment count for every time a given scenario is seen. And as stated above you need to include lines 23-46 in your for statement.
You should add a counter for num1 being greater a counter for num2 being greater a counter for them being equal. Then, use a variable where you store the percentage
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
int main ()
{
// assume that using <random> is not an option
std::srand( std::time(nullptr) ) ; // seed the legacy rng
constint ubound = 101 ; // upper bound on the numbers
// Prompt user to enter the number of times to play
std::cout << "Enter the number of times to play: ";
int times ;
std::cin >> times;
int cnt_less = 0 ; // count of first number < second number
int cnt_equal = 0 ; // count of first number == second number
for( int count = 0 ; count < times ; ++count )
{
// generate two random numbers in the range [0,100]
constint first = std::rand() % ubound ; // not the best way of doing this
constint second = std::rand() % ubound ;
if( first < second ) ++cnt_less ;
elseif( first == second ) ++cnt_equal ;
}
constint cnt_greater = times - cnt_less - cnt_equal ; // count of first number > second number
if( times > 0 ) // print out percentages
{
std::cout << std::fixed << std::setprecision(2) // fixed point, two digits after the decimal point
<< "\n first number less than second number: " << ( cnt_less * 100.0 ) / times << "%\n"
<< " first number equal to second number: " << ( cnt_equal * 100.0 ) / times << "%\n"
<< "first number greater than second number: " << ( cnt_greater * 100.0 ) / times << "%\n" ;
}
}
Hi JLBorges, i have input all the code into c++ but the program exit automatically after i key in any character when it prompts "Enter the number of times to play: ".......
but, i have another question: I have never learnt std:: in school yet, is it really necessary to insert std:: in front of cout/cin,etc? Is there any other function can replace it for the same output?
There is usingnamespace std; that you can put above the main functin. But you're much better off doing what @JLBorges does and use std::, saves your ass in the future