Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is this program right?

Reply
Thread Tools

Is this program right?

 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-13-2005
Protoman wrote:
> 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 (88 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...


 
Reply With Quote
 
 
 
 
Protoman
Guest
Posts: n/a
 
      09-13-2005
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?

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-14-2005
Protoman wrote:
> 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


 
Reply With Quote
 
Protoman
Guest
Posts: n/a
 
      09-14-2005
But how do I store the input in a vector and iterate through it?

 
Reply With Quote
 
hacker++
Guest
Posts: n/a
 
      09-14-2005
Protoman wrote:
> 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.....

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      09-14-2005
Protoman wrote:

> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to javac a java program w/ another java program which is w/o a main method cjeffwang@yahoo.com Java 1 10-31-2005 04:25 AM
System program/ Application program ?? Parvsandhu Java 2 07-11-2005 09:08 AM
how to convert a java program to an exe program ola Java 3 02-16-2004 09:42 AM
Calling Java program in another Java program Rey Java 4 12-12-2003 10:18 PM
passing data between Java program and C program--help pipi Java 1 07-21-2003 05:02 AM



Advertisments