Generate a random sequence

Hi I need to Design and write a C++ program that generates a random sequence of 20 integer values in the range between 0 and 200, finds the largest and the smallest values in the sequence, and computes the average of all values.

This i what i did for now
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{

    const int min = 0;
    const int max = 200;
    const int N = 20;

    int random_number;
    int largest = 0;
    int smallest = 200;
    int sum = 0;
    int count;
    
      srand ( time(NULL) );
      

  for (int count=0; count <20; count++)
  cout << "Random number =" << rand() % 201 <<endl;

  
    system("PAUSE");
    return EXIT_SUCCESS;
}


But i having a problem to assign the random Value to rand_number. you may say why i do that because the following is program algorithm given which i need to follow

//declare constant integers min = 0, max = 200, N = 20
//declare the integer variables:
//random_number, largest = 0, smallest = 200, sum = 0, count
//use srand (time(NULL)) function to ensure that your program will generate different
//sequences each time it runs
//start a for() loop where count = 0; count < 20; count ++
//within the loop perform the following:
// Use function rand() and an appropriate formula to generate a random value
// Assign the random value to rand_number
// Display the value on the screen
// Add rand_number to the sum ( sum = sum + rand_number)
// If rand_number > largest, re-assign largest = rand_number
// If rand_number < smallest, re-assign smallest = rand_number
//When the loop is completed display largest, smallest values and compute
//and display the average


So how can i assign the random value to random_number. can someone please help me. Thanks in advance for your help
You can try this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <cmath>
#include <limits>

int main()
{
    srand(time(0)); //seeds the rand() function

    const int N = 20;

    int random_number = 0;
    int largest = 0;
    int smallest = 200;
    int sum = 0;

    for (int i = 0; i < 20; i++) //for int i = 0; i is smaller than 20 (will stop when i is 19), i + 1
    {
        random_number = rand() % 200 + 1; //random number is now 1 - 200 (0 - 199 + 1)
        std::cout << "Random number " << i << ": " << random_number << std::endl; //prints random_number
        sum += random_number; //sum = sum + random_number
        if (random_number > largest) //if random_number is bigger than largest
            largest = random_number; //its now largest
        else if (random_number < smallest) //else if its smaller than smallest
            smallest = random_number; //its now smallest
    }
    std::cout << "Largest number is: " << largest << std::endl //prints largest
              << "Smallest number is: " << smallest << std::endl //prints smallest
              << "Average is: " << sum / N << std::endl; //prints average (sum divided by N which is const int 20)

    std::cout <<  "Press ENTER to continue..." << std::endl;
    std::cin.ignore(std::numeric_limit<std::streamsize>::max(), '\n'); //a better way to System("PAUSE"); you need to #include <limits> for this
    return(0);
}


I hope the comments explain it well enough.
Last edited on
Why do you always post complete solutions?
Thanks very much bluezor but i have 1 small problem with the std::cin.ignore(std::numeric_limit<std::streamsize>::max(), '\n');

It says numeric_limit' is not a member of `std' and i have double check that i am running #include <limits> database on top
Thanks very much bluezor but i have 1 small problem with the std::cin.ignore(std::numeric_limit<std::streamsize>::max(), '\n');

It says numeric_limit' is not a member of `std' and i have double check that i am running #include <limits> database on top

You get that error because you are using namespace std;. If you are using that just cut off the std:: off, then it should work.

Why do you always post complete solutions?

I'm not sure.. what should I do?
Instead of saying "here, it is done", say "to do it, you need such and such". Solving the problem for them doesn't really help them. If you notice one particular thing they're doing wrong, either point it out or give them hits so they can figure it out on their own.
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime."

And no, the compiler is smart enough to try std::std::X and std::X. The problem is that you made a typo. std::numeric_limits.
And OP is lazy as hell both for using your solution and for not trying to research on his own. Googling "numeric_limit" returns numeric_limits on its first result
Last edited on
I see... thanks helios..
Teaching to fish is much better in the long term
Topic archived. No new replies allowed.