Function that calculates the arithmetic mean using vectors?

P

Protoman

I have a function that calculates the mean of the some numbers; I need
it to accept any number of parameters (one needs to be the number of
the other parameters) Can you help me out here? Here's the function:

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

I want it to calculate the vector size at runtime. If you could help me
out, that'd be great. Thanks!!!
 
V

Victor Bazarov

Protoman said:
I have a function that calculates the mean of the some numbers; I need
it to accept any number of parameters (one needs to be the number of
the other parameters) Can you help me out here?

You need to see an implementation of 'fprintf'. va_args and va_list
are the macros to be used. Search the web for an example of how to
use them.

V
 
V

Victor Bazarov

Protoman said:
I have a function that calculates the mean of the some numbers; I need
it to accept any number of parameters (one needs to be the number of
the other parameters) Can you help me out here? [..]

'va_start', 'va_end', and 'va_list' are the macros you should read
about. The web is your friend.
 
K

Karl Heinz Buchegger

Protoman said:
I want to use vectors though.

Pass the vector to the function.
Handling user input surely is not the responsibility
of a function which calculates a mean.

long double mean( vector< int > values )
{
// now calculate the mean of the passed
// vector and return it
}

One function - one responsibility
 
P

Protoman

Here's my rewritten program(it finally works!!!!):

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

long double mean(vector<long double>& v)
{
long double ret=(accumulate(v.begin(),v.end(), 0.0)/v.size());
return ret;
}


int main()
{
for(;;)
{
cout << "Enter the number of items: " << endl;
int num;
cin >> num;
vector<long double> v;
for(int i=0;i<num;++i)
{
cout << "Enter a number: ";
long double temp;
cin >> temp;
v.push_back(temp);
}
long double ret=mean(v);
cout << "Here's the mean: " << fixed << ret << endl;
}
system("PAUSE");
return 0;
}

Now, how would I rewrite to make it more efficient and modularized; I'd
like some ideas. Thanks for the help!!!
 
K

Karl Heinz Buchegger

Protoman said:
Here's my rewritten program(it finally works!!!!):

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

long double mean(vector<long double>& v)
{
long double ret=(accumulate(v.begin(),v.end(), 0.0)/v.size());
return ret;
}

int main()
{
for(;;)
{
cout << "Enter the number of items: " << endl;
int num;
cin >> num;
vector<long double> v;
for(int i=0;i<num;++i)
{
cout << "Enter a number: ";
long double temp;
cin >> temp;
v.push_back(temp);
}
long double ret=mean(v);
cout << "Here's the mean: " << fixed << ret << endl;
}
system("PAUSE");
return 0;
}

I don't see, how the outermost for loop is ever terminated.
How about: If the user enters 0 for the number of items, the program
terminates.

<sidenote>
Did you format your code in that way or did that happen during the posting.
If you formatted that code, then work on your formatting style. The above
is awefull to read. If that formatting happend during posting, get a better
newsreader. If your programs get longer, no one here will be willing to wade
through such unformatted text
Now, how would I rewrite to make it more efficient and modularized; I'd
like some ideas. Thanks for the help!!!

Eg. move the whole input thing into a function of its own.
You might also want to try the following: When the program asks
for a number, enter 'a' and see what happens. I am trying to push you
into the direction of having a function which does just the task of entering
a number, but it does so with handling all possible errors and probably retrying
the input, until a valid input has been received.

Glad to see that you switched your attitude and we can talk normal with each
other. I am pretty sure that with that attitude you made it into some killfiles
and it will take some time to get out of there. You want all the help you can get,
so beeing in other peoples killfile isn't something you want to be.
 
P

Protoman

OK, here we go, inputting's a seperate function now:

#include <iostream>
#include <cstdlib>
#include <vector>
#include <numeric>
using namespace std;
void input(vector<long double>& v)
{
cout << "Enter the number of items: " << endl;
int num;
cin >> num;
for(int i=0;i<num;++i)
{
cout << "Enter a number: ";
long double temp;
cin >> temp;
v.push_back(temp);
}
}

long double mean(vector<long double>& v)
{
input(v);
long double ret=(accumulate(v.begin(),v.end(), 0.0)/v.size());
return ret;
}

int main()
{
vector<long double> v;
for(;;)
{
long double ret=mean(v);
cout << "Here's the mean: " << fixed << ret << endl;
bool cont;
cout << "Continue?[1=yes|0=no]: " << endl;
cin >> cont;
if(cont==true)continue;else break;
}
system("PAUSE");
return 0;
}

Next time I'll just post snips of my code. Now how to I do the error
checking? Thanks for the help!!!
 
P

Protoman

Oh, and I think there's a function in cstdlib that terminates the
program; could you tell me what that function is?
 
J

John Harrison

Protoman said:
Oh, and I think there's a function in cstdlib that terminates the
program; could you tell me what that function is?

exit and abort both do that (in different ways).

john
 
P

Protoman

OK, how do I test if my variables, num and temp, are characters, like a
or b? I need to make my code idiot proof.
 
J

John Harrison

Protoman said:
OK, how do I test if my variables, num and temp, are characters, like a
or b? I need to make my code idiot proof.

This question gets asked a lot. If you search the archive you'll get
lots of answers.

The only perfectly idiot proof method is to read a line into string,
check the string is in the correct form, and then convert to a number.

For a very nearly as good method see the 'resurrecting cin' thread by
float_dublin (c.l.c++ only). The full method doesn't come out until the
end so be prepared to read some less than complete posts (including one
by me).

john
 
J

John Harrison

John said:
This question gets asked a lot. If you search the archive you'll get
lots of answers.

The only perfectly idiot proof method is to read a line into string,
check the string is in the correct form, and then convert to a number.

For a very nearly as good method see the 'resurrecting cin' thread by
float_dublin (c.l.c++ only). The full method doesn't come out until the
end so be prepared to read some less than complete posts (including one
by me).

john

For a bonus point, see if you can work out what the incorrect input is
that the 'resurrecting cin' method will accept, but the perfect method
will reject.

john
 
P

Protoman

OK, here's the idiot proof input funct:

template<class T>
void input(vector<T>& v)
{
cout << "Enter the number of items: " << endl;
int num;
cin >> num;
if(num==0)
abort();
else if(!cin)
abort();
else
for(int i=0;i<num;++i)
{
cout << "Enter a number: ";
T temp;
cin >> temp;
if(!cin)
abort();
else
v.push_back(temp);
}
}

Any other suggestions for me?
 
J

John Harrison

Protoman said:
OK, here's the idiot proof input funct:

template<class T>
void input(vector<T>& v)
{
cout << "Enter the number of items: " << endl;
int num;
cin >> num;
if(num==0)
abort();
else if(!cin)
abort();
else
for(int i=0;i<num;++i)
{
cout << "Enter a number: ";
T temp;
cin >> temp;
if(!cin)
abort();
else
v.push_back(temp);
}
}

Any other suggestions for me?

It works, but the idiots aren't going to be very happy when the program
aborts. I would have suggested some sort of error recovery, 'Hey idiot!
Please enter the number again', that kind of thing.

john
 
B

BobR

Protoman wrote in message
Oh, and I think there's a function in cstdlib that terminates the
program; could you tell me what that function is?

If you can read this NG, you can read the docs for stdlib. <G>

You are thinking of 'exit()', BUT, don't use that for normal program
termination. It's intended as an 'emergency exit' (it's possible to shoot
yourself in the foot - or worse!).
All you need to do in your 'for(;;)' loop is execute 'break;', like you are
doing, and the 'main()' will finish, terminating your program normally.
You might find 'while(condition){}' a little cleaner/clearer than 'for(;;)'
(like Mr. Huber showed you.).

For your error checking question; look up 'isalpha' and 'isdigit' for an
idea.

What book are you using?

You might find this site interesting.
www.BruceEckel.com -> Books -> "Thinking in C++, vol. 1"
(since it only costs $(a download))
Two heads are better than one, and so are two books!

alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/

Hope that helps. Good luck.
 
P

Protoman

OK here's the input function that doesn't abort on bad data:

template<class T>
void input(vector<T>& v)
{
start:
cout << "Enter the number of items: " << endl;
int num;
cin >> num;
if(num==0)
{
cout << "Try again, with a number this time ";
goto start;
}
else if(!cin)
{
cout << "Try again, with a number this time ";
goto start;
}
else
for(int i=0;i<num;++i)
{
start2:
cout << "Enter a number: ";
T temp;
cin >> temp;
if(!cin)
{
cout << "Try again, with a number this time ";
goto start2;
}
else
v.push_back(temp);
}
}

But it keeps displaying the same message over and over again; what's
wrong? Thanks for the help!!! BTW, I use Learn C++ in 21 Days and
Standard C++ Bible. And my teacher told me there's a book that tells
you everything you can and can't do in C++. Could you tell me the name
of that book please? Thanks!!!
 
J

John Harrison

Protoman said:
OK here's the input function that doesn't abort on bad data:

template<class T>
void input(vector<T>& v)
{
start:
cout << "Enter the number of items: " << endl;
int num;
cin >> num;
if(num==0)
{
cout << "Try again, with a number this time ";
goto start;
}
else if(!cin)
{
cout << "Try again, with a number this time ";
goto start;
}
else
for(int i=0;i<num;++i)
{
start2:
cout << "Enter a number: ";
T temp;
cin >> temp;
if(!cin)
{
cout << "Try again, with a number this time ";
goto start2;
}
else
v.push_back(temp);
}
}

But it keeps displaying the same message over and over again; what's
wrong? Thanks for the help!!! BTW, I use Learn C++ in 21 Days and
Standard C++ Bible. And my teacher told me there's a book that tells
you everything you can and can't do in C++. Could you tell me the name
of that book please? Thanks!!!

There is nothing that cannot be done in C++, it is a general purpose
language. Of course there are somethings that are easy to do and some
that are difficult, compared to other languages.

As already advised read the thread 'resurrecting cin' on c.l.c++. It is
about exactly this topic. If you what to research this issue see if your
books say something about iostreams and error state.

Try and develop the skills to research answers to your own questions, at
the moment you want to be spoon fed.

john
 
P

Protoman

No, that book I was talking about, it tells you what the syntax
permits, not the language itself. I'm just going to print an error
message, then abort. Saves me the trouble. And what would "COBOL style
C++" look like?
 
J

John Harrison

Protoman said:
No, that book I was talking about, it tells you what the syntax
permits, not the language itself. I'm just going to print an error
message, then abort. Saves me the trouble. And what would "COBOL style
C++" look like?

I have no idea.

john
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top