finding minimum through array & random number generator

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;


int main()
{
    int count;
    int choice;
    int max, min;

    //asks user for amount of integers they would like to use.
    cout << "Please enter the number of integers between 1 and 75 that you would like to work with: ";
    cin >> choice;
    
    // if the user doesn't enter a number from the range 1 - 75
    while (choice < 1 || choice > 75)
    {
        cout << "Invalid Choice\n";
        cout << "Please enter a number between 1 - 75: ";
        cin >> choice;
    }    
    
    // array to store numbers
    const int ARRAY_SIZE = choice;
    double number[ARRAY_SIZE];
    
        // random 
	srand((unsigned)time(0));
	for (count = 0; count < ARRAY_SIZE; count++)
	{
		number[count] = rand() %100 + 1; 
		cout << " " << number[count] << endl;
        
        // maximum and minimum
        if (number[count] > max) max = number[count];
		else if (number[count] < min) min = number[count];
        
	}
    
    
    // finds average
    int sum = 0;
	double average = 0;
    
	for (count = 0; count < ARRAY_SIZE; count++)
	{
		sum += number[count];
        
	}

	average = double(sum) / ARRAY_SIZE;

        // displays average
	cout << "\nThe average is: " << average << endl;
    
    cout << "\nI will give the maximum and minimum of the numbers";
    
    //displays minimum and maximum
    cout << "\n\nThe maximum is: " << max;
    cout << "\nAnd the minimum is: " << min;
    
	return 0;
    
}


With the users input, the program displays a random set of numbers then finds the average, maximum, and minimum of the set of numbers.

The programs builds fine, average and maximum are fine, but the minimum is always zero. Any help please?
The problem is you never initialize max,min, so they contain junk (or maybe are initialized in your implementation as 0).

Anyway just put the first random number in both max and min and the rest as you have done.
Topic archived. No new replies allowed.