Protoman said:
#include <iostream>
#include <cstdlib>
#include <vector>
#include <numeric>
using namespace std;
(007) long double& mean(long long& x)
(008) {
(009) vector<long long> v(x);
(010) for(long long i=1;i<=x;i++)
(011) {
(012) cout << "Enter a number: ";
(013) long long num;
(014) cin >> num;
(015) v.push_back(x);
(016) }
(017) long double ret=(accumulate(v.begin(),v.end(), 0.0)/v.size());
(018) return ret;
(019) }
Just a few things here;
1. I am assuming it is a typo in line (015) and you mean
`v.push_back(num)'
2. What the types of long long and long double? int and double should
be just fine - unless you are dealing with a large amount of data.
Also, consider using an unsigned type (i.e. unsigned int) for x
because x is an ordinal and should never be negative. Probably a
user error if x is negative.
3. Why pass x by reference? It is not being modified in the function.
You may also want to make x constant because the function should not
be changing it.
4. As written, v.size() is dangerous. You already allocate space of
x-elements in v, and then push another x-elements onto the array.
You know the size so just use it.
(020) int main()
(021) {
(022) for(;
(023) {
(024) cout << "Enter a number: " << endl;
(025) long long num;
(026) cin >> num;
(027) long double mean=mean(num);
(028) cout << "Here's the mean: " << fixed << ret << endl;
(029) }
(030) system("PAUSE");
(031) return 0;
(032) }
and some more things,
5. The infinite for-loop that you start on line (022) has no way of
termination
6. Notice in line (027) you have a variable named `mean' and are
calling a function named `mean' - this is a sure way to really
confuse you compiler.
7. While the program is simple, there is no error checking or
user-input validation. There are many that will tell you that at
your stage in learning these things are not necessary and only serve
to confuse the issues (and they may be correct). However, I think it
is important for you to start thinking about these concepts as soon
as possible, for example.
a. what if a user entered a negative number in line (026) of you
program -- what would the effect be? What should you do if
a user does enter a negative number? what about a zero?
what about a one?
b. what if a user enters a non-integer for any of your inputs?
what does the program do?
Note, at your stage of learning you might not be able to program
defensively in these cases, but I feel it is important for you to
know what will happen, and as you progress in you studies search for
ways to handle these cases.
Oh, and can you help me design my program a bit more, uh, for a lack of
a better word, more "modularly"; like one function to get the inputs
and store it in a vector, and then pass the vector to my mean()
function.
Consider this, lets assume that you have two functions:
double mean(vector<int>&); // calculates the mean of a vector
bool getInput(vector<int>&); // gets user input
With these, you could write you program as:
int main(int argc, char** argv)
{
bool run = true;
int count
vector<int> v;
while(run)
{
cout << "Enter the number of elements, 0 to exit" << endl;
cin >> count;
if(count < 0)
{
// user error - do something
}
else if(count == 0)
{
run = false;
}
else
{
getInput(v);
cout << "The mean is: " << mean(v) << endl;
}
}
return 0;
}
George