Struggling with arrays and pointers

Hello,

I am in an intermediate C++ class that is moving at a fast pace and I am falling behind. I'm in desperate need of some assistance and am having trouble specifically with arrays, pointers, and generating the output for them. Here are the project parameters:

Problem
If you know how many items you will need before you compile a program, you can create arrays with a
known number of elements. However, this is not always possible. To create variables of unknown size at
runtime, C++ provides
a m[/b]eans for dynamic memory variables through the new and delete operators.
Variables that are allocated (reserved) in system memory are accessed indirectly (through their addresses)
when needed.
Last assignment we totaled lab scores - this assignment we’ll collect 3 major scores (Labs, MX, FX) to
determine final points and a letter grade for an unknown number of students. The number of students as as
well as their scores will have to be entered at runtime. The program should show a welcome message, then
prompt the user for the number of students.
ClassPass - student grade calculator
Please enter number of students:2
Using the entered number, create 3 dynamic arrays (using new), one for each score. You’ll need a loop to
add student information at runtime to your allocated arrays. After entering scores, the instructor should
have the option to accept the grades, re-enter them or exit the program. A count should be used as an
index into the arrays, and also work as a student identifier.
Student 0 Lab score (0-100): 65
Student 0 MX grade (0-100): 89
Student 0 FX grade (0-100): 67
A)ccept, R)e-enter or eX)it:a
After the instructor has entered all score information for the number of students specified, generate a grade
report by calculating total points for every student. Your report should have a report heading message, and
a single line for each student containing Student #, Points, Rounded points, Letter grade.
Grade Report -------------
Student:0 Points:68.9 Rounded:69 Grade: D+
Student:1 Points:89.25 Rounded:89 Grade: B+
When complete, your program should output a good-bye message showing that it is no longer running.
This lab has 4 problems/tasks to work out:
1. Data representation: Scores for Labs, MX and FX are all integers, but you should allocate integer
arrays at runtime, meaning you’ll need to use pointer notation for your array variables.
2. Data input: Loop and use prompts and console input to collect 3 scores into input variables for each
student. Once scores are entered, ask the user if they want to accept the scores or re-enter them or
exit the program. If the user exits, you should print the goodbye message and exit from the program. If
the user needs to re-enter the scores, you should re-prompt the user for the scores again without changing the count of processed students. Only if the user accepts the entered scores, should you
copy the input variables into your scores arrays and increment the student count.
3. Grade Report: Generate a grade report with a heading, and loop through your arrays to calculate
points based on the percentages provided in the syllabus (Labs = 70%, MX = 15%, FX = 15%). Round
the resulting points using the provided round function and then using an if-else ladder, determine the
final letter grade. Note - print both the original and rounded points and the letter grade. (See example.)
4. Free Memory: After your program run has completed (before return), you’ll need to delete your 3
allocated arrays. [/b]

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
  #include <string>
#include <iostream>

using std::cin;
using std::cout;
using std::endl;
using std::string;

inline int round(float num) { return num + 0.5f;}

