finding an average of an array

Hi, I'm trying to solve a problem: http://www.codeabbey.com/index/task_view/average-of-array. I'm pretty sure that my function is bad, but its my first time creating vector that has a vector type, I'm pretty much stuck and could use help, and sorry that my question isn't precise.

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
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
#include <sstream>
using namespace std;

void avgOfArr(vector<vector<int>> vec)
{
	int avg;
	vector<int> store;
	for (int i = 0; i < vec.size(); ++i)
	{
		int temp = 0;
		store = vec[i];
		for (int i = 0; i < store.size(); ++i)
		{
			temp = temp + store[i];
		}
		avg = temp / store.size();
		cout << avg << endl;
	}
}


int _tmain(int argc, _TCHAR* argv[])
{
	int t, nums;
	cout << "Enter number of test cases: " << endl;
	do
	{
		cin >> t;
	} while (t < 0 && (int)t != t);
	cout << "Enter " << t << " arrays: " << endl;
	vector< vector< int > > vecOfvecNums;
	vecOfvecNums.resize(t);
	for (int i = 0; i < t; ++i)
	{
		vector<int> numsVec;
		while (cin >> nums)
		{
			numsVec.push_back(nums);
		}
		vecOfvecNums.push_back(numsVec);
	}
	cout << endl;
	avgOfArr(vecOfvecNums);
	return 0;
}
Stepping through your program, i noticed you resize vecOfNums, then you push_back values onto it. resize actually adds elements into the vector in this case. You should instead be using reserve to reserve space instead. But this is not needed because vector dynamically allocates memory for you.
http://www.cplusplus.com/reference/vector/vector/resize/
http://www.cplusplus.com/reference/vector/vector/reserve/

Your next problem is lines 41 to 44:
1
2
3
4
5
vector<int> numsVec;
while (cin >> nums)
{
	numsVec.push_back(nums);
}


This is an infinite loop. There are many ways around this problem, here are two:
1
2
3
4
5
6
7
8
while (true) //infinite loop
{
	cin >> nums;
	if(nums == 0){
		break;//break out of loop
	}
	numsVec.push_back(nums);
}

1
2
3
4
5
6
cin >> nums;
while (nums != 0)
{
	numsVec.push_back(nums);
	cin >> nums;
}


And a minor problem with your code:
you do not need the following lines because they are not used:
1
2
3
#include <array>
#include <algorithm>
#include <sstream> 


There is one problem in your function. you are using i in both your for loops, you need to pick another variable name to use for either your inner or outer loop.

Other then that, you could remove vector<int> store; and replace it with vec[i], but this is not needed.
Last edited on
Your next problem is lines 41 to 44:

1
2
3
4
5
vector<int> numsVec;
while (cin >> nums)
{
	numsVec.push_back(nums);
}

This is an infinite loop. There are many ways around this problem, here are two:


That is not an infinite loop and your two "solutions" have another problem: 0 is a valid value.

Oops, not true. You read more closely than I did. The original solution, however, is not an infinite loop.
Last edited on
I was looking at the problem in the link where it shows 0 as a terminating value. Could you explain why the while loop is not infinite? From my understanding std::istream::operator>> returns a reference to itself.
Last edited on
BlatantlyX wrote:
Could you explain why the while loop is not infinite? From my understanding std::istream::operator>> returns a reference to itself.


It does. And in the context of a boolean value (such as in the expression used to control a while loop) this results in a conversion to bool. See:

http://en.cppreference.com/w/cpp/io/basic_ios/operator_bool

The effect is that while input extraction is successful the loop continues. When it is not, the loop terminates.
Thanks for the help guys its working now, but when i use the input provided from the site, some of the average numbers in my output are 1 integer less then the supposed output from the site.
Example:
Expected answer was:
117 537 924 204 534 7759 301 2303 10461
My output:
116 537 923 203 534 7758 300 2302 10461

My output is on point with smaller numbers, and some of the output averages are correct and i cant figure out whats wrong, here is modified working code:

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
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;


void avgOfArr(vector<vector<int>> vec)
{
	int avg;
	vector<int> store;
	for (int i = 0; i < vec.size(); ++i)
	{
		int temp = 0;
		store = vec[i];
		for (int j = 0; j < store.size(); ++j)
		{
			temp = temp + store[j];
		}
		avg = temp / float(store.size() - 1);
		cout << avg << endl;
	}
}


int _tmain(int argc, _TCHAR* argv[])
{
	int t, nums;
	cout << "Enter number of test cases: " << endl;
	do
	{
		cin >> t;
	} while (t < 0 && (int)t != t);
	cout << "Enter " << t << " arrays: " << endl;
	vector< vector< int > > vecOfvecNums;
	for (int i = 0; i < t; ++i)
	{
		vector<int> numsVec;
		do
		{
			cin >> nums;
			numsVec.push_back(nums);
		} while (nums != 0);
		vecOfvecNums.push_back(numsVec);
	}
	cout << endl;
	avgOfArr(vecOfvecNums);
	return 0;
}

My output is on point with smaller numbers, and some of the output averages are correct and i cant figure out whats wrong, here is modified working code:

You aren't making any attempt to round the numbers per the requirements.
I didn't know that the compiler truncates the numbers, everything is working now. Thanks cire and BlatantlyX !
Topic archived. No new replies allowed.