trying to calculate average in function with pointer notation. Keeps displaying memory address..

Hi,

I was trying to calculate an average, and return the value using pointers in the function. Instead, it keeps returning the memory address. If anyone could give me some insight, I would greatly appreciate it.

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


double average(double* scores, int numScores); //prototype

int main ()
{
	double scores;
	int numOfScores;

	do
	{
		cout << "How many test scores? ";
		cin >> numOfScores;
	}
	while (numOfScores < 0);
	cout << "\n----------------------------------\n" <<endl;
	for (int scoreList = 0; scoreList < numOfScores ; scoreList++)
	do
	{
		cout << "Enter score on test " << scoreList + 1 <<": "; 
		cin >> scores;
	}
	while (scores < 0);
	cout << "\n----------------------------------\n" <<endl;
	cout << "The average test score was: " << fixed << setprecision (2) << average <<endl;
	return 0;
}

double average(double* scores, int numScores)
{		
	double *avScores;
		
	for (*scores;  *scores < numScores; *scores++)
	{
			*avScores = *scores/numScores;
	}	
	return *avScores;
}  
Instead of calling the function you use its name in operator <<.
The syntax of calling a function is

function_name( argument_list );
Vlad,
Thank you. However, I need to use the format above and call the function into main (line 28).
It seems that you have not understood.

Here
cout << .... << average <<endl;

average is not a function call

function call has syntax when after the function name arguments enclosed in parentheses follow.
Last edited on
Also your average function is all wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
double average(double* scores, int numScores)
{		
	double *avScores;  // avScores is a pointer, but doesn't point to anything (bad pointer)
		
	for (*scores;  *scores < numScores; *scores++) // this just keeps incrementing the first
	// score in the array until it is >= the number of scores.  It does
	// does not look at any scores beyond the first
	{
			// avScores is a bad pointer.  trying to dereference it = BAD BAD BAD
			// memory corruption, possible program crash
			*avScores = *scores/numScores;
	}
	
	// again... avScores is a bad pointer.  can't dereference it!
	return *avScores;
}


There is no reason for avScores to be a pointer here. There's nothing for it to point to. You just want a normal double here.

Also, you can treat 'scores' like a normal array in this function.



EDIT: Actually I might be wrong on your for loop.... I can never remember if *foo++ is (*foo)++ or *(foo++). Either way, it's not doing what you think it does.
Last edited on
Hi Vlad,

Thank you for your reply. I just tried that solution, and I keep getting errors. Integers and doubles are in the function, so I keep getting an incompatibility error.

Cheers
Hey Disch,

I just changed some things around with your advice as well as Vlad's, and I still can't make it work... Unfortunately, I can't use any array notation in the function itself. I have to use pointer notation.
Pointers and Arrays are the same. (There are a few minute technicalities that make them different, but syntax-wise they're almost always identical)
L B, can you run my code and see why i'm such a f*ck-tard?

Cheers
You should enter values of scores into an array and pass this array to function average.
Vlad,
I really appreciate it, but i'm lost in space now.
Topic archived. No new replies allowed.