int main()
{
	const float kLabPercent = .70f, kMXPercent = .15f, kFXPercent = .15f;

	const string kSpacer(" "),
		kWelcomeMessage("\nClassPass - student grade calculator"),
		kCountPrompt("\nPlease enter number of students:"),
		kLabPrompt(" Lab score (0-100): "),
		kMXPrompt(" MX grade (0-100): "),
		kFXPrompt(" FX grade (0-100): "),
		kApprovePrompt("A)ccept, R)e-enter or eX)it:"),
		kErrorPrompt("I don't understand that key."),
		kStudent("Student "),
		kStudentGradeReport("Grade Report -------------"),
		kRepStudent("\nStudent:"),
		kRepPoints("\nPoints:"),
		kRepRounded("\nRounded:"),
		kRepGrade("\nGrade: "),
		kGoodbyeMessage("\nClassPass - is complete"),
		kReturnMessage("\nPlease re-enter grades"),
		kEntryMessage("\nPlease enter grades");
	
	//Get # of students
	int numStudents;
	cout << kWelcomeMessage << endl;
	cout << kCountPrompt; cin >> numStudents;

	//Ask system for arrays (allocate memory)
	//The * are called pointers
	int* lab = new int [numStudents];
	int* MX = new int [numStudents];
	int* FX = new int [numStudents];

	int studentNDX = 0;
	int ary [] = {numStudents};

	//Get scores
	int inLab, inMX, inFX;
	do{
			cout << kEntryMessage;
			cout << kLabPrompt; cin >> inLab;
			cout << kMXPrompt; cin >> inMX;
			cout << kFXPrompt; cin >> inFX;
		
		char key;
		cout << kApprovePrompt; cin >> key;
		key = toupper(key);

		if(key == 'X'){
			cout << kGoodbyeMessage;
			delete [] lab;
			delete [] MX;
			delete [] FX;
			return EXIT_SUCCESS;

		}else if(key == 'A'){
			lab [studentNDX] = inLab;
			MX [studentNDX] = inMX;
			FX [studentNDX] = inFX;
			studentNDX++;

		}else if(key != 'R'){
			cout << kErrorPrompt;
			cout << kReturnMessage;
			cout << kEntryMessage;
			cout << kLabPrompt; cin >> inLab;
			cout << kMXPrompt; cin >> inMX;
			cout << kFXPrompt; cin >> inFX;}
	
	}while(studentNDX < numStudents); 

	float repPoints;
		repPoints = (inLab * kLabPercent) + (inMX * kMXPercent) + (inFX * kFXPercent);

	int repRounded = round (repPoints);
	string letterGrade;
		if (repRounded >= 93){letterGrade = 'A';}
		else if(repRounded >= 90){letterGrade = 'A-';}
		else if(repRounded >= 87){letterGrade = 'B+';}
		else if(repRounded >= 83){letterGrade = 'B';}
		else if(repRounded >= 80){letterGrade = 'B-';}
		else if(repRounded >= 77){letterGrade = 'C+';}
		else if(repRounded >= 73){letterGrade = 'C';}
		else if(repRounded >= 70){letterGrade = 'C-';}
		else if(repRounded >= 66){letterGrade = 'D+';}
		else if(repRounded >= 60){letterGrade = 'D';}
		else if(repRounded <= 59){letterGrade = 'F';}

	//Outputting a report
	cout << kStudentGradeReport << endl;
	for (int ndx = 0; ndx <= numStudents; ndx++){
		cout << kRepStudent << ary[ndx] << kRepPoints << repPoints << kRepRounded << repRounded << kRepGrade << letterGrade;}

	cout << kGoodbyeMessage;
	system("PAUSE");
	
	//Return memory
	delete []lab;
	delete []MX;
	delete []FX;

	return EXIT_SUCCESS;}


My code isn't compiling or doing what I need it to do. I'm really having a tough time understanding my pointers and arrays, and outputting them. Any and all help would be appreciated.
I know there's a lot there but my issue is I don't even understand where I'm going wrong well enough to know what the important information is.
Online compiler does compile your code, but warns about lines 88, 89, 91, 92. Single quotes denote character, while double quotes denote string. A- is not a character.
You're actually pretty close. The main problems are just in generating the report.
Get rid of line 45. it isn't needed.

In the loop at line 101, the ending condition should be ndx < numStudents. If there are numStudents entries in the array then the valid indices are 0 to numStudents-1.

In the loop at line 101, you can access the grades for an individual student with lab[ndx], MX[ndx] and FX[ndx]. You need to calculate repPoints, repRounded, and letterGrade inside the loop. So the logic will look like this:
1
2
3
4
5
6
for (int ndx = 0; ndx < numStudents; ndx++){
    // compute repPoints, repRounded and letterGrade for the ndx'th student
    // using lab[ndx], MX[ndx] and FX[ndx].
    out << kRepStudent << ary[ndx] << kRepPoints << repPoints
       << kRepRounded << repRounded << kRepGrade << letterGrade;
}


You will find that you need some formatting changes in the report, but I'll leave that to you.


Topic archived. No new replies allowed.