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, 31, 69, 87, and 11, the output of the program would be:

1

5

31

69

87

  • 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 43, 31, and 31, the output of the program would be:

43

31

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

Solutions

Expert Solution

//Kindly note that only one question can be answered according to our guidelines

//But for you i hinted the change in condition in a comment line and you'll get the required output for second usecase

import java.util.Random;
public class RandomG
{
public static void main(String args[])
{
// creating an object of Random class
   Random random = new Random();
// Generates random integers 0 to 100
   int last=random.nextInt(100);
   boolean flag=true;
   System.out.println(last);
   while(flag){
       int c=random.nextInt(100);
       if(c>last){
       //if(c<last){
       //This condition yields the output for second use case
           System.out.println(c);
           last=c;
       }else{
           flag=false;
       }
   }
  
}
}

Output:

C++:

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
   bool flag=true;
   int last = (rand() % 100) + 1;
   cout<<last<<endl;
   while(flag) {
       int c= (rand() % 100) + 1;
       if(c>last){
       //if(c<last){
       //This condition yields the output for second use case
           cout<<c<<endl;
           last=c;
       }else{
           flag=false;
       }
      
   }
   return 0;
}

//Please mask sure about specification because we need to work out more without any detailed specs


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