Write a program to input name and percentage of 35 students of class X in two separate
one dimensional arrays. Arrange students details according to their percentage in the
descending order using selection sort method. Display name and percentage of first
ten toppers of the class.
Answers
The program of the given question to display name and percentage of first ten toppers of the class is as follows:
import java.util.Scanner ; // created object called scanner
class School_Students // created class named School_Students
{
public static void main(String[] args)
{
String a[ ] = new String[35]; // variable string
int b[ ] = new int[35]; // variable integer
Scanner pin = new Scanner(System.in);
for (int i = 0; i < 35; i++) // loop
{
System.out.print("Enter the name of the student " + (i + 1) + " : "); // input from user is asked
a[i] = pin.nextLine();
System.out.print("Enter the percentage of the students : "); //input from user is asked
b[i] = pin.nextInt();
pin.nextLine();
}
int X=b.length;
for (int i=0; i<l-1; i++) // selection sort
{
int pop = i;
for (int j=i+1; j<l; j++)
{
if (b[j] > p[pop])
{
pop = j;
}
}
int X1 = b[pop];
b[pop] = b[i];
b[i] = X1; // percentage is swaped
String X2 = a[pop];
a[pop] = a[i];
a[i] = X2; // name is swaped
}
System.out.println("RANK \t NAME \t PERCENTAGE");
for (int i = 0; i < 10; i++)
{
System.out.println((i+1) + " \t " + a[i] + " \t " + b[i]); // top 10 students and their percentages are printed
}
}
}
Explanation:
import Java.io.*;
public class Toppers
{
public static void main(String args[])throws IO Exception
{
BufferedReader br= new Buffered Reader(new InputStreamReader(System.in);
int p[]= new int[35];
String n[]=new string[35];
for ( int i=0;i <p.length;i++)
{
system.out.print("Name");
n[i]=br.readLine();
system.out.print("Percentage");
p[i]=Integer.ParseInt(br.readLine());
}
for(int i=0;i<p.length;i++)
{
int large=p[i];
int pos=i;
for(int j=i+1;j<p.length;j++)
{
if(p[j]>large)
{
large=p[j];
pos=j;
}
}
int temp=p[i];
p[i]=large;
p[pos]=temp;
string t=n[i];
n[i]=n[pos];
n[pos]=t;
}
System.out.println("Names and Percentages of first 35 toppers:");
for(int i=0;i<35;i++)
System.out.println(n[j]+"_"+p[i]);
}
}
}