array displaying address instead of double

It seems as though everything is working fine except in the end at the function displayTotal (line 113) it displays "'student name' scored 0023F5A8 and got a 'grade'"
how do I change it to display the actual average instead of address.

I think I need to put a & somewhere but I cannot figure it out.
Thank you in advance

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream>
#include <string>

using namespace std;

void getNames(string[], int);
void getScores(string[], double [], int);
double getAverage(double[], int);
void displayTotal(string[], char[], double[]);

int main()
{
	const int NAMES = 5;
	const int GRADES = 5;
	const int SCORES = 4;

	string studentNames[NAMES];
	char grades[GRADES] = { 'A', 'B', 'C', 'D', 'F' };
	double studentTest1[SCORES];
	double studentTest2[SCORES];
	double studentTest3[SCORES];
	double studentTest4[SCORES];
	double studentTest5[SCORES];
	double average[GRADES];

	// GET NAMES OF THE 5 STUDENTS
	getNames(studentNames, NAMES);
	cout << endl;

	//GET THE 4 SCORES FROM STUDENT 1
	getScores(studentNames, studentTest1, SCORES);
	cout << endl;

	//GET THE 4 SCORES FROM STUDENT 2
	getScores(studentNames, studentTest2, SCORES);
	cout << endl;

	//GET THE 4 SCORES FROM STUDENT 3
	getScores(studentNames, studentTest3, SCORES);
	cout << endl;

	//GET THE 4 SCORES FROM STUDENT 4
	getScores(studentNames, studentTest4, SCORES);
	cout << endl;

	//GET THE 4 SCORES FROM STUDENT 5
	getScores(studentNames, studentTest5, SCORES);
	cout << endl;

	//GET AVERAGE OF STUDENT 1 TEST SCORE
	average[0] = getAverage(studentTest1, SCORES);

	displayTotal(studentNames, grades, average);

	//GET AVERAGE OF STUDENT 2 TEST SCORE
	average[1] = getAverage(studentTest2, SCORES);

	displayTotal(studentNames, grades, average);

	//GET AVERAGE OF STUDENT 3 TEST SCORE
	average[2] = getAverage(studentTest3, SCORES);

	displayTotal(studentNames, grades, average);

	//GET AVERAGE OF STUDENT 4 TEST SCORE
	average[3] = getAverage(studentTest4, SCORES);

	displayTotal(studentNames, grades, average);

	//GET AVERAGE OF STUDENT 5 TEST SCORE
	average[4] = getAverage(studentTest5, SCORES);

	displayTotal(studentNames, grades, average);

	system("pause");
	return 0;
}

void getNames(string name[], int numNames)
{
	for (int count = 0; count < numNames; count++)
	{
		cout << "Name of Student " << (count + 1) << ": ";
		cin >> name[count];
	}
}

void getScores(string studentNames[], double studentTest[], int test)
{
	static int studentNum = 0;	// STATIC INT TO KEEP NAME UNTIL FUNCTION CHANGES TO REUSE
	cout << "\tOnly scores 0 - 100 will be accepted." << endl;
	for (int count = 0; count < test; count++)
	{
		do {
			cout << "Score of Test " << (count + 1) << " From Student " << studentNames[studentNum] << ": ";
			cin >> studentTest[count];
		} while (studentTest[count] < 0 || studentTest[count] > 100);
	}
	studentNum++;
}

double getAverage(double studentTest[], int num)
{
	int sum = 0;
	for (int count = 0; count < num; count++)
	{
		sum += studentTest[count];
	}
	sum = (sum / 4);
	return sum;
}

void displayTotal(string studentNames[], char grade[], double avg[])
{
	static int count = 0;
	if (avg[count] < 60)
	{
		cout << studentNames[count] << " scored " << avg << " and got an " << grade[4] << endl;
		count++;
	}
	else if (avg[count] <= 69)
	{
		cout << studentNames[count] << " scored " << avg << " and got a " << grade[3] << endl;
		count++;
	}
	else if (avg[count] <= 79)
	{
		cout << studentNames[count] << " scored " << avg << " and got a " << grade[2] << endl;
		count++;
	}
	else if (avg[count] <= 89)
	{
		cout << studentNames[count] << " scored " << avg << " and got a " << grade[1] << endl;
		count++;
	}
	else
	{
		cout << studentNames[count] << " scored " << avg << " and got an " << grade[0] << endl;
		count++;
	}
}
An array is actually a pointer (correct me if I'm wrong). In your displayTotal() function you are printing the array avg without a subscript operator. So instead the address is printed.
I changed avg to avg[count] and it still printed the address so I tried changing it to &avg[count] and still showing the address. the average number is coming out correct because the grades A - F are showing up correctly, but the address of the array is still showing up. Thank you for the suggestion though. I thought that might do it lol
An array is actually a pointer

Actually, it's not.

An array has an address - the address of its first element - and a size. But a pointer is a variable which stores the address of some other variable.

When you pass an array as a parameter to a function, it "decays" to a pointer to the first element.

What is array decaying?
http://stackoverflow.com/questions/1461432/what-is-array-decaying

void printArray(double arr[], int arrSize);

can be called for

1
2
const int resultsSize = 100;
double results[resultsSize]; // filled in somehow... 


like

printArray(results, resultsSize); // array decays to pointer

or

printArray(&results[0], resultsSize); // pass address of first element explicitly

Andy
Topic archived. No new replies allowed.