Advertisement
mmayoub

array of counters-histogram

Jul 3rd, 2017
614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. package class170629;
  2.  
  3. import java.util.Random;
  4. import java.util.Scanner;
  5.  
  6. public class arrayHistogram {
  7.  
  8.     public static void main(String[] args) {
  9.         /*
  10.          * histogram
  11.          * input: grades of students. until the first invalid grade
  12.          * output: the number of grades, and a histogram of grades
  13.          */
  14.  
  15.         // create a scanner
  16.         Scanner s = new Scanner(System.in);
  17.  
  18.         // array for counting grades
  19.         // 0 for 0-9, 1 for 10-19, ... , 9 for 90-99, 10 for 100
  20.         int[] count = new int[11];
  21.         for (int i = 0; i < count.length; i += 1) {
  22.             count[i] = 0; // initialize counter
  23.         }
  24.         int total = 0; // total number of students
  25.  
  26.         // data source
  27.         char dataSource; // auto=computer or manual=user;
  28.         do {
  29.             System.out
  30.                     .println("select data source ('m' for manual, 'a' for auto): ");
  31.             dataSource = s.next().charAt(0);
  32.         } while (dataSource != 'a' && dataSource != 'A' && dataSource != 'm'
  33.                 && dataSource != 'M');
  34.  
  35.         if (dataSource == 'a' || dataSource == 'A') {
  36.             // computer select number of students and their grades
  37.             Random rnd = new Random();
  38.             total = 20 + rnd.nextInt(980); // number of students: 20 to 999
  39.  
  40.             for (int i = 0; i < total; i += 1) {
  41.                 int grade = rnd.nextInt(101); // get random grade
  42.                 count[grade / 10] += 1; // update counter
  43.             }
  44.         } else {
  45.             // read grades from the user
  46.             System.out
  47.                     .println("Enter grades of students (1 to 100), or any other number to exit: ");
  48.             do {
  49.                 int grade = s.nextInt();
  50.                 // if invalid grade
  51.                 if (grade < 0 || grade > 100) {
  52.                     // then stop
  53.                     break;
  54.                 }
  55.  
  56.                 // else
  57.                 count[grade / 10] += 1;
  58.                 total += 1;
  59.  
  60.             } while (true);
  61.             s.close();
  62.         }
  63.  
  64.         // printing results
  65.         System.out.println("distribution of the grades:");
  66.         System.out.println("+-----------+-----+--------+");
  67.         System.out.println("|   Range   | No. |    %   |");
  68.         System.out.println("+-----------+-----+--------+");
  69.         for (int i = 0; i < count.length; i += 1) {
  70.             int rangeFrom = 10 * i;
  71.             int rangeTo = i == 10 ? 10 * i : 10 * (i + 1) - 1;
  72.             double percent = 100 * count[i] / (double) total;
  73.  
  74.             System.out.printf("| %3d - %3d | %3d | %6.2f |\n", rangeFrom,
  75.                     rangeTo, count[i], percent);
  76.             System.out.println("+-----------+-----+--------+");
  77.         }
  78.         System.out.printf("Total number of students is %d\n", total);
  79.  
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement