Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.lang.Math;
  3.  
  4. class ArrayStatistic{
  5. public static void main(String[] args)
  6. {
  7. Scanner scan = new Scanner(System.in);
  8.  
  9. System.out.println("How long do you want the array?");
  10. int x = scan.nextInt();
  11. if ( x <= 0 ){
  12. System.out.println("Not a valid length!");
  13. } else {
  14. // takes users input and initializes the array
  15. double x1[] = new double[x];
  16. for (int i = 0; i < x1.length;i++){
  17. System.out.println("Enter a number: ");
  18. x1[i] = scan.nextDouble();
  19. }
  20.  
  21. // prints out the users array
  22.  
  23. System.out.print("Your array is {");
  24. for ( int i = 0; i < x1.length; i++){
  25. if ( i == ( x - 1)){
  26. System.out.print(x1[i] + "");
  27. } else {
  28. System.out.print(x1[i] + ", ");
  29. }
  30. }
  31. System.out.print("}");
  32.  
  33. // average of the array
  34. double a = 0;
  35. for(int i = 0; i < x1.length ; i++){
  36. a = (x1[i] + a);
  37. }
  38. System.out.println("\nThe average is " + (a / x));
  39.  
  40. // figures out the range
  41.  
  42.  
  43. double m = x1[0];
  44. double m1 = x1[0];
  45.  
  46. // min
  47. for( int i = 0; i < x1.length ; i++){
  48.  
  49. m = Math.min(m, x1[i]);
  50.  
  51.  
  52. }
  53.  
  54. // max
  55. for (int i = 0; i < x1.length ; i++){
  56.  
  57. m1 = Math.max(m1, x1[i]);
  58.  
  59. }
  60. //prints the range
  61. System.out.println("The range is " + (m1 - m));
  62.  
  63. // checks if the user inputed array is in increasing/decreasing/unsorted order
  64.  
  65. boolean sorted = true;
  66. boolean s1 = true;
  67. for (int i = 0; i < x1.length - 1; i++) {
  68. if (x1[i] > x1[i+1]) {
  69. sorted = false;
  70. break;
  71. }
  72. }
  73. for (int i = 0; i < x1.length - 1; i++) {
  74. if (x1[i] < x1[i+1]) {
  75. s1 = false;
  76. break;
  77. }
  78. }
  79. if( sorted == true){
  80. System.out.println("The array is sorted in increasing order");
  81. }else if (s1 == true){
  82. System.out.println("The array is sorted in decreasing order");
  83. } else{
  84. System.out.println("The array is unsorted");
  85. }
  86.  
  87.  
  88.  
  89. // this is the end of the if conditional statement
  90. }
  91.  
  92.  
  93.  
  94. }
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement