Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.lang.Math;
- class ArrayStatistic{
- public static void main(String[] args)
- {
- Scanner scan = new Scanner(System.in);
- System.out.println("How long do you want the array?");
- int x = scan.nextInt();
- if ( x <= 0 ){
- System.out.println("Not a valid length!");
- } else {
- // takes users input and initializes the array
- double x1[] = new double[x];
- for (int i = 0; i < x1.length;i++){
- System.out.println("Enter a number: ");
- x1[i] = scan.nextDouble();
- }
- // prints out the users array
- System.out.print("Your array is {");
- for ( int i = 0; i < x1.length; i++){
- if ( i == ( x - 1)){
- System.out.print(x1[i] + "");
- } else {
- System.out.print(x1[i] + ", ");
- }
- }
- System.out.print("}");
- // average of the array
- double a = 0;
- for(int i = 0; i < x1.length ; i++){
- a = (x1[i] + a);
- }
- System.out.println("\nThe average is " + (a / x));
- // figures out the range
- double m = x1[0];
- double m1 = x1[0];
- // min
- for( int i = 0; i < x1.length ; i++){
- m = Math.min(m, x1[i]);
- }
- // max
- for (int i = 0; i < x1.length ; i++){
- m1 = Math.max(m1, x1[i]);
- }
- //prints the range
- System.out.println("The range is " + (m1 - m));
- // checks if the user inputed array is in increasing/decreasing/unsorted order
- boolean sorted = true;
- boolean s1 = true;
- for (int i = 0; i < x1.length - 1; i++) {
- if (x1[i] > x1[i+1]) {
- sorted = false;
- break;
- }
- }
- for (int i = 0; i < x1.length - 1; i++) {
- if (x1[i] < x1[i+1]) {
- s1 = false;
- break;
- }
- }
- if( sorted == true){
- System.out.println("The array is sorted in increasing order");
- }else if (s1 == true){
- System.out.println("The array is sorted in decreasing order");
- } else{
- System.out.println("The array is unsorted");
- }
- // this is the end of the if conditional statement
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement