Help with vectors

P

Protoman

Hi!!! I need to write a program in which the user defines how many
params a mean calculating function uses; here's what I have
#include <iostream>
#include <cstdlib>
#include <vector>
#include <numeric>
using namespace std;

const long double& mean(void)
{
vector<int> v;
for(int i= 1; i <=3; ++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;
}

Can you help me; I want it to work like this:

Enter the number of numbers to average: 10
Enter those 10 numbers:
1 2 3 4 5 6 7 8 9 10
Here's the mean: 5.5
Continue[1=yes|0=no]: 0
Exiting...

Thanks for the help!!!
 
J

Jim Langston

Protoman said:
Hi!!! I need to write a program in which the user defines how many
params a mean calculating function uses; here's what I have
#include <iostream>
#include <cstdlib>
#include <vector>
#include <numeric>
using namespace std;

const long double& mean(void)
{
vector<int> v;
for(int i= 1; i <=3; ++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;
}

Can you help me; I want it to work like this:

Enter the number of numbers to average: 10
Enter those 10 numbers:
1 2 3 4 5 6 7 8 9 10
Here's the mean: 5.5
Continue[1=yes|0=no]: 0
Exiting...

Thanks for the help!!!

This sounds like homework, but it looks like you're trying, so I'll give you
some pointers.

First you need some method to get the values to take the mean average of.

std::cin >> somevar;

would get a number.

int MyInt;
std::cin >> MyInt;

to input an int.

Now you would push that number onto your vector.

Also, you probably want to declare your vector in your main, then pass it by
reference to your mean function.

Now, your terminology is off. You dont' want the user to declare how many
parameters, but how many values.

See if you can figure it out from this. If you need more help post your new
code.
 
J

Jim Langston

Jim Langston said:
Protoman said:
Hi!!! I need to write a program in which the user defines how many
params a mean calculating function uses; here's what I have
#include <iostream>
#include <cstdlib>
#include <vector>
#include <numeric>
using namespace std;

const long double& mean(void)
{
vector<int> v;
for(int i= 1; i <=3; ++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;
}

Can you help me; I want it to work like this:

Enter the number of numbers to average: 10
Enter those 10 numbers:
1 2 3 4 5 6 7 8 9 10
Here's the mean: 5.5
Continue[1=yes|0=no]: 0
Exiting...

Thanks for the help!!!

This sounds like homework, but it looks like you're trying, so I'll give
you some pointers.

First you need some method to get the values to take the mean average of.

std::cin >> somevar;

would get a number.

int MyInt;
std::cin >> MyInt;

to input an int.

Now you would push that number onto your vector.

Also, you probably want to declare your vector in your main, then pass it
by reference to your mean function.

Now, your terminology is off. You dont' want the user to declare how many
parameters, but how many values.

See if you can figure it out from this. If you need more help post your
new code.

Oh gawd, did I actually reply to protoman the troll?

People,ignore this guy. So far he's claimed to be a university professor,
someone working for NASA and now he' works for the NSA. Seems he's gone
back to school as a student now.
 
P

Protoman

OK, I admit; I'm lying. I just like to make up huge stories. I'm just a
high school freshmen; Just please help me; and could you kindly give me
a list of all of the member functions for vectors. You can yell at me
for lying if you'd like. Really, really sorry :-(
 
J

John Harrison

Protoman said:
OK, I admit; I'm lying. I just like to make up huge stories. I'm just a
high school freshmen; Just please help me; and could you kindly give me
a list of all of the member functions for vectors. You can yell at me
for lying if you'd like. Really, really sorry :-(

You can get the member functions for vector (and everything else) here

http://www.dinkumware.com/refxcpp.html

john
 
P

Protoman

Thanks!!! And thanks for not calling me a troll anymore!!! What does
troll mean, anyway?
So, could you give me a piece of code to add up all the elements in a
vector. Thanks!!!
 
J

John Harrison

Protoman said:
Thanks!!! And thanks for not calling me a troll anymore!!! What does
troll mean, anyway?

Someone who posts just to wind others up, but I never called you a
troll. All the fantasy stuff was amusing once I realised what it was.
So, could you give me a piece of code to add up all the elements in a
vector. Thanks!!!

If vec is your vector then

double total = 0.0;
for (int i = 0; i < vec.size(); ++i)
total += vec;

john
 
?

=?ISO-8859-1?Q?Stefan_N=E4we?=

John said:
Protoman said:
Thanks!!! And thanks for not calling me a troll anymore!!! What does
troll mean, anyway?


Someone who posts just to wind others up, but I never called you a
troll. All the fantasy stuff was amusing once I realised what it was.
So, could you give me a piece of code to add up all the elements in a
vector. Thanks!!!

If vec is your vector then

double total = 0.0;
for (int i = 0; i < vec.size(); ++i)
total += vec;


double total = std::accumulate(vec.begin(), vec.end(), 0.0);


Stefan
 
P

Protoman

OK, now it's saying my return variable, ret, is undeclared and some
other stuff; here's my new program:

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

long double& mean(long long& x)
{
vector<long long> v(x);
for(long long i=1;i<=x;i++)
{
cout << "Enter a number: ";
long long num;
cin >> num;
v.push_back(x);
}
long double ret=(accumulate(v.begin(),v.end(), 0.0)/v.size());
return ret;
}

int main()
{
for(;;)
{
cout << "Enter a number: " << endl;
long long num;
cin >> num;
long double mean=mean(num);
cout << "Here's the mean: " << fixed << ret << endl;
}
system("PAUSE");
return 0;
}

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.
 
P

Protoman

OK here's my new code:

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

long double& mean(long long& x)
{
vector<long long> v(x);
for(long long i=1;i<=x;i++)
{
cout << "Enter a number: ";
long long num;
cin >> num;
v.push_back(x);
}
long double ret=(accumulate(v.begin(),v.end(), 0.0)/v.size());
return ret;
}

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

Now, when I entered three and five, I got one as the answer; could you
point out where the problem is for me; then I'll fix it. And could you
tell me how to rewrite it so the main program can enter the data, and
mean() can do just what it has to, average the data in the vector?
Thanks for the help!!!
 
M

Mark P

Protoman said:
OK, now it's saying my return variable, ret, is undeclared and some
other stuff; here's my new program:

What other stuff? The more you provide the more we can help.
#include <iostream>
#include <cstdlib>
#include <vector>
#include <numeric>
using namespace std;

long double& mean(long long& x)

Why are you using long long and long double? What's wrong with int and
double? Also there's really no reason to pass the input as a reference
and returning a reference as you've done is downright dangerous since it
refers to a local variable which will go out of scope when the function
returns.
{
vector<long long> v(x);

Bad idea. You've now created a vector v with x objects already in it.
Since you want the vector to start empty, use the default constructor.
The vector will grow as you push_back elements.
for(long long i=1;i<=x;i++)

Almost certainly no reason for long long here as a counter. A vector
may not even be able to hold as many elements as the range of long long.
{
cout << "Enter a number: ";
long long num;
cin >> num;
v.push_back(x);
}
long double ret=(accumulate(v.begin(),v.end(), 0.0)/v.size());

Using v.size() is not very efficient in general and it's also error
prone in your original code where the size would now be 2x (for x
elements via the constructor, and x more that you've inserted). If you
know that v.size() is x, just use x.
return ret;
}

int main()
{
for(;;)
{
cout << "Enter a number: " << endl;
long long num;
cin >> num;
long double mean=mean(num);

You can't define a variable with the same name as the function. Pick
something else, like "avg".
cout << "Here's the mean: " << fixed << ret << endl;

ret? Who's ret? There's no variable named ret declared in this scope.
You probably meant to use the value returned by the function mean
which, if you follow my advice above, would be "avg".
}
system("PAUSE");
return 0;
}

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.

Make it work first. Worry about that later. For a program this size
there's not much point in making it more modular anyway.

Mark
 
M

Mark P

Protoman said:
OK here's my new code:

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

long double& mean(long long& x)
{
vector<long long> v(x);
for(long long i=1;i<=x;i++)
{
cout << "Enter a number: ";
long long num;
cin >> num;
v.push_back(x);
}
long double ret=(accumulate(v.begin(),v.end(), 0.0)/v.size());
return ret;
}

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

Now, when I entered three and five, I got one as the answer; could you
point out where the problem is for me; then I'll fix it.

(0 + 0 + 3 + 5) / 4 = 2

See my other reply for details.

Mark
 
P

Protoman

OK, here's my new code(it works!!! Praise God!!!!):

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

long double mean(int x)
{
vector<int> v;
for(int i=0;i<x;++i)
{
cout << "Enter a number: ";
long double num;
cin >> num;
v.push_back(num);
}
long double ret=(accumulate(v.begin(),v.end(), 0.0)/x);
return ret;
}

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

Now, how do I rewrite it so that main does the inputting, not mean?
 
P

Protoman

OK here's a better version of the above:

#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;
}

How do I make this to be more moduaralized and efficient? Thanks for
the help!!!
 
G

George Huber

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
 
M

Markus Becker

["Followup-To:" nach comp.lang.c++ gesetzt.]

George Huber said:
5. The infinite for-loop that you start on line (022) has no way of
termination

That's a limitation most infinite loops have to deal with! ;-)
But: it _can_ be terminated by an exception.
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.

It's not about confusing the compiler.
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?

Yes, what about it? I'm not sure I understand your comment, no, I'm sure
I don't understand your comment.

Markus
 
G

George Huber

Markus said:
["Followup-To:" nach comp.lang.c++ gesetzt.]

Been awhile from the original post, I am not sure I remember everything...
That's a limitation most infinite loops have to deal with! ;-)
But: it _can_ be terminated by an exception.

quite correct - every infinite loop construct, i.e. while(1){...}
does need some why break out of the loop. As I recall I did not see
any method to exit the loop.
It's not about confusing the compiler.

just do not remember enough to comment on this, sorry.
Yes, what about it? I'm not sure I understand your comment, no, I'm sure
I don't understand your comment.

as I recall, I was making the point that you should always validate user
input. I believe that the user input was the number of inputs that
was going to be entered. My comment was an attempt to get you to
think about user validation and what should your program do.

- what should your program do if the use says that he want to
calculate the mean / median of zero numbers? could this be used
as a sentinel to exit out of the infinite loop? eg:

while(1)
{
cout << "Enter the number of entries: ";
cin >> count;

if(0 == count)
break;
else
// do other things here...
}

- what should your program do if the user says that he wants to
calculate the mean / median of a negative number (say -5)? Now
to us, this is probably a non-sensical input - who would ever
do this - right? Well a careless user could accidentally type
in a negative number, and my belief is that the program should
catch these errors and recover gracefully. In addition, a
malicious user might be able to use something like this to hack
the system.

Now granted your average program, due to what it is doing, has almost no
chance to do this. But what if you were writing something more
critical. My believe is that programmers need to be taught from their
first course on some of the fundamentals of secure programming, such as
validation of user input.

George
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top