Question

In: Computer Science

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 equal to 0. For example, assuming:
    • the first three random integers are: 98, 23, 48
    • the second three random integers are: 45, 5, 75
    • the third three random integers are: 4, 95, 33
    • the fourth three random integers are: 11, 61, 21
    • the fifth three random integers are: 100, 20, 40 ---------> The program terminates as all the three numbers end with 0

The output will be:

5, 45, 75

11, 21, 61

20, 40, 100

Please notice that all the numbers are not displayed at the end of the program. Every line is displayed in one iteration and the last line is displayed in the last iteration of your loop.

  • If the very first set of numbers generated all end with zero, for example, the first three integers are: 50, 40, 10, after the program shows these three numbers in ascending order, it also displays: "... are your lucky numbers."

So, the output would be:

10, 40, 50 are your lucky numbers.

California College

Solutions

Expert Solution

Program:

#include <iostream>

using namespace std;

void sortNumbers(int num1, int num2, int num3) {
    if(num1<num2 && num1<num3 && num2>num3 )
    {
        cout << num1 << " " << num3 << " " << num2;
    }
    else if(num1<num2 && num1<num3 && num2<num3 ) 
    {
        cout << num1 << " " << num2 << " " << num3;
    }
    else if (num2<num1 && num2<num3 && num3>num1 ){
        cout << num2 << " " << num1 << " " << num3;
    }
    else if (num2<num1 && num2<num3 && num3<num1 ){
        cout << num2 << " " << num3 << " " << num1;
    }
    else if (num3<num1 && num3<num2 && num1>num2){
        cout << num3 << " " << num2 << " " << num1;
    }
    else if (num3<num1 && num3<num2 && num1<num2){
        cout << num3 << " " << num1 << " " << num2;
    }
}

int main()
{
    bool isCheck = true;
    while (isCheck) {
        // To get random numbers range of 1-100
        int num1 = (rand() % 100) + 1;
        int num2 = (rand() % 100) + 1;
        int num3 = (rand() % 100) + 1;
        // Will give the right most digit of the number
        int num1Rem = num1%10;
        int num2Rem = num2%10;
        int num3Rem = num3%10;
        // condition to check if the right most digits are 0 for all 3 numbers
        if (num1Rem == 0 && num2Rem == 0 && num3Rem == 0) {
            isCheck = false;
            // Sort the numbers in ascending order 
            sortNumbers(num1, num2, num3);
            cout << " are lucky numbers" << endl;
        }
        // Condition to check if the right most digits are same for all 3 numbers
        else if (num1Rem == num2Rem && num1Rem == num3Rem && num2Rem == num3Rem) {
            cout << "Random integers are: ";
            sortNumbers(num1, num2, num3);
            cout << endl;
        }
        
    }

    return 0;
}

Output:


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 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....
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 4: Calorie Counting Specifications: Write a program that allows the user to enter the number of calories consumed per day. Store these calories in an integer vector. The user should be prompted to enter the calories over the course of one week (7 days). Your program should display the total calories consumed over...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 1: Largest and Smallest Vector Values Specifications: Write a program that generates 10 random integers between 50 and 100 (inclusive) and puts them into a vector. The program should display the largest and smallest values stored in the vector. Create 3 functions in addition to your main function. One function should generate the...
Use Selections, DO NOT USE ARRAYS AND METHODS. (Find future dates) Write a program that prompts...
Use Selections, DO NOT USE ARRAYS AND METHODS. (Find future dates) Write a program that prompts the user to enter an integer for today’s day of the week (Sunday is 0, Monday is 1, ..., and Saturday is 6). Also prompt the user to enter the number of days after today for a future day and display the future day of the week. ** Can not use java.time.DayOfWeek; SAMPLE RUN #1: java FindFutureDates Enter today's day: 4↵ Enter the number...
Write a program in C++ that generates and displays the first N three digit odd numbers....
Write a program in C++ that generates and displays the first N three digit odd numbers. Whereas the number N is provided by the user.
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an array which can store up to 50 student names where a name is up to 25 characters long an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
Program Assignment 1 C++ please Instructions This assignment will require the use of three arrays, which...
Program Assignment 1 C++ please Instructions This assignment will require the use of three arrays, which will be used in parallel. Create a program that keeps track of the sales of BBQ sauces for a company. The company makes several different types of sauces, Original, Sticky Sweet, Spicy, Sweet Heat, Hickory Bourbon and Smokey Mesquite. One array will contain the names of the different BBQ sauces. This array will be initialized from a text file with the 6 different names....
this has to be coded in c ++ • Write a program which generates a DIFFERENT...
this has to be coded in c ++ • Write a program which generates a DIFFERENT random number between 1 and 100 everytime you run the program. Please go online and see how poeple are trying to solve this. You can submit their code. Make sure you include a comment which shows from where the code was taken. You do not need to understand the code but if you can, thats great. This program will be used in the upcoming...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT