Guest User

ICSE Specimen Paper 2020 - Question 5

a guest
Oct 29th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.88 KB | None | 0 0
  1. /**
  2.  * Question 5 of the ICSE Specimen Paper 2020.
  3.  * Specimen Paper: http://www.cisce.org/pdf/ICSE-Class-X-Specimen-Question-Papers-2020/Computer%20Applications_Specimen_2020.pdf
  4.  */
  5. import java.util.Scanner;
  6. public class Question_5
  7. {
  8.     static void sort(int[] a, String[] names) //Selection sort.
  9.     {
  10.         int i, j, temp;
  11.         String tempname; //Variable created to temporarily store the name of the students in the sorting process.
  12.         int maxvalue;
  13.         for(i=0;i<a.length;i++)
  14.         {
  15.             maxvalue = i;
  16.             for(j=i;j<a.length;j++)
  17.             {
  18.                 if(a[j] > a[maxvalue])
  19.                 {
  20.                     temp = a[j]; //Temporarily storing the percentage.
  21.                     tempname = names[j]; //Temporarily storing the corresponding name.
  22.                    
  23.                     a[j] = a[maxvalue]; //Switching the percentage.
  24.                     names[j] = names[maxvalue]; //Switching the name.
  25.                    
  26.                     a[maxvalue] = temp; //Placing the temporarily stored percentage back.
  27.                     names[maxvalue] = tempname; //Placing the temporarily stored name back in the same position as the percentage.
  28.                 }
  29.             }
  30.         }
  31.     }
  32.    
  33.     public static void main(String[] args)
  34.     {
  35.         Scanner sc = new Scanner(System.in);
  36.         String[] names = new String[35]; //Array to store names.
  37.         int[] per = new int[35];         //Array to store percentages.
  38.         int i;
  39.        
  40.         for(i=0;i<names.length;i++) //Accepting user input.
  41.         {
  42.             System.out.println("Please enter the name of the student.");
  43.             names[i] = sc.next();
  44.             System.out.println("Please enter the percentage of the student.");
  45.             per[i] = sc.nextInt();
  46.         }
  47.        
  48.         for(i=0;i<names.length;i++) //Displaying the names of the students along with their percentages.
  49.         {
  50.             System.out.println(names[i]+"\t"+per[i]);
  51.         }
  52.         System.out.println();
  53.        
  54.         sort(per, names); //Sorting and displaying the 10 toppers of the class.
  55.        
  56.         System.out.println("Toppers:");
  57.         for(i=0;i<10;i++)
  58.         {
  59.             System.out.println(names[i]+"\t"+per[i]);
  60.         }
  61.        
  62.         sc.close();
  63.     }
  64. }
Add Comment
Please, Sign In to add comment