Rainfall program issues.

Hello,
I am having issues finishing up my program for class. As of right now the program runs, but the issues I have are in my lowest and highest it only displays 12. Also I need to in corporate a string to display which month is the lowest and which month is the highest. Any help would be greatly appreciated.

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <iomanip>
using namespace std;

double getMonthTotal(); 
double getTotal(double[], int);
double findAverage(double[], int);
double findHighest(double[], int, int&);
double findLowest(double[], int, int&);

int main ()
{
	const int NUM_MONTHS = 12;
	int index = 0;
	double months[NUM_MONTHS]; 
	double yearTotal, average, highest, lowest; 
	
	cout << "This program will ask you for rain fall totals for 12 months.\n"
	<< "Once the date is entered the program will give you a total, an average\n"
	<< " and the highest and lowest months.\n";
	for (int count = 0; count < NUM_MONTHS; count++)
	{
		months[count]= getMonthTotal(); 
	}
	yearTotal = getTotal(months, NUM_MONTHS);
	average = findAverage(months, NUM_MONTHS); 
	highest = findHighest(months, NUM_MONTHS, index); 
	lowest = findLowest(months, NUM_MONTHS, index); 

	cout << "Total Rainfall for year: \t" << yearTotal << endl; 
	cout << "Average Monthly rainfall: \t" << average <<endl; 
	cout << "Least rainfall was in month: \t"<<lowest << endl;
	cout << "Most rainfall occured in month \t" <<highest <<endl;

	system("pause");
}
double getMonthTotal()
{
	double monthtotal;
	cout << "\nPlease enter a total: ";
	cin >> monthtotal;
	if (monthtotal < 0)
	{ cout <<"Invalid input\n";
		getMonthTotal();
	}
	return monthtotal;
}
double getTotal(double month[], int num_months) 
{
	double total = 0;
	for (int counter = 0; counter < num_months; counter++)
	{
		total += month[counter];
	}
	return total;
}
double findAverage( double month[], int num_months) 
{
	double total = 0;
	for (int counter = 0; counter < num_months; counter++)
	{
		total += month[counter];
	}
	double average = (total / num_months);
	return average;
}
double findHighest(double month[], int num_months, int& index) 
{
	int highest = month[0];
	index = 0;
	for (int count = 0; count < num_months; count++)
	{
			if (month[count] > highest)
			highest = month[count];
			index = count + 1;
	}
	return index;
}
double findLowest(double month[], int num_months, int& index) 
{
	int lowest = month[0];
	index = 0;
	for (int count = 0; count < num_months; count++)
	{
		if (month[count] < lowest)
			lowest = month[count];
			index = count + 1;
	}
	return index;
}
This line:
index = count + 1;

You are incrementing the variable 'index', but not using it to index into your array, therefore you'll always increment to 12 and return this.

Also, is there any reason you're writing most of this in C? e.g. unsafe c-style arrays?
First a note, some things could be simple: average = yearTotal / NUM_MONTHS;

The line 44. You don't update the monthtotal.

Highest and lowest, why index = count + 1;? Why return index as double? The index is a reference parameter too.

Index is 12, because lines 75 and 87 are not within the IF.

Make an array of strings for the names. Then you can use index to dereference an element to get the name.
Topic archived. No new replies allowed.