Cant get average value to print from vector

I cant get the average value from the vector and dont know what I´m doing wrong. I want to do the calculation with a for-loop

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
 #include <iostream>
#include <vector>
#include <numeric>

using namespace std;

// Function Declarations
float average(vector<int>&);
// average - calculates average value in vector

void print(float);
// print - prints averageValue
// @param float - averageValue to print

// Initiate values
	int sum = 0; 
	int i=0;
	double averageValue;

int main()
{
			
	// Initiating vector

	vector <int> vectorValues(10);

	// Initiating loop, vector size is zero
	// if i less than ten count up

    for (vector<int>::size_type i=0; i < 10; i++)
	{
		
		// Flush makes sure that the buffer is cleared and the characters are written to their destination
		cout << "Enter value to vector #" << i+1 << ": " << flush;
		
		// Enter values into vector
		cin >> vectorValues[i];	
	}	

	average(vectorValues);
	print(averageValue);
	return 0;
}

	float average(vector<int>& vectorValues)
	{
		for (i = 0; i > vectorValues.size(); i++)
		{
			sum += vectorValues[i];
		}
		// Adding values from vector into variable sum
	//	sum = vectorValues[0] + vectorValues[1] + vectorValues[2] + vectorValues[3] + vectorValues[4] + vectorValues[5] + vectorValues[6] + vectorValues[7] + vectorValues[8] + vectorValues[9];
	
		// Calculating average value
		averageValue =  (float) sum / vectorValues.size();

		return averageValue;

	}

void print(float averageValue)
{

	// Print average value
	cout << endl;
	cout << "Average value " << averageValue << endl;
}
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
#include <iostream>
#include <vector>
//#include <numeric>
using namespace std;

double average(vector<int> vectorValues) ///
	{
	    int sum=0; ////////////
		for (int i = 0; i < vectorValues.size(); i++) /////////
		{
			sum += vectorValues[i];
		}

		double averageValue =  (double) sum / vectorValues.size(); /////////////////////
		return averageValue;
	}

void print(double averageValue)
{
	cout << endl;
	cout << "Average value " << averageValue << endl;
}


int main()
{	
	vector <int> vectorValues;
    for (vector<int>::size_type i=0; i < 5; i++)
	{
		int n; /////		
		cout << "Enter value to vector #" << i+1 << ": " << flush;		
		// Enter values into vector
		//cin >> vectorValues[i];	
		cin >> n;
		vectorValues.push_back(n);
	}	

	for(auto x: vectorValues)
		cout << x << " ";

	double av = average(vectorValues); //////////////////
	print(av);
	return 0;
}
Look at the line 47
for (i = 0; i > vectorValues.size(); i++)

Do you see that?
Yes I do, whats with it?
You assign 0 to i, and then in loop it checks whether value of i - which is 0 - is higher than the size of the vector.
Last edited on
for (i = 0; i > vectorValues.size(); i++)

changing it

for (i = 0; i < vectorValues.size(); i++)

now it works, thank you man. I stared myself blind, a simple error like that spoiled Everything. Thank you!
Topic archived. No new replies allowed.