Vectors- storing integers and calculating mean

I am trying to store integers in a vector. The integers will be provided by the user. I cannot get the vector to keep the integers because every time I print it they are all zeros. Then I am trying to take the mean of the values that the user input into the vector. Just trying to figure out how to store the data there to do some math with.

#include <iostream>
#include <vector>
#include <cmath>
#include <iterator>

using namespace std;

int fillVector(vector<int>&, int input, int user_ints);
void printVector(vector<int>& arbNums);
double meanFunc(vector<int>&, int input, int user_ints);

int main()
{
int user_input;
int user_integers;
double avg;
vector<int> v(user_input);

cout << "How many integers do you want in the vector?" << endl;
cin >> user_input;

fillVector(v, user_input, user_integers);
printVector(v);

avg = meanFunc(v, user_input, user_integers);

cout << "here" << endl;
cout << "The average of the vector is: " << avg << endl;
}

int fillVector(vector<int>&, int input, int user_ints)
{
vector<int> arbNums(input);

for (int i = 0; i < input; i++)
{
cout << "Enter integers: " << endl;
cin >> user_ints;

arbNums.push_back(user_ints);
/*cout << user_ints << " added.";
cout << "size is: " << arbNums.size() << endl;
cin >> user_ints;*/


/*cout << "You entered: ";
for (unsigned int i = 0; i < arbNums.size(); i++)
{
cout << arbNums[i] << " " << endl;
}*/

//cout << arbNums.size(i) << endl;
}
return 0;
}

void printVector(vector<int>& arbNums)
{
for (int i = 0; i < arbNums.size(); i++)
{
cout << arbNums[i] << endl;
}
}

double meanFunc(vector<int>&, int input, int user_ints)
{
int i;
double sum = 0;
double avg= 0;
vector<int> arbNums(input);

for (i = 0; i < input; i++)
{
arbNums.push_back(user_ints);
sum += arbNums[i];
avg = (sum) / (input);
}

return avg;
}
Last edited on
Does your code actually compile? It doesn't look like it should. You are passing parameters without a name, as in:

double meanFunc(vector<int>&, int input, int user_ints)

Above, what's the name for the vector<int> reference?

My advice is that you go over the theory of functions one more time, then try to rewrite your code:
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/

Finally, please use code tags around your code. That way indentation is preserved and syntax highlighting is used, making your code easier to read.
Last edited on
It compiles. But average is a 0. This is exactly the same code that works when I do it with arrays with some subtle changes. I will go over those tutorials. Thanks.
This starting statements are already invalid

int user_input;
int user_integers;
double avg;
vector<int> v(user_input);

Variable user_input was not initialized.
That was it. I was messing around while watching some youtube video examples and forgot I had that there. Now it works.
Guess that is what happens when you work on it too much.

Thank you.
If it indeed works nevertheless your code is invalid.:)
Now it works.

Are you sure? If so, perhaps the code you posted in your first post isn't your current version?

Your code does not read data into the vector, for starters.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
flip1 ~/labs/lab10 164% ./vectors
How many integers do you want in the vector?
4
Enter integers:
1
Enter integers:
2
Enter integers:
3
Enter integers:
4
The average of the vector is: 2.5
The standard deviation of the vector is: 1.29099
flip1 ~/labs/lab10 165%
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <vector>
#include <cmath>
#include <iterator>

using namespace std;

int fillVector(vector<int>& arbNums, int input, int user_ints);
double meanFunc(vector<int>& arbNums, int input, int user_ints);
double stndDev(vector<int>&arbNums, int input, double avg, int user_ints);

int main()
{
int user_input;
int user_integers;
double avg;
vector<int> v;

cout << "How many integers do you want in the vector?" << endl;
cin >> user_input;

fillVector(v, user_input, user_integers);

avg = meanFunc(v, user_input, user_integers);

cout << "The average of the vector is: " << avg << endl;
cout << "The standard deviation of the vector is: " << stndDev(v, user_input, avg, user_integers);
}

int fillVector(vector<int>&arbNums, int input, int user_ints)
{

for (int i = 0; i < input; i++)
{
cout << "Enter integers: " << endl;
cin >> user_ints;

arbNums.push_back(user_ints);
}
return 0;
}


double meanFunc(vector<int>&arbNums, int input, int user_ints)
{
int i;
double sum = 0;
double avg= 0;

for (i = 0; i < input; i++)
{
sum += arbNums[i];
avg = (sum) / (input);
}

return avg;
}

double stndDev(vector<int>&arbNums, int input, double avg, int user_ints)
{
int i;
double sum2 = 0;
double dev = 0;

for (i = 0; i < input; i++)
{
sum2 += pow((arbNums[i] - avg),2);
}
dev = sqrt(sum2 / (input -1));
return dev;
}
Looking good!

There is still room for improvement here and there. For instance, why not drop the user_ints parameter since you never use it?

You do misuse it in fillVector() as a mere local variable. You could just as well declare it as such:

1
2
3
4
5
6
7
8
9
10
11
void fillVector(vector<int>& arbNums, int input)
{
    int user_ints; // local variable

    for (int i = 0; i < input; i++)
    {
        cout << "Enter integers: " << endl;
        cin >> user_ints;
        arbNums.push_back(user_ints);
    }
}


Also, if your function doesn't return anything meaningful, why not make it a void function (as shown above)?
Topic archived. No new replies allowed.