Example Program

  • Removed need for pointers.
  • Maintained a rigid block structure.
  • Had global garbage collection to clean up memory and prevent leaks.

  1. // Java Example Program
  2. // Input: An integer, listlen, where listlen is less
  3. // than 100, followed by length-integer values
  4. // Output: The number of input data that are greater than
  5. // the average of all input values
  6.  
  7. import java.io.*;
  8.  
  9. class IntSort {
  10. public static void main(String args[]) throws IOException {
  11. int listlen,
  12. counter,
  13. sum = 0,
  14. average,
  15. result = 0;
  16.  
  17. int[] intlist = new int[99];
  18.  
  19. listlen = Integer.parseInt(in.readLine());
  20.  
  21. if ((listlen > 0) && (listlen < 100)) {
  22. /* Read input into an array and compute the sum */
  23. for (counter = 0; counter < listlen; counter++) {
  24. intlist[counter] =
  25. Integer.valueOf(in.readLine()).intValue();
  26. sum += intlist[counter];
  27. }
  28.  
  29. /* Compute the average */
  30. average = sum / listlen;
  31.  
  32. /* Count the input values that are > average */
  33. for (counter = 0; counter < listlen; counter++) {
  34. if (intlist[counter] > average) result++;
  35. /* Print result */
  36. System.out.println(
  37. "\nNumber of values > average is:" + result);
  38. } //** end of then clause of if ((listlen > 0) ...
  39.  
  40. else System.out.println(
  41. "Error—input list length is not legal\n");
  42. } //** end of method main
  43. } //** end of class IntSort