function/array problem.. ugly code

hw plz help! I've wrote this awful code and I'm just interested in what may look wrong or inefficient, we're going over FUNCTIONS and ARRAYS in class right now and it's taken me quite a while just to get this far.. and it looks rough to me, even just looking at it. there has to be a better way! lol

heres the problem:
"Write a C++ program that includes two functions named calcavg() and variance(). The calcavg() function should calculate and return the average of values stored in an array named testvals. The array
should be declared in main() and include the values 89, 95, 72, 83, 99, 54, 86, 75, 92, 73, 79 75, 82, and 73. The variance is obtained by subtracting the average from each value in testvals, squaring the
values obtained, adding them, and dividing by the number of elements in testvals. The values returned from calcavg() and variance() should be displayed by using cout statements in main(). Use type double
for all variables."

here's my code, so far:
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
#include<iostream>
#include<cmath>
using namespace std;

double calcavg(double, double);
double variance(double, double, double);

int main()
{
	double testvals[14]{ 89, 95, 72, 83, 99, 54, 86, 75, 92, 73, 79, 75, 82, 73 };
	double size = 14.0;
	double sum = 0.0;
	double a = calcavg(sum, size);

	cout << "Average of array elements is " << calcavg(sum, size) << endl;

	cout << "Variance of array elements is " << variance(sum, size, a) << endl;

	system("pause");
	return 0;
}

double calcavg(double sum, double size)
{
	double testvals[14]{ 89, 95, 72, 83, 99, 54, 86, 75, 92, 73, 79, 75, 82, 73 };
	for (int i = 0; i<14; i++)   //Loop which inputs arrays data and calculates its sum
	{
		sum = sum + testvals[i];
	}
	return sum / size;
}

double variance(double sum, double size, double a)
{
	double testvals[14]{ 89, 95, 72, 83, 99, 54, 86, 75, 92, 73, 79, 75, 82, 73 };
	for (int i = 0; i<14; i++)   //Loop which inputs arrays data and calculates its sum
	{
		sum = sum + ((testvals[i] - a)*(testvals[i] - a));
	}
	return sum / size;
}


I don't know how to pass arrays in and out of functions without declaring them in both main and the function itself, and it's a really repetitive code cause i was trying to use concepts i somewhat understood and break down the project into tiny parts.. it compiled but it just seems off to me, ANY SUGGESTIONS WOULD BE MUCH APPRECIATED!
Well, it's not so bad for a first attempt.

a) remove the parameter 'sum' from both functions. Instead, define a variable sum inside both functions and give it a starting value 0 there

b) You pass arrays to functions the same way you pass other types.

c) So, the header of the calcavg function should be:

double calcavg(double testvals[], int size)

...in fact, the name of that parameter does not need to be named testvals at all.

d) the comment in line 36 is off
Last edited on
And, by the way, you should really use std::vector instead of c-style arrays. But if your instructor insists on c-style arrays, as he appears to do, there might be something wrong with him..
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>

double calcavg(double[], int);

int main()
{
	double testvals[] = { 1, 2, 3};
        int size = sizeof(testvals)/sizeof(double);
	
	double average = calcavg(testvals, size);
	std::cout << "Average of array elements is " << average << std::endl;
	
	return 0;
}

double calcavg(double anArray[], int size)
{
	double sum = 0;
	for (int i = 0; i < size; i++)
	{
		sum += anArray[i];
	}
	return sum / (double)size;
}


BTW: Don't worry about the array/vector police - the whole issue is a non-event. :)
Last edited on
Topic archived. No new replies allowed.