0% found this document useful (0 votes)
3 views2 pages

Java Program to Rank Schools by Marks

The Java program collects names and marks of five schools from user input. It identifies and prints the top three schools based on their marks by creating new arrays that exclude the previously identified top schools. The program uses loops to find the highest marks and manage the arrays accordingly.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Java Program to Rank Schools by Marks

The Java program collects names and marks of five schools from user input. It identifies and prints the top three schools based on their marks by creating new arrays that exclude the previously identified top schools. The program uses loops to find the highest marks and manage the arrays accordingly.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import [Link].

*;

class School {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String[] schools = new String[5];
int[] marks = new int[5];

int l = 0, i, j, c = 0;

// Input school names and marks


for (i = 0; i < 5; i++) {
[Link]("Enter name of school " + (i + 1) + ": ");
schools[i] = [Link]();
[Link]("Enter marks for " + schools[i] + ": ");
marks[i] = [Link]();
[Link](); // consume newline
}

// Find 1st place


l = 0;
for (i = 1; i < [Link]; i++) {
if (marks[i] > marks[l])
l = i;
}
[Link](marks[l] + " " + schools[l]);

// Make new arrays without 1st place school


String[] schools_new = new String[[Link] - 1];
int[] marks_new = new int[[Link] - 1];

for (j = 0, i = 0; i < [Link]; i++) {


if (i == l)
continue;
schools_new[j] = schools[i];
marks_new[j] = marks[i];
j++; // FIXED: increment j
}

// Find 2nd place


l = 0;
for (i = 1; i < marks_new.length; i++) {
if (marks_new[i] > marks_new[l])
l = i;
}
[Link](marks_new[l] + " " + schools_new[l]);

// Make arrays without 2nd place school


String[] schools_old = new String[schools_new.length - 1];
int[] marks_old = new int[marks_new.length - 1];

for (j=0,i=0;i<schools_new.length;i++) {
if(i==l)
continue;
schools_old[j] = schools_new[i];
marks_old[j] = marks_new[i];
j++;
}
// Find 3rd place
l=0;
for (i = 1; i < marks_old.length; i++) {
if (marks_old[i] > marks_old[l])
l = i;
}
[Link](marks_old[l] + " " + schools_old[l]);
}
}

You might also like