user2152567
user2152567

Reputation: 11

Creating java array to convert numeric score to alphabetic grade

novice java programmer, new to arrays, working on an assignment of the following prompt:

Write a program that will plot the grade distribution of scores of a test. The scores are inputted one at a time and the loop will break when a score of 0 (zero) is entered. The output will print a * for each score in the letter grade and will place the letter grades on the horizontal axis below the graph.

My main issue is in the creation of an array that enables me to sum the number of scores in each grade (A, B, C...). I am prohibited from the use of if or switch statements in this conversion. I'm wondering where to start in the creation of this array. Thanks!

Upvotes: 0

Views: 1189

Answers (2)

Kong Huang
Kong Huang

Reputation: 11

Of course, if you don't care about memory efficiency (which you always should while coding!), you can make a new array like so:

int[] grades = new int[101];

Then whenever a user enters an input, you can do something like:

int grade = input.nextInt();
grades[grade] = grades[grade] + 1;

You can figure out the number of grades that equal A by running something like:

int A = 0;
for (int i = 91; i < 101; i++){
  A += grades[i];
}

That's what I thought of when you said you weren't allowed to use if or switch statements. Let me know if it helped. Again, terribly inefficient, but at least you keep track of all the scores you have. That's a plus.

This should be a rough runtime of O(n), but could be better I think.

Good luck!

EDIT: You can do a more efficient version of the method above by using the concept of integer division. What is integer division you may ask, it's when you divide two integers, say 10/3 and the answer might be 3.333 but java discards the fractional parts so that the answer is 3. Therefore, if you divide by 10, you can use the result to get which scores are A and so forth. For example: 92/10 = 9, 97/10 = 9, 83/10 = 8, etc. The caveat is that the score is from 91-100 for A so you have to subtract 1 before applying this concept.

This should reduce the array from 101 elements to 10 since you are only keeping track of the number in the tens digit, which is more important anyways. You may be able to further optimize this but again, this isn't my homework so I don't want to spend too much time on it. I thought of this when I woke up :).

Hope this gave you some food for thought!

Upvotes: 1

Chris Knight
Chris Knight

Reputation: 25074

Does it have to be an array? If not, a Map is a good choice for this type of scenario. The keys of the map are the various grades (A, B, C, etc) and the value of each key is an integer (or long) to hold the number of grades for that key. So, the basic logic is to get the counter from the map for the grade (i.e. key), increment it and put it back into the map.

If you don't mind using external libraries, then Guava's Multiset is an even better fit.

EDIT: OK so you need to use an array, but one challenge (if I read your post correctly) is that you can't use if or switch statements (presumably to access the array). One possible way around this is to assign 'A' to index 0, 'B' to index 1, etc. Then you can use the following notation for array indexing:

char gradeAsChar = ...;  //I'll leave this to you to get the grade as an (uppercase) char
gradesArray[gradeAsChar - 'A'] = gradesArray[gradeAsChar - 'A'] + 1;

'A' - 'A' is 0, 'B' - 'A' is 1, etc. The above, of course, is ripe for index out of bounds issues if the character is unexpected so you'll need some error handling there.

Upvotes: 1

Related Questions