Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Question 5 of the ICSE Specimen Paper 2020.
- * Specimen Paper: http://www.cisce.org/pdf/ICSE-Class-X-Specimen-Question-Papers-2020/Computer%20Applications_Specimen_2020.pdf
- */
- import java.util.Scanner;
- public class Question_5
- {
- static void sort(int[] a, String[] names) //Selection sort.
- {
- int i, j, temp;
- String tempname; //Variable created to temporarily store the name of the students in the sorting process.
- int maxvalue;
- for(i=0;i<a.length;i++)
- {
- maxvalue = i;
- for(j=i;j<a.length;j++)
- {
- if(a[j] > a[maxvalue])
- {
- temp = a[j]; //Temporarily storing the percentage.
- tempname = names[j]; //Temporarily storing the corresponding name.
- a[j] = a[maxvalue]; //Switching the percentage.
- names[j] = names[maxvalue]; //Switching the name.
- a[maxvalue] = temp; //Placing the temporarily stored percentage back.
- names[maxvalue] = tempname; //Placing the temporarily stored name back in the same position as the percentage.
- }
- }
- }
- }
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- String[] names = new String[35]; //Array to store names.
- int[] per = new int[35]; //Array to store percentages.
- int i;
- for(i=0;i<names.length;i++) //Accepting user input.
- {
- System.out.println("Please enter the name of the student.");
- names[i] = sc.next();
- System.out.println("Please enter the percentage of the student.");
- per[i] = sc.nextInt();
- }
- for(i=0;i<names.length;i++) //Displaying the names of the students along with their percentages.
- {
- System.out.println(names[i]+"\t"+per[i]);
- }
- System.out.println();
- sort(per, names); //Sorting and displaying the 10 toppers of the class.
- System.out.println("Toppers:");
- for(i=0;i<10;i++)
- {
- System.out.println(names[i]+"\t"+per[i]);
- }
- sc.close();
- }
- }
Add Comment
Please, Sign In to add comment