Is this program right?

V

Victor Bazarov

Protoman said:
Why are you doing this to me?!!!!!!!!!!!? I'm just asking if you know
if Borders *stocks* that book?!!!!?

Are you kidding? What is this, a kindergarten? Have you heard of
"The Web"? If so, visit www.bordersstores.com and search their
inventory. If you haven't, call (888) 81-BOOKS (-26657) and ask
them. Why do you want _us_ to do it for you? And yes, in most
cases when it comes to well-known programming books, Borders does
stock them, although of course I would be guessing without doing
the actual search or talking to them...
 
P

Protoman

OK, here's what I got:

#include <iostream>
#include <cstdlib>
#include <vector>
#include <numeric>
using namespace std;

const long double& mean(void)
{
vector<int> v;
for(int i= 1; i <= 10; ++i)
{
v.push_back(i);
}
const long double ret=accumulate(v.begin(), v.end(), 0.0) / v.size();
return ret;
}
int main()
{
cout << "Mean: " << mean() << endl;
system("PAUSE");
return 0;
}

OK now how do I let it accept user input and let the user determine the
number of parameters?
 
V

Victor Bazarov

Protoman said:
OK, here's what I got:

#include <iostream>
#include <cstdlib>
#include <vector>
#include <numeric>
using namespace std;

const long double& mean(void)
{
vector<int> v;
for(int i= 1; i <= 10; ++i)
{
v.push_back(i);
}
const long double ret=accumulate(v.begin(), v.end(), 0.0) / v.size();
return ret;

This is a cause for undefined behaviour. You're returning a reference
to a local variable. Why don't you simply return by _value_?
}
int main()
{
cout << "Mean: " << mean() << endl;
system("PAUSE");
return 0;
}

OK now how do I let it accept user input and let the user determine
the number of parameters?

What does your favourite C++ book say? You've successfully used 'cout',
now it's time to try using 'cin'. All possible examples are in any book
that even remotely considered decent.

V
 
H

hacker++

Protoman said:
But how do I store the input in a vector and iterate through it?

Did you ever read a C++ book and try doing the above yourself?
Surely there must be a C++ book at the NASA library.....
 
K

Kai-Uwe Bux

Protoman said:
But how do I store the input in a vector and iterate through it?

A classical idiom would be:


int dummy;
while ( std::cin >> dummy ) {
vector_variable.push_back( dummy );
}

Now, you have a vector and can operate on that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top