Link to home
Start Free TrialLog in
Avatar of ThePATMAN26
ThePATMAN26Flag for United States of America

asked on

Homework question

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?
Avatar of Infinity08
Infinity08
Flag of Belgium image

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.
Avatar of js-profi
js-profi

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]++;



ASKER CERTIFIED SOLUTION
Avatar of HalfAsleep
HalfAsleep

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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)
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!
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!
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.