0

If i have 2 parallel arrays. One is is short[] and other String[]. short has a range of marks. short[95, 96, 95, 99, 100]. String has names of students [Mark, Steve, Emma, Rachael, Justin] They have already been declared and are corresponding. I know how to sort alphabetically and according marks. The question asks me to print out " * " for the times a student has received a grade. The user does not put any input in. for example: This would be the output.

100 = * (As only 1 person got a 100)
99 = *
96 = *
95 = **

I have to get to that output without any user inputs. I kinda think i have an idea of how to do it but im pretty lost. I dont even know how to start. All i know is a counter has to be used but that would only work if a user inputs a mark. I dont know how to do it without any input.

2 Answers2

0

assuming - no of students = no

and your 2 arrays are

String [] students;

and

short [] marks.

Create a third array

int [] count = new int[101];


int i = 0;

for(i = 0; i < no; ++i)
    ++count[marks[i]];

count array will contain no of times each mark occurs i.e. count[50] will contain no of students who got 50.

You can print it out as

for(int i = 0; i < no; ++i)
    System.out.println(i + " = " + count[i]);
user93353
  • 13,733
  • 8
  • 60
  • 122
0

Use an array, such that the 100th index corresponds to the grade 100, the 99th index corresponds to the grade 99, etc. This way if somebody gets a grade g you can just increment the gth element of the array.

Zim-Zam O'Pootertoot
  • 17,888
  • 4
  • 41
  • 69