C++, I am trying to finish 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. So that's my code so far.http://pastebin.com/E62wvaiW What am I doing wrong and the array won't show up ?? Please help ?
your cout << exams[100] << endl; is outside the while loop
Can you please copy and paste your code here? I don't think that's the case.
@inkyvoyd
also, you declared exam of type char - your directions ask for type int?
Yea true that. I just have no clue how to display "A" "B" "C" "D" "F" in the array with another way.
just use if statements to display A B C D and F if the grades range - don't store them as ABCDF
inkyvoyd is right. You declared exams to be 100 characters. They're indexed 0 - 99. cout << exams[100] << endl; says to print the 101st element of exams, which is undefined.
you should store the grades inside the int exam[100] and you need rewrite your code so the array is part of a while loop, seeing as you don't want to print a single element. basically I would do this: Write a chunk of code that handles input Write a chunk of code that counts the number of A's, B's, C's, D's, and F's in the array, and store each as a separate variable Write a chunk of code that outputs this information as a table.
"Write a chunk of code that outputs this information as a table." How do I do that?
you have loops in each part of these.
I don't know what format the table is supposed to be seeing as you didn't give the example. I won't be able to help you there until you edit that in.
ugh, you know what, I'll just write some psuedo code to give you an idea of how I would do it.
thanks man
http://pastebin.com/V33TePMZ don't even try to compile it, read it and use it as an example maybe
Global variables. Bad, bad things.
The problem with that example is that while it is workable, it defeats the purpose of the lesson. Christos has just started working with arrays, so the purpose it to force the student to use an array... no matter how useless the array is! That is why that loop should be broken into two loops. One to load the array and one to evaluate the array.
honestly, I'm just a kid with a very limited background in programming (I don't even know object orientated lol) - my code is probably runnable, but I don't expect it to have a programmer's elegance
@inkyvoyd It really is not an issue of how good a coder you are, elegance, and so on. It comes down to reading the question. "Write a program and declare an array called “Exam” with 100 integers." OK, so int Exam[100] must be in there. "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)." You are supposed to take input for an int n as the number of students. You are supposed to take in the percentage grade for all the students. Now, there is a key part right after this, "The program should then calculate and display:" You are NOT supposed to do any calculations while taking in the data. You must do it after. Therefore you must have two loops or you are not following the instructions. " a) the average (float) and the highest and lowest marks of the class," That is straightforward, find the average as a float and the highest and lowest, which would be ints. "b) the number of grades A, B, C, D and F, according to the following table." and how many of each type of mark needs to be counted up. So what is the real goal of all this? There are simple enough instructions. If I were to do things elegantly, I would do running calculations as the input is happening. That is easy because the computer operates so much faster than people. However, that is not what the question wants. Elegance goes out the window and you are required to take in a set of data separate from doing anything to it. Now, this does have some real world prescience. What if you have 50 people entering thousands of pieces of data and after they are all done you finally get to evaluate the data? This type of program is generally used to introduce a simple concept like that. Well, Christos said, in a different post, that she was just starting out with arrays. Therefore, the purpose of this task is to force the learner into iterating through an array. And because the processing is separate from the input: iterate the array twice! No elegance, just creation and use of an integer array as part of a learning process.
It has been a couple weeks since you started asking about this... so here is what I did some time back: http://dpaste.com/hold/1365204/ It is not 100% correct. I left the number of students as students, and not n. It started as your code and then I started reworking it into the answer required. I was hoping you would get to this point without seeing an answer...
And I don't know why it indented it oddly. It looked much nicer in my compiler!
Ah, tabs and spaces. Went to just spaces: http://dpaste.com/hold/1371090/ Much easier to read there... and I changed Students to n.
Join our real-time social learning platform and learn together with your friends!