C++, I need to do this problem 2) Write a program and declare an array called “Exam” with 100 integers. Hence, ask a lecturer to enter the number of students in a class along with each student’s mark that should be stored in the array (use a variable n to know the number of students). The program should then calculate and display: a) the average (float) and the highest and lowest marks of the class, b) the number of grades A, B, C, D and F, according to the following table. Can you please help me fix my code? http://pastebin.com/2LyVsufa
what errors do you get when you compile it
I cant make it so that it can print in array......
To print an antire array you need to loop through it.
Also, you have a bug. You print both A and F for As.
What are you trying to print out when you print: cout << Exam[Students]; ??
Also: ``` cout << "Average : " << Average; cout << "Highest : " << highest; cout << "Lowest : " << lowest; ``` prints out everything on one line! No spaces between, just BANG! there. I think you want: ``` cout << "Average : " << Average << endl; cout << "Highest : " << highest << endl; cout << "Lowest : " << lowest << endl; ``` or: ``` cout << "Average : " << Average << " "; cout << "Highest : " << highest << " "; cout << "Lowest : " << lowest << endl; ```
If grades can only be between 0 and 100, you can pre-set the highest to -1 and the lowest to 101. Then just use an if statement to change them as needed. No need to deal with first time at all.
Hmmm... and I do not see how this does the "b) the number of grades A, B, C, D and F, according to the following table." part, and you seem to be printing the letters for reasons other than answering the question... but that is not bad. Printing is a good way to test things.
I revised it, and mine prints out this: ``` Students : 10 Enter Grades : 70 C Enter Grades : 82 B Enter Grades : 89 B Enter Grades : 61 D Enter Grades : 65 D Enter Grades : 77 C Enter Grades : 63 D Enter Grades : 71 C Enter Grades : 78 C Enter Grades : 83 B Average : 73 Highest : 89 Lowest : 61 There were: 0 As, 3 Bs, 4 Cs, 3Ds, and 0Fs. ``` NOTE: Even though there are no As or Fs, I still get higest and lowest. But there is one more bug to find. The Average is incorrect. It is supposed to be a float, but did not produce one.
but yet again, ain't you supposed to print those things within an "array" ?
Print what things?
Use casting: ``` int var = 1 float f_var = (float)var ``` It fixes your output: Students : 5 Enter Grades : 54 F Enter Grades : 66 D Enter Grades : 77 C Enter Grades : 88 B Enter Grades : 99 A Average : 76.8 Highest : 99 Lowest : 54 There were: 1 As, 1 Bs, 1 Cs, 1Ds, and 1Fs.
OK: From what it says in the question, the output should be something more like this: Students : 7 Enter Grade #1: 79 Enter Grade #2: 62 Enter Grade #3: 93 Enter Grade #4: 44 Enter Grade #5: 88 Enter Grade #6: 57 Enter Grade #7: 78 Average : 71.5714 Highest : 93 Lowest : 44 There were: 1 As, 1 Bs, 2 Cs, 1Ds, and 2Fs.
So that's an array ? It fixes your output: Students : 5 Enter Grades : 54 F Enter Grades : 66 D Enter Grades : 77 C Enter Grades : 88 B Enter Grades : 99 A Average : 76.8 Highest : 99 Lowest : 54 There were: 1 As, 1 Bs, 1 Cs, 1Ds, and 1Fs.
I am not even sure why they are using an array in this one.
Yea me neither, it's my second problem with arrays so I am not even sure what an array is supposed to look like on an actual program
Ah, this is probably just to get you used to arrays. OK. So you probably need to do two loops. The first loop to take in the array. The second loop to evaluate the array.
hm
Would it be easy for you to provide me with a tiny bit example ?
You have most of it. Just don't do it all in one loop. Put things into the array in one loop, thenm the ifs in the second loop.
hm lets see
``` for (int i=0; i < Students;i++) { cout << "Enter Grade #" << i+1 << ": "; ``` That part would be the same... well, that was my vesrion of it. Then, your cin goes into the array at location i.
In sudocode, mine is: ``` #include <iostream> using namespace std; int main() { declare a bunch of variables ask how many students for loop input grades into array for loop evaluate each student in array increment letter grade variables add up total of grades print results return 0; } ``` And I get: Number of students? 7 Enter grade for student #1: 67 Enter grade for student #2: 99 Enter grade for student #3: 76 Enter grade for student #4: 88 Enter grade for student #5: 32 Enter grade for student #6: 79 Enter grade for student #7: 81 7 students input. Now evaluating. Math completed! Average: 74.5714 Highest: 99 Lowest: 32 There were: 1 As, 2 Bs, 2 Cs, 1Ds, and 1Fs.
Any progress?
How did you fix highest and lowest variable @e.mccormick
? *
When I initialize high and low I put the high to a negative number and the low to a number over 100. Then every step I just compare those to every number. So the first number is higher than the high and lower than the low and changes both values. The second number might be higher or lower than this, so it may change one of them, so on and so forth.
Join our real-time social learning platform and learn together with your friends!