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, 6, 34, 67, 88, and 12, the output of the program would be:

1

6

34

67

88

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

44

32

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

Note: Attach your cpp file to this question.

Solutions

Expert Solution

Code:

#include<iostream>  // header file for input output operation
#include <ctime>    // for random number generation
using namespace std;

int main(){
        srand ( time(NULL) );   // setting srand to get random different number each time code is executed 
    
    int number, array[1000], flag= 0, count = 0, value = 0; // value variable is used to decide the series is increasing or decreasing
    while (flag != 1){  // while loop till flag is set to 1 (flag variable used to check the rules)
        number = rand()%100 + 1;    // generating the random number
        array[count] = number;  // assigning to array
        if(count == 0){ // checking if first number is already entered or not   
            count++;    // increasing the counter value
            continue;   // skiping all other statements
        }

        if(count == 1 && number == array[0]){   // checking if first number id equal to second number
                flag = 1;   // setting the flag
                continue;
                } // end of if
        else if( count == 1 && number < array[0]){
            value = -1; // seting the value -1 for decreasing series
        }
        else if( count == 1 && number > array[0]){
            value = 1;  // seting the value 1 for increasing series
        }
        
        else{
            if( count > 1 && value == -1 ){ // decreasing series
                if (number >= array[count -1]){ // checking if number is greater than or equal to last entered number
                    flag = 1; // setting the flag
                } // end of if
                if (flag == 1){
                    for (int i=0;i<count;i++)   // loop to print the array contant
                        cout << array[i] << endl; // printing element
                } // end of if
            }
            
            
            else if(count > 1 && value == 1){  // increasing series
                if (number <= array[count -1]){ // checking if number is less than or equal to last entered number
                    flag = 1; // setting the flag
                } // end of if
                if (flag == 1){ // if flag is set
                    for (int i=0;i<count;i++)   // loop to print the array contant
                        cout << array[i] << endl; // printing element
                } // end of if
            }
            
        }// end of else
    count++;
    
    } // end of while
    return 0;
}

Sample output:

* you need to run the code again and again repidly to get best result. to get these result I have to run too many times as again and again system is generating number like big,lower,big and lower,big,lower

Note: Please let me know if you have any doubt or there is any change required in the code.


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