Assistance with a program.

I know it's probably pretty simple and I'm missing something simple, but coding is really not my strong suit and I could use some help to get it running or at least some sort of push in the right direction.

I'm supposed to be working on this with a partner, but he's been no help and the teacher can't help us at all and the providing readings aren't helpful at all, so please; any help would be appreciated!!

Here's the problem:
Develop a program that will calculate students’ quiz grades:
Input
Ask for the number of students in the class
Processing
Each time a quiz grade is entered, it will display “Pass” or “Fail” for that grade, then it will prompt the teacher to enter the next quiz grade until they have entered a quiz grade for each student
After all grades have been entered, the program will read all of the grades and perform calculations:
o Average Quiz Grade
o Highest grade
o Lowest grade
o #A’s
o #B’s
o #C’s
o #D’s

Output
After all grades have been entered, the program will display the following output:
Average grade
Highest grade
Lowest grade
#A’s
#B’s
#C’s
#D’s
#Fail
#Pass

And here's my shotty attempt at the code (I know it's probably horrid, but again; coding is just something I'm not strong with :s

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

void displayGrades(int[]);
int main()
{
	int students;
	cout << "How many students are in your class? ";
	cin >> students;

	double grade = 0;
	int resultArray[99] = { students };
	cout << "\n Enter Quiz Grade (enter -1 to end): ";
	cin >> grade;
	cout << fixed;
	while (grade >= 0)
	{
		int grades;
		int pass;
		int fail;
		int index = static_cast <int>(grades = grade);
		if (index >= 10)
		{
			resultArray[10]++;
		}
		else
		{
			resultArray[index]++;
		}
		
		if (grades >= 70)
		{
			grades = pass;
		}

		else if (grades < 70)
		{
			grades = fail;
		}

		cout << "Pass or Fail: " << grades << endl;
		cout << "\n Enter Quiz Grade (-1 to end): ";
		cin >> grade;
	}
	displayGrades(resultArray);
	system("PAUSE");
	return 0;
}

void displayGrades(int printArray[])
{
	int upperBound;
	int lowerBound;
	int average;
	int maxgrade;
	int lowgrade;
	int AGrade;
	int BGrade;
	int CGrade;
	int DGrade;
	int failure;
	int passing;

	cout << "These are the results!" << printArray[10] << endl << endl;

	for (int i = 1; i <= 100; i++)
	{
		cout << "\nAverage Grade: " << average << endl;
		cout << "Highest Grade: " << maxgrade << endl;
		cout << "Lowest Grade: " << lowgrade << endl;
		cout << "A's: " << AGrade << endl;
		cout << "B's: " << BGrade << endl;
		cout << "C's: " << CGrade << endl;
		cout << "D's: " << DGrade << endl;
		cout << "Failing: " << failure << endl;
		cout << "Passing: " << passing << endl << "\t"
			<< printArray[i] << endl;
	}
}
Last edited on
Here, I've done the first half for you.
I've placed comments where I think they might be helpful.

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

int main() {

	int num_students = 0;
	//This variable represents the number of students in the class.
	//Since there is one grade per student, it also represents the number of grades.

	std::cout << "Enter the number of students in your class:\t";
	std::cin >> num_students;
	std::cout << std::endl;

	int* grades = new int[num_students] {};
	//This is a dynamic array called 'grades'.
	//We use the 'new' operator to dynamically allocate the desired amount of integers.
	//For example, if the teacher enters '3' for num_students, this array will have three integers.
	//Each integer in this array represents one grade.


	const int fail_threshold = 69;
	//Any grade above this value is considered to be 'passing'.
	//Any grade below this value is considered to be 'failing'

	int input = 0;
	//This integer will be used to read the teacher's input.
	
	//We're about to enter a for-loop.
	//One iteration per student
	for (int i = 0; i < num_students; ++i) {
		std::cout << "Enter grade for student #" << (i + 1) << " of " << num_students << ":\t";
		std::cin >> input;

		if (input > fail_threshold) {
			std::cout << "Student #" << (i + 1) << " passed." << std::endl;
		}
		else {
			std::cout << "Student #" << (i + 1) << " failed." << std::endl;
		}
		std::cout << std::endl;

	}

	//More code goes here

	delete[] grades;
	//We have to make sure to de-allocate our previously allocated memory.

	return 0;
}


