Question

In: Computer Science

Write a program that repeatedly generates a random integer in the range [1, 100], one integer...

Write a program that repeatedly generates a random integer in the range [1, 100], one integer at a time, and displays the generated numbers on the screen according to the following rules:

  • If the second number generated is greater than the first number, they are displayed on the screen in the order of their input and while the next random number generated is greater than the previous one, the random number is displayed on the screen and the program continues. As soon as the number generated is less than or equal to the previous one, the program terminates. For example, if the first 6 random numbers generated are 1, 5, 32, 68, 89, and 11, the output of the program would be:

1

5

32

68

89

  • If the second number generated is less than the first number, they are displayed on the screen in the order of their input and while the next random number generated is less than the previous one, the random number is displayed on the screen and the program continues. As soon as the number generated is greater than or equal to the previous one, the program terminates. For example, if the first 3 random numbers generated are 45, 31, and 31, the output of the program would be:

45

31

  • If the second number generated is equal to the first number, the program terminates and no message is displayed on the screen.

IN C++ ONLY PLEASE

Solutions

Expert Solution

1- Required program in C++ -->

#include <iostream>
#include <random>
using namespace std;
int main()
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> distr(1, 100); // range 1-100
int a=distr(gen); //produce a random variable and store the value in a
std::cout<<a<<endl; // print a
int c=1; // let c=1
while(c==1){ // while loop will run till break statement is executed
int b=distr(gen); // produce a random variable and store the value in b
if(b<=a){ // if b is less than or equal to a
break; // break the while loop
}
std::cout<<b<<endl; // print b
a=b; // assign value of b to a
}
}

2-

Required code in C++ -->

#include <iostream>
#include <random>
using namespace std;
int main()
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> distr(1, 100);   // range 1-100
int a=distr(gen); //produce a random variable and store the value in a
std::cout<<a<<endl; // print a
int c=1; // let c=1
while(c==1){ // while loop will execute till break is used
int b=distr(gen); //produce a random variable and store the value in b
if(b>=a){   // if b is greater than or equal to a
break; // break the while loop
}
std::cout<<b<<endl; // print b
a=b;   // assign value of b to a
}
}


Related Solutions

Write a program that repeatedly generates a random integer in the range [1, 100], one integer...
Write a program that repeatedly generates a random integer in the range [1, 100], one integer at a time, and displays the generated numbers on the screen according to the following rules: If the second number generated is greater than the first number, they are displayed on the screen in the order of their input and while the next random number generated is greater than the previous one, the random number is displayed on the screen and the program continues....
Write a program that repeatedly generates a random integer in the range [1, 100], one integer...
Write a program that repeatedly generates a random integer in the range [1, 100], one integer at a time, and displays the generated numbers on the screen according to the following rules: If the second number generated is greater than the first number, they are displayed on the screen in the order of their input and while the next random number generated is greater than the previous one, the random number is displayed on the screen and the program continues....
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the...
Write a program that generates a random number between 1 and 100 and asks the user...
Write a program that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. Your program should also keep a...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three random integers in the range [1, 100] and continues as follows: If the right-most digit of all the three integers is equal, the program displays them in ascending order on the screen and continues. If the generated integers have different right-most digits, they are not displayed and the program continues. The program terminates once the right-most digits of all the three random numbers are...
Challenge 4 – Random Integer in Range Write a function to return a random integer between...
Challenge 4 – Random Integer in Range Write a function to return a random integer between a minimum value and maximum value. var ival = IntRandomRange(, );
we saw a C program called GuessNumber.c that generates a random integer between 1 and 1000...
we saw a C program called GuessNumber.c that generates a random integer between 1 and 1000 and asks the user to guess that number. Modify the program so that at the end of each round, it also prints the number of guesses made by the user to reach the answer as well as the best score so far, i.e., the minimum number of guesses used in any round since the program was started. The source code of the original version...
Develop a C program that generates a random number from 1 to 100, then prompt the...
Develop a C program that generates a random number from 1 to 100, then prompt the user to guess the number until it is guessed correctly. Let the user know if the guess is too high, too low, or correct. The program should count the number of times the user guesses and display the number, along with their rank, after the number is guessed correctly. Rank the user as follows: Super Guesser: 1 to 4 guesses Excellent Guesser: 5 to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT