fork(1) download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. import java.util.Arrays;
  6. class Main {
  7.  
  8. public static void main (String args[]) {
  9.  
  10. int numbers[]= {1,5,-9,12,-3,89, 18,23,4,-6};
  11.  
  12. // Find minimum (lowest) value in array using loop
  13. System.out.println("Minimum Value = " + getMinValue(numbers));
  14.  
  15. // Find maximum (largest) value in array using loop
  16. System.out.println("Maximum Value = " + getMaxValue(numbers));
  17.  
  18. // ADD CODE TO CALL getAvgValue AND PRINT THE AVERAGE
  19. // Don't forget to call the avg method below ... call it just like
  20. // the two methods above ...
  21. // Add any challenge method calls here ....
  22. System.out.println("Average Value = " + getAvgValue(numbers));
  23.  
  24. // ADD CODE TO SORT THE NUMBERS IN THE ARRAY, PRINT THE SORTED ARRAY
  25. // Use the built-in array sort method on the array
  26. // ... then add a loop here (no need to call a method) to print
  27. // the now sorted array. See our lecture notes for examples.
  28. Arrays.sort(numbers);
  29. System.out.print("Sorted Array = ");
  30. for (int i = 0; i < numbers.length; i++) {
  31. System.out.print(numbers[i] + " ");
  32. }
  33. System.out.println(); // add a newline after printing array
  34. }
  35.  
  36.  
  37. // Find maximum (largest) value in array using loop
  38.  
  39. public static int getMaxValue(int[] numbers) {
  40.  
  41. int maxValue = numbers[0]; // set the first array element (index of 0) as the max
  42.  
  43. // ... remember that arrays start with an index of 0
  44. // ADD CODE TO ADD A LOOP, CHECK EACH ARRAY ELEMENT
  45. // AGAINST THE CURRENT maxValue (USE AN IF STATEMENT)
  46.  
  47. // The key is to use a loop with the length property of the array
  48. // to access and check every element after the first element. The if statement
  49. // each time will check the current max value against the current
  50. // array value. At the end of the loop, maxValue will contain
  51. // the largest number in the array.
  52.  
  53. // Access each array element starting with the second element (i = 1, not 0)
  54.  
  55. for (int i = 1; i < numbers.length; i++) {
  56. if (numbers[i] > maxValue) { // current element > current max value ?
  57. maxValue = numbers[i]; // the current element is now the max value
  58. } // end if
  59. } // end for
  60.  
  61. return maxValue; // return the largest array element value
  62. } // getMaxValue
  63.  
  64.  
  65. // Find minimum (lowest) value in array using loop
  66.  
  67. public static int getMinValue(int[] numbers) {
  68.  
  69. int minValue = numbers[0];
  70.  
  71. // ADD CODE TO ADD A LOOP, CHECK EACH ARRAY ELEMENT
  72. // AGAINST THE CURRENT minValue (USE AN IF STATEMENT)
  73.  
  74. // Hint: This is just like the for loop in the max function above,
  75. // just revise to check for min value instead of max value
  76.  
  77. for (int i = 1; i < numbers.length; i++) {
  78. if (numbers[i] < minValue) {
  79. minValue = numbers[i];
  80. }
  81. }
  82.  
  83. return minValue;
  84.  
  85. } // getMinValue
  86.  
  87.  
  88. // Find the average of an array of integers
  89.  
  90. public static double getAvgValue(int[] numbers) {
  91.  
  92. // ADD CODE TO SET UP NEEDED VARIABLES, ONE TO SUM ALL ARRAY ELEMENT VALUES, AND
  93. // AND ANOTHER ONE, average, TO BE USED TO CALCULATE THE AVERAGE OF
  94. // ALL ARRAY ELEMENT VALUES
  95.  
  96. double average = 0;
  97. double sum = 0;
  98.  
  99. // ADD CODE TO ADD A LOOP TO COMPUTE A RUNNING TOTAL OF ALL ARRAY ELEMENTS
  100.  
  101. // use a loop like in the max function, but instead of an if, add the current
  102. // array element value to sum. This will keep a running total as each element value
  103. // is added to sum as it is accessed in the loop: sum = sum + numbers[i] ;
  104.  
  105. for (int i = 0; i < numbers.length; i++) {
  106. sum = sum + numbers[i];
  107. }
  108.  
  109. // ADD CODE TO COMPUTE THE AVERAGE
  110.  
  111. // The average just takes the sum variable and divides it by the total
  112. // number of array items (i.e., numbers.length)
  113.  
  114. average = sum / numbers.length;
  115.  
  116. return average;
  117.  
  118. } // getAvgValue
  119.  
  120. // Add any Challenge methods here ...
  121.  
  122. } // Main Class
Success #stdin #stdout 0.21s 55904KB
stdin
Standard input is empty
stdout
Minimum Value = -9
Maximum Value = 89
Average Value = 13.4
Sorted Array = -9 -6 -3 1 4 5 12 18 23 89