trouble finding the minimum value in a array

Hello

I am having a issue with finding the lowest value in a array. For some reason it keep comes up with what the average is.

my input values are 3.1, 4.3, 2, 0.2, 1.2, 1.3, 4.5, 1.3, 2.2, 2, 1, 1.1
total comes out as 24.2
average comes out as 2.0
maximum comes out as 4.5
minimum comes out as 2.0 it should be 0.2

thanks in advance for helping me.


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
69
70
71
72
73
74
 #include <iostream>
#include <iomanip>
using namespace std;

float fun(float);

int main()
{
	const int MONTHS = 12;
	float rainFall [MONTHS];
	float rain = 0;

	cout << fixed << showpoint << setprecision(1) << endl;

	rain = fun(rain); 

	cout << " Enter the rainfall totals for the remaining months: " << endl;
	rainFall[0] = rain;
	cin >> rainFall[1];
	cin >> rainFall[2];
	cin >> rainFall[3];
	cin >> rainFall[4];
	cin >> rainFall[5];
	cin >> rainFall[6];
	cin >> rainFall[7];
	cin >> rainFall[8];
	cin >> rainFall[9];
	cin >> rainFall[10];
	cin >> rainFall[11];

float max;
	int num1;

	max = rainFall[0];
	for (num1 = 1; num1 < MONTHS; num1++)
	{
		if (rainFall[num1] > max)
			max = rainFall[num1];
	}

	float min;
	int num2;

	min = rainFall[0];
	for (num2 = 1; num2 < min; num2++)
	{
		if (rainFall[num2] < min)
			min = rainFall[num2];
	}

	float total = 0;
	for (int count = 0; count < MONTHS; count++)
		total += rainFall[count];

	float average = total / MONTHS;

	

	cout << endl;

	cout << " Total rainfal:      " << total << endl;
	cout << " Average:            " << average << endl;
	cout << " Maximum:            " << max << endl;
	cout << " Minumum:            " << min << endl;

}

float fun(float rain)
{
	cout << " Enter the rainfall total for month one: " << endl;
	cin >> rain;
	
	return rain;
} Put the code you need help with here.
closed account (48T7M4Gy)
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
#include <iostream>
 
using namespace std;

int main()
{
	float rainFall []  = {3.1, 4.3, 2, 0.2, 1.2, 1.3, 4.5, 1.3, 2.2, 2, 1, 1.1};
	int MONTHS = sizeof(rainFall)/sizeof(float);
	
	float max;
	
	max = rainFall[0];
	for (int i = 1; i < MONTHS; i++)
	{
		if (rainFall[i] > max)
			max = rainFall[i];
	}

	float min;
	
	min = max;
	for (int i = 1; i < MONTHS; i++)
	{
		if (rainFall[i] < min)
			min = rainFall[i];
	}

	float total = 0;
	for (int i = 0; i < MONTHS; i++)
		total += rainFall[i];

	float average = total / MONTHS;
	
	cout << "Total rainfall: " << total << endl;
	cout << "      Average: " << average << endl;
	cout << "      Maximum: " << max << endl;
	cout << "      Minumum: " << min << endl;
	
	return 0;
}
Total rainfall: 24.2
      Average: 2.01667
      Maximum: 4.5
      Minumum: 0.2
 
Exit code: 0 (normal program termination)
Last edited on
A small mistake here

for (num2 = 1; num2 < min; num2++)

use MONTHS instead of min
thanks guys i appreciate the help .
Topic archived. No new replies allowed.