Finding average Temperatures w/ Vectors

I've been working on this project for a while and cannot figure out what to do next. I'm trying to ask the user to put their values into a vector called temps using a for loop. After that, I'm trying to use a function called average_vector to find the average of the values put in. Finally, it'll print out this average. Thanks.

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
#include <iostream>
#include <vector>
using namespace std;

double average_vector(vector <double> average)
{
	double average = 0;
	for(int m = 0; m<= 11; m++)
			{
				average = average/average[m];
			}
	return average;
}

int main()
{
	vector <double> temps;
	int answer;
	int monthtemp;
	int yeartemp;
	double sum = 0;

	cout << "Please enter: ";
	cout << "\n\t1 To calculate the average yearly temperature.";
	cout << "\n\t2 To calculate the average temperature for a decade.";
	cout << "\n\t3 To quit.";
	cout << "\nEnter number now: ";
	cin >> answer;
	do
	{
		if(answer == 1)
		{
			for(int m = 0; m <= 11; m++)
			{
				cout << "Please enter the average temp for month " << m + 1 << ": ";
				cin >> monthtemp;
				temps.push_back(monthtemp);
				cout << "\n";
				
				average_vector(temps);
			}
			
			cout << sum;
		}
		else if(answer == 2)
		{
			for(int m = 0; m <= 9; m++)
			{
				cout << "Please enter the average temp for year " << m + 1 << ": ";
				cin >> yeartemp;
				temps.push_back(yeartemp);
				cout << "\n";
				average_vector(temps);
			}
			cout << sum;
		}
		else if(answer == 3)
		{
			cout << "I said GOOD DAY, Sir!";
			break;
		}
		else
		{
			cout << "Input error! Try again: ";
			cin >> answer;
		}
	}
	while(answer != 1 || 2 || 3);
		
	system("pause");
}
(1) Give different names to variables in your first function:
1
2
double average_vector(vector <double> temps)
double average = 0;


(2) Use a reference in your first function so that a copy of the vector is not made when called.
double average_vector(vector <double> &temps)

(3) Use an iterator in your first function to sum each element:

1
2
3
4
 vector<double>::const_iterator it;
for (it = temp.begin(); it != temp.end(); ++it)
  sum += *it;
return sum/temp.size();

Add detection of temp.size() == 0 so there is no divide by zero.
Last edited on
So the first function should look like this?
1
2
3
4
5
6
7
8
9
10
double average_vector(vector <double> &temps)
{
	double sum = 0;
	vector<double>::const_iterator it;
	for (it = temps.begin(); it != temps.end(); ++it)
	{
		sum += *it;
	}
	return sum/temps.size();
}
Last edited on
You can also use std::accumulate http://www.cplusplus.com/reference/numeric/accumulate/?kw=accumulate http://ideone.com/5po8Z7

Also, this while(answer != 1 || 2 || 3); is a problem. If answer is not 1 the statement evaluates to while(false || true || true)
Add:
if (temps.size() == 0) return 0.0;
somewhere to avoid divide by zero.
Topic archived. No new replies allowed.