Hello ladies and gents,

Had to make the following exercise in Accelerated C++

Write a program to calculate the average of the numbers stored in a vector<double>.

THis was my solution:

#include <iostream>
#include <iomanip>
#include <vector>

using std::cin;			using std::istream;
using std::cout;		using std::setprecision;
using std::endl;		using std::vector;				

istream& vec(istream&, vector<double>&);
double median(vector<double>&);

int main()
{
	vector<double> numbers;

	cout << "Fill the vector with double values. To quit, press Ctrl 'z'." << endl;
	vec(cin, numbers);

	cout << "The average of these numbers is " << setprecision(2) << median(numbers) << endl;

	cin.ignore(1, '\0');
		
	return 0;
}

istream& vec(istream& is, vector<double>& number)
{
	double numb;

	while (is >> numb)
		number.push_back(numb);
	cin.clear();

	return is;
}

double median(vector<double>& nr)
{
	double average = 0.0;
	
	for (int i = 0; i < nr.size();++i)
		average += nr[i];

	return average/nr.size();
}

Would you change anything in it, and if so, why?

Thanks.

apart from renaming the function "median" to "average" to better reflect its function?
And of course renaming the local variable "average" to something like "sum" to reflect its function.
What's the reason for returning the stream from your "vec" function when you then discard the darn thing?

Thanks for the c/c jwenting, Ive changed those know.

I used the median function name because I wanted to use the median to find the average, didn't succeed in using it though, so, had to find another way.

Changed the return from vec to void.

To avoid the for loop, I would use the std function std::accumulate:

double average;
average = std::accumulate(nr.begin(), nr.end(), 0)/nr.size();

Check the function documentation for more details ;)

Best,

commented: Don't bump old threads -1
commented: Counter-neg +15

@ pvfloripa:
Do you think that after 4 years the OP is still in search of a "best" solution? Also this thread was marked solved!!
You are new to daniweb, so in future keep this in mind - you should not post in very old threads (and this one was 4 years old) unless its absolutely necessary.
Read the rules for once at least and try to stick to them. It make things a lot easier.
Good luck.

@NP-Complete: I disagree.
There's a reason why we don't automatically close threads after x time and that's because something useful might be added. This is very rarely the case I agree, but the reply from pvfloripa is useful.

We don't allow the "hijacking" of thread by asking another question in someone else's thread, but bumping with useful information is perfectly fine.
Please don't scare away new posters by slapping them with rules that aren't there. Thanks.

My apologies to pvfloripa for the misunderstanding.

commented: :) +15

my apologies too...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.