dimanche 27 mai 2018

C++ Generate Random Amount (x) random integers (num) -- How to display the random integers?

I am stuck on this code (very beginner) and not sure what I am doing wrong. I need to display X number of random integers (num), but I am not sure how to do this.

Currently, I was able to find how to make x random, but do not know how to make it display that many random integers...not sure how or where to enter "num"

Any pointers? Really lost on this. Assignment description below too. Tried to make sure I followed the guidelines, and not sure how to search for this to see if there were duplicates first.

/*
P33: Smallest, Largest, Sum, and Average with Random Numbers
Write a program that generates X random integers Num.
Num is a random number between 20 to 50.
X is a random number between 10 to 15.
Calculate and show the Smallest, Largest, Sum, and Average of those numbers.

Sample Run:
Generating 11 random numbers (11 is a random number between 10 to 15)...

...11 Random Numbers between 20 to 50:  26, 23, 48, 32, 44, 21, 32, 20, 49, 48, 34

Sum = 377

Average = 377 / 11 = 34.3

Smallest = 21

Largest = 49
*/

#include <stdlib.h>
#include <time.h>
#include <iostream>

using namespace std;

int sum = 0;
int x = 10+rand()%6;
int num = 20+rand()%31;
int max;
int largest;
int smallest;

int main(void)
{
     srand(time(NULL));
{
while (true){
        int sum = 0;
        int x = 10+rand()%6;
        int num = 20+rand()%31;
        cout<<"Generating " << x << " random numbers (" << x << " is between 10 and 15)... \n";
        int max;
        int largest;
        int smallest;
        cout << "..." << x << " Random Numbers between 20 to 50:  \n";
        largest = num;
        smallest = num;
        sum = sum + num;

        while (x < max - 1)
        {
            cout << num;
            sum = sum + num;
            x++;
            if ( num > largest)
                largest = num;
            if ( num < smallest)
                smallest = num;
        }
        cout << "Largest = " << largest << endl;
        cout << "Smallest = " << smallest << endl;
        cout << "Sum = " << sum << endl;
        cout << "Average = " << sum / max << endl;

            break;
    }
    return 0;
}
}

/*
SAMPLE RUN
==========
Generating 10 random numbers (10 is between 10 and 15)...
...10 Random Numbers between 20 to 50:
Largest = 29
Smallest = 29
Sum = 29
Average = -14

Process returned 0 (0x0)   execution time : 0.172 s
Press any key to continue.

*/




Aucun commentaire:

Enregistrer un commentaire