Vectors!!!

Dec 15, 2009 at 6:30am
i have to write a program that(prompt the user to enter them one at a time) an unspecified number of temperatures in Celcius from the keyboard. Add each value to a vector created using the STL with the push_back function. Instruct the user to enter an invalid temperature (<-273) to indicate that they are done entering values. Use STL functions and algorithms to find the max, min, sum, and count (number of values) and display the results.Also sort the temperatures in increasing order and display the values.

Here is what i have so far"

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
int main()
{
vector<double> temps;
vector<double>::iterator iter;
//ostream_iterator<double> screen_output (cout, " ");
//double degrees,sum,max,min,count;


while(degrees < -273)
{
cout << "Enter the temperature values: ";
cin >> degrees;
temps.push_back(degrees);
}

iter = max_element(temps.begin(),temps.end());
cout << "The max temperature is: " << *iter << endl;
//sum = accumulate (temps.begin(),temps.end(),0);
//cout << "The sum of the temperatures is: " << sum << endl;

system("pause");
return 0;
}
Please help any1
Dec 15, 2009 at 6:46am
I edited yours a little. Changed how the while loop works for entering degrees.

And showed an example of how to use your functions. It's not a great way to do it but. See if you can clean it and do the rest.

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

int main()
{
	vector<double> temps;
	vector<double>::iterator iter;
	double degrees = 0;
	double sum = 0;
	double max = 0;
	double min = 0;
	double count = 0;

	while(degrees > -273 && degrees < 273)
	{
		cout << "Enter the temperature values: ";
		cin >> degrees;
		if(degrees > -273 && degrees < 273)
		{
			temps.push_back(degrees);
		}
	} 

	iter = max_element(temps.begin(),temps.end());
	max = *iter;
	cout << "Max temp = " << max << endl;

	sum = accumulate(temps.begin(),temps.end(),0);
	cout << "The sum of the temperatures is: " << sum << endl;

	system("pause");
	return 0;
}
Dec 15, 2009 at 12:57pm
mythios tyvm for responding to this. however, the max works but the sum doesn't gives me an error message of

C:\Dev-Cpp\include\c++\3.4.2\bits\stl_numeric.h In function `_Tp std::accumulate(_InputIterator, _InputIterator, _Tp) [with _InputIterator = __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, _Tp = int]':

43 K:\EGR125\HW5Part5.cpp instantiated from here
89 C:\Dev-Cpp\include\c++\3.4.2\bits\stl_numeric.h [Warning] converting to `int' from `double'

line 43 is the sum=accumulate one, but here is the thing, there is no line 89 so what exactly am converting int from double?
Dec 15, 2009 at 7:44pm
accumulate is a template that synthesizes its return type from the type of its last parameter.
You are passing an int (0). Therefore, the function is attempting to return int. But your
iterators are iterators to doubles, so somewhere in there it is adding together an int and
a double and storing the result in an int.

sum = accumulate( temps.begin(), temps.end(), 0.0 );
Dec 16, 2009 at 3:17am
tyvm you guys rock!!
Topic archived. No new replies allowed.