Forum Stats

  • 3,875,374 Users
  • 2,266,908 Discussions
  • 7,912,186 Comments

Discussions

Array Help

User_6D5FS
User_6D5FS Member Posts: 1 Green Ribbon
edited Sep 28, 2022 1:00PM in Java Programming

Hi everyone,

Here's the problem.

Write a program that declare an array element consisting of students’ test scores in the range 0 – 200. It should then determine the number of students having scores in each of the following ranges: 0-24, 25-49, 50-74, 75-99, 100-124, 125-149, 150-174 and 175-200. (Note: assign the following scores to get the Frequency of the scores; 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189) 


public class Group7_MidLabAct1_Problem1{

public static void main (String[]args) {


int grade[]={76,89,150,135,200,76,12,100,150,28,178,189,167,200,175,150,87,99,129,149,176,

200,87,35,157,189};


String[][]output= {{"0-24\t\t"},

  {"25-49\t\t"},

  {"50-74\t\t"},

  {"75-99\t\t"},

  {"100-124\t\t"},

  {"125-149\t\t"},

  {"150-174\t\t"},

  {"175-200\t\t"}};

for(int b=0;b<output.length;b++) {

Integer.parseInt(output[b][1]);}

System.out.println("Grades\t\tFrequency");

for(int a=0;a<grade.length;a++) {

if (grade[a] <= 0 || grade[a] <=24) 

output[0][1]=output[0][1]+1;

else if (grade[a] <= 25 || grade[a] <=49) 

output[1][1]=output[1][1]+1;

else if (grade[a] <= 50 || grade[a] <=74) 

output[2][1]=output[2][1]+1;

else if (grade[a] <= 75 || grade[a] <=99) 

output[3][1]=output[3][1]+1;

else if (grade[a] <= 100 || grade[a] <=124) 

output[4][1]=output[4][1]+1;

else if (grade[a] <= 125 || grade[a] <=149) 

output[5][1]=output[5][1]+1;

else if (grade[a] <= 150 || grade[a] <=174) 

output[6][1]=output[6][1]+1;

else if (grade[a] <= 175 || grade[a] <=200) 

output[7][1]=output[7][1]+1;

}

for(int d=0;d<output.length;d++) {

for(int e=0;e<output[d].length;e++) {

System.out.print(output[d][e]);

}

System.out.println();

}

}

}


problem of the my code instead of having the number of frequency it have per range it just show how many it is by 0 and 1


sample output of the code:

Grades Frequency

0-24 01

25-49 011

50-74 0

75-99 0111111

100-124 01

125-149 0111

150-174 011111

175-200 011111111

it should be

Grades Frequency

0-24 1

25-49 2

50-74 0

75-99 6

100-124 1

125-149 3

150-174 5

175-200 8

Total: 26

Answers

  • Subhradeep Saha-Oracle
    Subhradeep Saha-Oracle Posts: 1 Employee
    edited Oct 24, 2022 4:18AM

    output[0][1]=output[0][1]+1;

    This is actually adding '1' to the string output[0][1] since output[0][1] is of type String. You need to

    1. convert the output[0][1] to int
    2. add 1 to the converted int value
    3. convert the new value to string
    4. assign output[0][1] to this string value

    Same for other output[i][1] values.