Help with Vector Averages

Hi there. I need some help with a program i am making for a class. It is a statistics program for vectors/arrays. One of the functions i need to make is one that will calculate the average of the array. I have made a program before on finding the average of 3 numbers, but i just dont know where to et started for one that could have any number of things to add. My real problem is this: Im not sure how to add all parts of the array up at once. I tried a nested for loop, a regular for loop, etc.

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
73
74
75
76
77
#include <iostream>
#include <vector>


using namespace std;
void mysort(vector<int> &v);
double average (vector<int> v);
int main(){

    int size, i, data, n;
    double mean;



    cout << "Please Enter the desired size of your array: ";
    cin >> size;

    vector<int>name(size);
    for (i=0; i < name.size(); i++) {
        cout << "array[" << i << "]: ";
        cin >> data;
        name[i] = data;

    }



    cout << "Sort: "; //sorts the array from least to greatest

    for (i=0; i < name.size(); i++) {
       mysort (name);

        cout << name[i] << ", ";
    }
    cout << '\n';

    cout << "n: " << name.size() << '\n'; //returns size of array

    cout << "mean: "; //returns average
    mean = average (name);
    for (i=0; i < name.size(); i++) {
        cout << mean;

    }



return 0;
}


void mysort (vector<int> &v) {
    int i, j, k;

 for (i = v.size(); i > 0; i--) {

  for (j = 0, k = 1; k < i; j++, k++) {

   if (v[j] > v[k]) {

    int temp = v[j];
    v[j] = v[k];
    v[k] = temp;
   }
  }
 }
}

double average (vector<int>v) {
    int i, j, k;
    for(i=0; i < v.size() - 1; i++) {
        for (j = 0, k = 1; k < j; j++, k++) {
                v[j] + v[k] / v.size();
        }
    }

}
Last edited on
You can't really add them all up at once. Do it iteratively.
1
2
3
4
5
6
7
8
vector<int> numbers;
int sum = 0;
for (int i = 0; i < numbers.size(); i++)
{
        sum += numbers[i];
}

double average = sum / numbers.size();


Also, your average function doesn't return anything, but your definition says it's supposed to return a double.
i think that helped...
I say think because of this input/output:

input: 8, 1, 6

output

mean: nan
"nan" is the output?

OH!

I forgot - vectors DOUBLE in size when they need more room.

Try this:
1
2
3
4
5
6
7
8
9
10
vector<int> numbers;
int sum = 0;
int numberOfElements = 0;
for (int i = 0; i < numbers.size(); i++)
{
        sum += numbers[i];
        numberOfElements++;
}

double average = sum / numberOfElements;

Also, try using this in your code after you declare your "name" vector: name.reserve(size)
That will reserve just enough elements so that you don't have your vector getting too big.
Last edited on
yeah, right after replying, i fiddled around with it a bit.

1
2
3
4
5
6
7
8
9
vector<int> numbers;
int sum = 0;
for (int i = 0; i < numbers.size(); i++)
{
        sum += numbers[i];
}

//double average =
return sum / numbers.size();

I just got rid of "double average = " and replaced it with "return"
Thanks for the help
Topic archived. No new replies allowed.