This snippet prompts the teacher to enter the number of students in their class.
Then, for each student in the class, the teacher is asked to enter a corresponding grade.
If an entered grade is below a certain threshold, a "failed" message is printed.
If an entered grade is above that threshold, a "passed" message is printed.

What's left for you to do:
Tally each time a student fails.
Tally each time a student passes.

Process the grades - determine the following:
The highest of all the grades.
The lowest of all the grades.
The average grade.

The number of A's.
The number of B's.
The number of C's.
The number of D's.
(For these it would help to have different thresholds for each)

Finally, print all the results. Don't forget to print the number of failing/passing students.
Last edited on
Oh wow, thank you so much dude! This does help me out, especially with the comments. A bit more helpful if I do say so myself. :)
Glad to hear it! If you have any other questions don't hesitate.
The one thing I am wondering is, since the grades are deleted/de-allocated, how do I get the grades down to the 2nd half of the problem? That's the only thing I'm curious about and I think I could get it if I could just figure that out.
Sorry for not being clear. You only delete/de-allocate the grades once you're done using them (at the very end of your program, right before your main function returns). Basically, your remaining code would go on line 43 of the snippet I provided. Like you said, it wouldn't make sense to delete the array before you're done using it. When you delete it is entirely up to you, just as long as you don't forget to delete it - typically, it makes the most sense to delete right before your program terminates.
Last edited on
Oh okay. I apparently jumped over that code and didn't see that there! Now, I just gotta make sure I have an array somewhere (forgot to mention it's needed for this project above), and still figure out how to input these grades in properly. And don't apologize btw, you're being quite clear; I just happened to misread.
Just to recap, I don't know if you've covered dynamic memory / dynamic arrays in your class yet, but the use of a dynamic array like I demonstrated technically does qualify as an array. Of course, it will look suspicious if you haven't covered that material in class yet...

The reason I opted for a dynamic array instead of a regular one is because as the name implies, it's dynamic - meaning you don't need to know it's size in advance (or, at compile time), whereas a regular array needs to know it's size in advance. Since you can't know the number of students the teacher will enter in advance, this makes the most sense.
no, we've covered both types of arrays so it'll be fine~ I just was double checking because I'm still trying to catch up on the chapter as it is because I've been busy with a lot of other work as well. But, I get it and I understand it :)
The only thing I still can't seem to figure out is how to to do the average and sort them into the letter grades. Those are really the only things I can't figure out; I pasted the second half of my code in, but unsure what to do from there :s
Here are two small programs. They can't be copy-pasted directly.

This first one shows how to compute an average.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main() {

	int num_students = 3;

	int* grades = new int[num_students] {70, 10, 50};
	//The grades are 70, 10 and 50.

	int sum = 0;
	for (int i = 0; i < num_students; ++i) {
		sum += grades[i];
		//In this for-loop, the sum of all grades is accumulated in our variable 'sum'
	}

	//At this point, 'sum' is 70 + 10 + 50.
	//In other words, 'sum' is 130.

	int average = sum / num_students;
	//The average grade is the sum of all grades divided by the number of grades.

	delete[] grades;
	return 0;
}


This second one shows how one could compute the number of A's, B's, C's and D's. There are more elegant solutions. Also, your instructions didn't mention how the letter grades are defined. I did a quick google search, and it tells us the following:

A: 90% - 100%
B: 80% - 89%
C: 70% - 79%
D: 60% - 69%


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
int main() {

	const int threshold_d = 69;
	const int threshold_c = 79;
	const int threshold_b = 89;
	//const int threshold_a = 100;
	//These threshold values are expressed in percentages.

	const int max_score = 100;
	//This is the maximum grade that can be scored on an exam.

	int num_students = 4;
	int* grades = new int[num_students] {60, 70, 80, 90};
	//We have four grades, because we have four students.
	//Their grades are 60, 70, 80 and 90 respectively.

	const int num_letter_grades = 4; //A, B, C, D
	int letter_grades[num_letter_grades] = {};
	//This array is our counter for the number of A's, B's, C's and D's.
	//Element 0 is our A's.
	//Element 1 is our B's.
	//Element 2 is our C's.
	//Element 3 is our D's.

	for (int i = 0; i < num_students; ++i) {

		int percentage = (grades[i] / max_score) * 100.0;
		//First, convert grade to percentage

		int index = 0;
		if (percentage >= threshold_d) {
			index = 3;
		}
		else if (percentage >= threshold_c) {
			index = 2;
		}
		else if (percentage >= threshold_b) {
			index = 1;
		}
		else {
			index = 0;
		}

		letter_grades[index]++;

	}

	delete[] grades;
	return 0;
}
The grades you found from Google are correct. :) This is really helpful and seriously, thank you so so much for this! You've been a great help and I'm sorry if I was a bit annoying with any of my questions and everything, but I thank you so much for being patient with me and helping me out :)
It needs some tinkering but hey, it's getting somewheere!
Last edited on
I'm doing something I don't normally do, which is frowned upon by the programming community, but I did your assignment just because I can still see some glaring errors, and I was in the mood.

