Structured Data with Pointers

Hello all, I have a question with my Program. I have defined a structure for Student Grade Records, declared an array of records on the heap, populated the array, although I am having issues with my "Average" Function. It is returning this - I have a function which loops through each row of exam scores and calculates that average. I'm sure its an easy fix any help will be appreciated, thank you!

1. Name: Amy Adams Avg: 4.42129e+008
2. Name: Bob Barr Avg: 4.42129e+008
3. Name: Carla Carr Avg: 4.42129e+008
4. Name: Dan Dobbs Avg: 4.42129e+008
5. Name: Elena Evans Avg: -8.42146e+006

Press any key to continue . . .

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

const int NG = 4;
string names[] = { "Amy Adams", "Bob Barr", "Carla Carr", "Dan Dobbs", "Elena Evans"};

int exams[][NG] = { { 98, 87, 93, 88 }, 
		    { 78, 86, 82, 91 },
		    { 66, 71, 85, 94 },
		    { 72, 63, 77, 69 },
		    { 91, 83, 76, 60 },
		  };
struct student
{
	string name;
	int exams[NG];
	double average;
};

double calcAverage(int exams[]);
void display(student *students);

int main()
{
    student *students = new student[NG];

	for (int i = 0; i < 5; i++) 
	{
	students[i].name = names[i];

	   for (int j = 0; j < 4; j++) 
	   {
	   students[i].exams[i] = exams[i][j];
	   }
	students[i].average = calcAverage(students[i].exams);
	}
        display(students);
        return 0;

	delete[] students;
	students = NULL;
}
double calcAverage(int exams[]) 
{
	double average = 0;
	int total = 0;
	for (int i = 0; i < 4; i++) 
	{
	total += exams[i];
	}
	average = total / NG;
	
	return average;
}
void display(student *students)
{
	for (int i = 0; i < 5; i++)
	{
	cout << i + 1 << ". Name: " << students[i].name << " Avg: " << students[i].average << '\n';
	}
	cout << endl;
Problem #1: You have 5 students, but at line 26, you're only allocating 4 student instances.

Problem #2: Line 34, You're not indexing into student.exams correctly.
 
students[i].exams[i] = exams[i][j];


Should be:
 
students[i].exams[j] = exams[i][j];





Problem #3: Line 47 total declared as int:

1
2
3
4
5
6
7
const int NG = 4;
/ ...
double average = 0;
int total = 0;
/...
average = total / NG; // Right-hand side evaluates first to int,
                      // then is casted to double, but decimal part is already gone. 


And regarding your original question: read about io manipulators. That will help you to format the output the way you like (ie print in decimal, hex or octal notation - or as in your example - scientific notation, set number of decimal places including 0s etc).
http://www.cplusplus.com/reference/iomanip/?kw=iomanip

After fixing problems 1-3 (and adding missing closing bracket) the code works for me with this output:
1. Name: Amy Adams Avg: 91.5
2. Name: Bob Barr Avg: 84.25
3. Name: Carla Carr Avg: 79
4. Name: Dan Dobbs Avg: 70.25
Last edited on


That's my suggestion:


cast your int 's to doubles in calcAverage ... or s.th. alike ...
and set students[i].exams[j] = .... in line 34

and let me know, if it worked.

cheers

... oha, when i read this thread for the last time, the two above replies were not yet displayed. ... sorry for the repetitions ...
Last edited on
Thanks guys! I got it working, I appreciate the help.

Here is my output:

1
2
3
4
5
6
7
1. Name: Amy Adams Avg: 91.50
2. Name: Bob Barr Avg: 84.25
3. Name: Carla Carr Avg: 79.00
4. Name: Dan Dobbs Avg: 70.25
5. Name: Elena Evans Avg: 77.50

Press any key to continue . . .
Topic archived. No new replies allowed.