fork 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.  
  23. // ADD CODE TO SORT THE NUMBERS IN THE ARRAY, PRINT THE SORTED ARRAY
  24.  
  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.  
  29. }
  30.  
  31.  
  32. // Find maximum (largest) value in array using loop
  33.  
  34. public static int getMaxValue(int[] numbers) {
  35.  
  36. int maxValue = numbers[0]; // set the first array element (index of 0) as the max
  37.  
  38. // ... remember that arrays start with an index of 0
  39. // ADD CODE TO ADD A LOOP, CHECK EACH ARRAY ELEMENT
  40. // AGAINST THE CURRENT maxValue (USE AN IF STATEMENT)
  41.  
  42. // The key is to use a loop with the length property of the array
  43. // to access and check every element after the first element. The if statement
  44. // each time will check the current max value against the current
  45. // array value. At the end of the loop, maxValue will contain
  46. // the largest number in the array.
  47.  
  48. // Access each array element starting with the second element (i = 1, not 0)
  49.  
  50. for (int i = 1; i < numbers.length; i++) {
  51. if (numbers[i] > maxValue) { // current element > current max value ?
  52. maxValue = numbers[i]; // the current element is now the max value
  53. } // end if
  54. } // end for
  55.  
  56. return maxValue; // return the largest array element value
  57. } // getMaxValue
  58.  
  59.  
  60. // Find minimum (lowest) value in array using loop
  61.  
  62. public static int getMinValue(int[] numbers) {
  63.  
  64. int minValue = numbers[0];
  65.  
  66. // ADD CODE TO ADD A LOOP, CHECK EACH ARRAY ELEMENT
  67. // AGAINST THE CURRENT minValue (USE AN IF STATEMENT)
  68.  
  69. // Hint: This is just like the for loop in the max function above,
  70. // just revise to check for min value instead of max value
  71.  
  72. return minValue;
  73.  
  74. } // getMinValue
  75.  
  76.  
  77. // Find the average of an array of integers
  78.  
  79. public static double getAvgValue(int[] numbers) {
  80.  
  81. // ADD CODE TO SET UP NEEDED VARIABLES, ONE TO SUM ALL ARRAY ELEMENT VALUES, AND
  82. // AND ANOTHER ONE, average, TO BE USED TO CALCULATE THE AVERAGE OF
  83. // ALL ARRAY ELEMENT VALUES
  84.  
  85. double average = 0;
  86. double sum = 0;
  87.  
  88. // ADD CODE TO ADD A LOOP TO COMPUTE A RUNNING TOTAL OF ALL ARRAY ELEMENTS
  89.  
  90. // use a loop like in the max function, but instead of an if, add the current
  91. // array element value to sum. This will keep a running total as each element value
  92. // is added to sum as it is accessed in the loop: sum = sum + numbers[i] ;
  93.  
  94. // ADD CODE TO COMPUTE THE AVERAGE
  95.  
  96. // The average just takes the sum variable and divides it by the total
  97. // number of array items (i.e., numbers.length)
  98.  
  99. return average;
  100.  
  101. } // getAvgValue
  102.  
  103. // Add any Challenge methods here ...
  104.  
  105. } // Main Class
Success #stdin #stdout 0.15s 55664KB
stdin
Standard input is empty
stdout
Minimum Value = 1
Maximum Value = 89