Some things to note - do not plagiarize. Not for my sake, but for yours. Teachers have different policies on what constitutes plagiarism. It's not unheard of that a teacher will take a snippet of their student's work, punch it into google or a popular programming forum like this one, and make sure that nobody else has written it for them. If your work ends up looking too similar to somebody else's you could get in trouble.

As far as I'm concerned, you're welcome to model your project around my solution. See where you and I do things differently, perhaps learn something. I've tried to purge as many complex concepts from this snippet as possible.

Also, you're not annoying. I like answering questions.

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

int main() {

	const int threshold_fail = 69;
	const int grade_max = 100;
	const int num_letter_grades = 4;

	int letter_grades[num_letter_grades] = {};

	int counter_failed = 0;
	int counter_passed = 0;
	int grade_highest = 0;
	int grade_lowest = 0;
	int grade_average = 0;

	int num_students = 0;
	std::cout << "Enter the number of students in your class:\t";
	std::cin >> num_students;
	std::cout << std::endl;

	int* grades = new int[num_students] {};

	int input = 0;
	for (int i = 0; i < num_students; ++i) {
		std::cout << "Enter grade for student #" << (i + 1) << " of " << num_students << ":\t";
		std::cin >> input;

		grades[i] = input;

		if (input >= threshold_fail) {
			std::cout << "Student #" << (i + 1) << " has passed." << std::endl;
			counter_passed++;
		}
		else {
			std::cout << "Student #" << (i + 1) << " has failed." << std::endl;
			counter_failed++;
		}
		std::cout << std::endl;
	}

	//Determining highest grade
	for (int i = 0; i < num_students; ++i) {
		if (grades[i] > grade_highest) {
			grade_highest = grades[i];
		}
	}

	grade_lowest = grade_max;
	//Determining lowest grade
	for (int i = 0; i < num_students; ++i) {
		if (grades[i] < grade_lowest) {
			grade_lowest = grades[i];
		}
	}

	//Computing average grade
	int sum = 0;
	for (int i = 0; i < num_students; ++i) {
		sum += grades[i];
	}
	grade_average = sum / num_students;

	//Computing letter grades
	for (int i = 0; i < num_students; ++i) {

		int percentage = ((double)grades[i] / (double)grade_max) * 100.0;

		int index = 0;
		if ((percentage >= 90) && (percentage <= 100)) {
			index = 0;
		}
		else if ((percentage >= 80) && (percentage <= 89)) {
			index = 1;
		}
		else if ((percentage >= 70) && (percentage <= 79)) {
			index = 2;
		}
		else {
			index = 3;
		}

		letter_grades[index]++;
	}

	std::cout << "Average:\t" << grade_average << std::endl;
	std::cout << "Highest:\t" << grade_highest << std::endl;
	std::cout << "Lowest:\t\t" << grade_lowest << std::endl;
	std::cout << "A's:\t\t" << letter_grades[0] << std::endl;
	std::cout << "B's:\t\t" << letter_grades[1] << std::endl;
	std::cout << "C's:\t\t" << letter_grades[2] << std::endl;
	std::cout << "D's:\t\t" << letter_grades[3] << std::endl;
	std::cout << "Passing:\t" << counter_passed << std::endl;
	std::cout << "Failing:\t" << counter_failed << std::endl;

	delete[] grades;
	return 0;
}


Also, this is a convoluted, but straight-forward way of doing it. Very explicit.
Last edited on
holy wow, thank you so much! Just so you know, I am truly grateful this and I have no intention of copying this each for each, but rather using it as a guide to make it my own and figure it out. Not only would it cheat me if I just didn't do it myself, but I'd just be hurting myself in the end. Either way, thank you so much!
Topic archived. No new replies allowed.