Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++ Write a program that Generates 50 random numbers (numbers can range from

ID: 3913474 • Letter: I

Question

In C++

Write a program that Generates 50 random numbers (numbers can range from -100 to 100) and outputs the following

Sorted numbers in ascending order.

Sorted numbers in descending order

Sum of all numbers

Product of all numbers

Returns the min and max number

Asks user if they want to repeat the program or exit.

The program should not accept string as an input and should be able to handle it by sending a message to the user asking to input numbers in a correct format again. The program should not be halted in an event where a string was inputted.

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

//Code

#include<iostream>

#include<stdlib.h>

#include<time.h>

using namespace std;

//method to generate random numbers between -100 and 100 and fill the array

void generate_random(int *array,int size){

                int min=-100,max=100;

                for(int i=0;i<size;i++){

                                int num=min + rand() % (( max + 1 ) - min);

                                array[i]=num;

                }

}

//method to sort the array in ascending order

void sortAsc(int *array,int size){

                for(int i=0;i<size-1;i++){

                                for(int j=0;j<size-i-1;j++){

                                                if(array[j+1]<array[j]){

                                                                int temp=array[j+1];

                                                                array[j+1]=array[j];

                                                                array[j]=temp;

                                                }

                                }

                }

}

//method to sort the array in descending order

void sortDesc(int *array,int size){

                for(int i=0;i<size-1;i++){

                                for(int j=0;j<size-i-1;j++){

                                                if(array[j+1]>array[j]){

                                                                int temp=array[j+1];

                                                                array[j+1]=array[j];

                                                                array[j]=temp;

                                                }

                                }

                }

}

//method to print the array

void print(int *array,int size){

                for(int i=0;i<size;i++){

                                cout<<array[i]<<" ";                      

                }

                cout<<endl;

}

//method to find the sum of elements in the array

int sum(int *array,int size){

                int sum=0;

                for(int i=0;i<size;i++){

                                sum+=array[i];                 

                }

                return sum;

}

//method to find the product of elements in the array. the resultant number will be huge

//which is why I have used a long long data type

long long product(int *array,int size){

                long long prod=1;

                for(int i=0;i<size;i++){

                                prod=prod*array[i];                      

                }

                return prod;

}

//method to find the minimum element in the array

int min(int *array,int size){

                int min=0;

                for(int i=0;i<size;i++){

                                if(i==0){

                                                min=array[i];

                                }else if(array[i]<min){

                                                min=array[i];

                                }

                }

                return min;

}

//method to find the maximum element in the array

int max(int *array,int size){

                int max=0;

                for(int i=0;i<size;i++){

                                if(i==0){

                                                max=array[i];

                                }else if(array[i]>max){

                                                max=array[i];

                                }

                }

                return max;

}

int main(){

                //seeding random number generator

                srand(time(NULL));

                int size=50;

                int array[size];

                //generating array with random elements

                generate_random(array,size);

                int choice=1;

               

                int sumTotal,minimum,maximum;

                long long prod;

                //looping until user wishes to quit

                while(choice!=7){

                                //showing the menu

                                cout<<"1. Sorted numbers in ascending order"<<endl;

                                cout<<"2. Sorted numbers in descending order"<<endl;

                                cout<<"3. Sum of all numbers"<<endl;

                                cout<<"4. Product of all numbers"<<endl;

                                cout<<"5. The min and max number"<<endl;

                                cout<<"6. Generate numbers again"<<endl;

                                cout<<"7. Quit"<<endl;

                                cout<<"Enter your choice: "<<endl;

                               

                                cin>>choice;

                                if(!cin){

                                                //string entered in place of int

                                                cout<<"Please enter a valid choice!"<<endl;

                                                cin.clear();

                                cin.ignore(1,' ');

                                }else{

                                //performing operation based on choice             

                                switch(choice){

                                                case 1: sortAsc(array,size);

                                                                                print(array,size);

                                                break;

                                                case 2: sortDesc(array,size);

                                                                                print(array,size);

                                                break;

                                                case 3:

                                                                sumTotal= sum(array,size);

                                                                cout<<"Sum: "<<sumTotal<<endl;

                                                break;

                                                case 4:

                                                                prod= product(array,size);

                                                                cout<<"Product: "<<prod<<endl;

                                                break;

                                                case 5:

                                                                minimum= min(array,size);

                                                                maximum=max(array,size);

                                                                cout<<"Min: "<<minimum<<endl;

                                                                cout<<"Max: "<<maximum<<endl;

                                                break;

                                                case 6:

                                                                generate_random(array,size);

                                                                cout<<"Generated!"<<endl;

                                                break;

                                                case 7: cout<<"Bye!"<<endl;

                                                break;

                                                default: cout<<"Enter a valid choice (1-7)"<<endl;

                                }

                }

                               

                }

}

/*OUTPUT*/

1. Sorted numbers in ascending order

2. Sorted numbers in descending order

3. Sum of all numbers

4. Product of all numbers

5. The min and max number

6. Generate numbers again

7. Quit

Enter your choice:

4

Product: -8322652111380676608

1. Sorted numbers in ascending order

2. Sorted numbers in descending order

3. Sum of all numbers

4. Product of all numbers

5. The min and max number

6. Generate numbers again

7. Quit

Enter your choice:

1

-97 -91 -86 -75 -66 -64 -54 -53 -35 -30 -29 -28 -22 -20 -16 -14 -11 -6 -3 3 5 12 12 19 25 29 36

39 40 41 52 53 62 64 66 68 73 73 77 78 81 82 82 84 85 88 93 95 96 99

1. Sorted numbers in ascending order

2. Sorted numbers in descending order

3. Sum of all numbers

4. Product of all numbers

5. The min and max number

6. Generate numbers again

7. Quit

Enter your choice:

2

99 96 95 93 88 85 84 82 82 81 78 77 73 73 68 66 64 62 53 52 41 40 39 36 29 25 19 12 12 5 3 -3 -

6 -11 -14 -16 -20 -22 -28 -29 -30 -35 -53 -54 -64 -66 -75 -86 -91 -97

1. Sorted numbers in ascending order

2. Sorted numbers in descending order

3. Sum of all numbers

4. Product of all numbers

5. The min and max number

6. Generate numbers again

7. Quit

Enter your choice:

3

Sum: 1012

1. Sorted numbers in ascending order

2. Sorted numbers in descending order

3. Sum of all numbers

4. Product of all numbers

5. The min and max number

6. Generate numbers again

7. Quit

Enter your choice:

5

Min: -97

Max: 99

1. Sorted numbers in ascending order

2. Sorted numbers in descending order

3. Sum of all numbers

4. Product of all numbers

5. The min and max number

6. Generate numbers again

7. Quit

Enter your choice:

6

Generated!

1. Sorted numbers in ascending order

2. Sorted numbers in descending order

3. Sum of all numbers

4. Product of all numbers

5. The min and max number

6. Generate numbers again

7. Quit

Enter your choice:

t

Please enter a valid choice!

1. Sorted numbers in ascending order

2. Sorted numbers in descending order

3. Sum of all numbers

4. Product of all numbers

5. The min and max number

6. Generate numbers again

7. Quit

Enter your choice:

1

-100 -89 -89 -88 -86 -75 -74 -72 -72 -72 -68 -63 -62 -56 -55 -51 -46 -44 -42 -41 -34 -26 -22 -2

0 -19 -4 2 14 16 20 29 32 41 45 47 57 59 60 61 66 68 68 71 72 73 83 83 91 95 98

1. Sorted numbers in ascending order

2. Sorted numbers in descending order

3. Sum of all numbers

4. Product of all numbers

5. The min and max number

6. Generate numbers again

7. Quit

Enter your choice:

7

Bye!