average function

Apr 30, 2010 at 4:14pm
Almost completed my project. Instructor says " need to call the avg funtion before you have the values of the array, call the average function after the values of the array are read, and need an avg function definition. Below is my code it compiles and runs,but not sure if I did what my instructor wanted. Please help...
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
#include <iostream>
using namespace std;

int main ()
{
	int sum = 0;
	int average = 0;
	int score [10];
	int k;
	int avg(int a[])

//int sum = 0;
// int average = 0;
	for (k = 0; k<10; k++)
		
	{
		cout <<"Enter score for 10 test:"<< (k+1) <<endl;
		cin>> score [k];
        average = avg(score)
		sum = sum + score[k];
	}
	average = sum/10;
	for (k=0; k<10; k++)
		cout<<"score for test:"<<(k+1)<<"is"<<score[k]<<endl;
}
int avg (int a[size])
{
	cout<<"The average for all 10 test is"<<average<<endl;
	return 0;
}
Apr 30, 2010 at 5:51pm
closed account (S6k9GNh0)
1) This does not build with any standard compiler. Even most non-standard compilers won't build this. You should have realized this.
2) int main() has no way of calling avg because its not in its scope. To fix this, you must place a definition of the avg() function before int main().

3) Here is a heavily modified version of your code that keeps the original intent:

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
#include <iostream>
using namespace std;

int avg(int*);

int main ()
{
	int sum = 0;
	int average = 0;
	int score [10];
	int k;

	for (k = 0; k<10; k++)

	{
		cout <<"Enter score for 10 test:"<< (k+1) <<endl;
		cin >> score [k];
                average = avg(score);
		sum = sum + score[k];
	}

	average = sum/10;
	for (k=0; k<10; k++)
		cout<<"score for test:"<<(k+1)<<"is"<<score[k]<<endl;
}

int avg (int a[])
{
	cout << "The average for all 10 test is" << /* average << */ endl;
	return 0;
}


This code compiles. I will mention that this is butt ugly code. What did your professor want you to do? If it was to implement a function that gives the average of an integer based array, you are no where close.
Last edited on Apr 30, 2010 at 5:51pm
Apr 30, 2010 at 5:57pm
Thank you, yes her last comments were to have the average function called from main, and to avg the function after I collected the value. Im not sure what int avg (int*) is. I have never seen that. Here are my origional instructions. I have to be honest I am barly passing the class, as now unemployed police officer I was searching for something I could do from home. Apparently this is not it. But I would like to finish this class with a passing grade before I move on. Any help would be greatly appreciated. "Allow users to enter 10 numbers in doubel format, store the numbers in an array. Create and use a function to calculate the average. Out put the average. She does want the avg funtion seperate from main.
May 1, 2010 at 1:13am
closed account (S6k9GNh0)
Like I said your function doesn't do anything. In C++, we would create a vector, and pass it to the function, then create a temporary variable to hold the total and iterate through the vector, adding each one to the temporary variable, then returning the temporary variable divided by the size of the vector.

I don't personally know if your professor wants to introduce the STL yet. There is, however, no decent and proper way to implement an average function that passes an array without memset to prevent passing trash to your average function (when you declare a variable, it is given a location that often carries trash until assigned a value. This is why its wise to ALWAYS initialize variables.) For a beginner class, it seems to a lot more complicated than it might first seem, especially since your teacher asked for doubles.

The average class is also a good chance to use a simple list, but I won't introduce this either. The main reason this is good is to allocate a dynamic amount of list elements in C so you don't have to hardcode the the number of numbers allowed.

Here's a cheap example:
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
#include <iostream>
#include <cstdlib>

double avg(double * score, int size)
{
	double total;
	
	for (int i = 0; i < size; ++i)
	{
		total += score[i];
	}
	
	return ( total / (double)size );
}

int main(int argc, char ** argv )
{
	double score[10];
	
	if (argc < 2) //no arguments
	{
		std::cout << "Please enter valid numbers!" << std::endl;
		return 0;
	}
	
	for (int i = 0; i < argc - 1; ++i) //argc is always at least one so we need to subtract one in case given no parameters.
	{
		score[i] = atoi(argv[i+1]);
	}
	
	std::cout << "The average of the given numbers is: " << avg(score, argc - 1) << std::endl;
	return 0;
}


Last edited on May 1, 2010 at 5:07am
May 1, 2010 at 1:45am
computerquip... erm... you have a [/code tag at the bottom of your post. Do you mind fixing it?

Also, (this is not to you computerquip, I expect you wrote the code the contorted way you did for a reason), note that the instructor asked for a double format, meaning your array needs to be something else than it actually is.

Optional: I'd also write a function to do something if argc < 2 besides complain.

-Albatross
May 1, 2010 at 3:17am
Do your own homework.
May 1, 2010 at 5:11am
closed account (S6k9GNh0)
I didn't use anything to advanced (or tried not to) and gave a freebie.

Please note that whatever numbers you put it, it will always be a few millionths off which has to do with double precision. Not much you can do about it really unless you get advanced.
Topic archived. No new replies allowed.