Okay, so I just got some help on how to find the average of 30 randomly generated numbers. The next thing I would like to do however is a little trickier, I think. I would like to display the highest randomly generated number in the equation.

Here is what i have so far.

/*********************************
  Name:  Roger Clemons
*********************************/
#include <iostream> 
#include <cstdlib>
using namespace std;

#include <math.h>

void get_range(int &a, int &b)
{
    cout << "Enter the lowest temperature range: ";
    cin >> a;
    cout << "Enter the highest temperature range: ";
    cin >> b;

}

int main()
{
    int low = 0;
    int high = 0;
    const int SEPTEMBER = 30;
    int random = -1;
    int randomArray[SEPTEMBER];
    double sum = 0;

    get_range(low, high);
    cout << endl;
    cout << "Low = " << low << endl;
    cout << "High = " << high << endl;

    int range=(high-low)+1;

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

        random = low + int(range*rand()/(RAND_MAX + 1));
        randomArray[i] = random;
        sum += randomArray[i];
    }
    cout << "Average = " << sum/SEPTEMBER << endl;

    return 0;
}

So now all i would like to do is display the highest number in the array, anyone that is willing to help that would be awesome. Thanks!

Recommended Answers

All 5 Replies

>I would like to display the highest randomly generated number in the equation.
How is that trickier? Keep a running maximum (like the sum in your average algorithm) as you walk through the array. When you see a value that's larger than the maximum, it becomes the new maximum:

int max = a[0];

for ( int i = 1; i < 30; i++ ) {
  if ( a[i] > max )
    max = a[i];
}

At the end of the loop, max contains the largest value in the entire array.

Well one option would be to create an integer called 'max_value', or something. Set it equal to the first element of the array. Iterate through the entire array and test each element against the max value. If the element it bigger assign, else leave it.

Thanks alot Narue you are incredibly good C++

>Narue you are incredibly good C++
I know, but this is still very basic stuff. Fortunately, you can take what you've learned and apply it to a broad range of problems. I don't expect to have to show you the same concept a third time...

Nope just did the lowest number in the range basically the same way. I pretty much have grasped the concept of random arrays and finding out things about them. Thanks alot for all your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.