Homework question

ThePATMAN26
ThePATMAN26 used Ask the Experts™
on
Ok, this has me stumped. Looking for a solution.

"A math professor needs a grading distribution program so that he can "grade on a curve." As you know, the numeric grades in a math exam typically vary between 0 and 100 and are expressed as integer numbers. Write a program that allows the professor to enter the grades one at a time, terminating with an EOF at his or her keyboard. After EOF is entered, the program will report a list of the grades and the number of entries for each grade."

The lesson was on Arrays. I can build a program that will create an array with a fixed number of elements and populate each element in the array, but I can't think of a way to have it display a cumulative count for each instance of a grade. Also, I want the program to allow the professor to enter as many grades as he/she wants. How would I write the program so that the number of elements in the array is determined at run-time?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2009

Commented:
What you'd need is a frequency table. ie. an array of fixed size (one item for each possible grade 0 through 100). Each of the array items represents a counter - ie. the amount of times that that grade occurred. When a grade is entered, you simply increment the appropriate counter.
You can use malloc to dynamically allocate memory at run time, however, I would be surpised if you were expected to do this.

I would suggest you simply define a very large array e.g. 1000 and then build a loop to loop around these 1000 entries requesting the grades to be entered, until EOF is entered and then exit the loop.  You will need to keep a count of the number of grades stored and this will then identify the upper bound of the array.

Commented:
in c++ you might think of a map, std::map<int, int>. the first int of template types is the key, in your case the grade. the second int is the value, in your case the count.

if you had such a map defined then after deteing a grade you simply could add a grade from input by

    std::cin >> grade;
    grademap[grade]++;



OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

The array itself will store the number of times a grade is entered, it will not store the grades themselves.  That way, you will only need a static array of size 100, like Infinity suggests.

If the user enters the grade 50, you will increment the array position number 50 (or 49, if zero-indexed) by one.

To display the results, you can make a for loop that iterates through the array and prints to screen.

something like:

For each grade in the array, print the grade and the number of times it was entered.
Top Expert 2009
Commented:
>> a static array of size 100

101 ;) Since I assume grades 0 and 100 are entirely possible too.
So true Infinity, you are on the ball as always.
If indeed 0 is also a valid grade, then the array has to be of size 101 and the grade 50 would be at index 50 in the array.
Just to clarify the way using calloc function, you could require the user to enter the number of grades he enters first. Then, use the dynamic memory allocation to make an array of that size.

      int n; // number of grades entered
      scanf("%d", &n);
      int *p;

      p = (int*)calloc(n, sizeof (int));

p is a pointer but you can treat it as a array, p[0] or *p to access value store in p, p[i] or *(p + i) to access value store in p + i.
Thus, you can get the grades by running a loop and use scanf("%d", p + i)
Top Expert 2009

Commented:
For the requirements, there is no need to store all the grades entered, so there is no need for dynamic memory allocation.
Besides, the better solution is to use the array and not require the professor to know how many grades he is about to enter.  In fact, one of the requirements is to take input until eof.
If you actually do want to store the entered numbers, "malloc" an initial array to hold the first 100 or so values.  Start filling the array and, if you get to the end of the array, "realloc()" it to make it bigger, then keep on filling it.  If you get to the end of the new array, "realloc()" again, and so on.  The professor will probably get bored of entering grades before you run out of memory!
Top Expert 2009

Commented:
But why would you ? It's more complicated, more error prone, consumes more resources and is less efficient.

The frequency table approach is a lot easier (so there's less room for errors), and will quickly get the required result without risking to consume too many resources.
This is a homework question - reality and homework rarely collide!
Top Expert 2009

Commented:
That's no excuse to turn in a bad solution imo ;) Especially not if there is a better solution that is easier to implement.
I did say "If you actually do want to store the entered numbers", since that may, for some problems, be a valid thing to do.  It is, for certain problems, a good solution.  Homework answers do not necessarily need to use a "good" solution according to your llimited definiton - sometimes exploring alternatives and providing more complex solutions will show a better grasp of available technologies than a simplistic one.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial