Joined

·
3,583 Posts

It's in Java.

import java.util.*;

public class Lab10 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int[] Numbers = new int[10];

int total = 0;

double average;

int count = 0;

int mode = 0;

int store = 0;

int x = 0;

System.out.println("Enter 10 integers:");

for(; x < Numbers.length; x++);

Numbers[x] = input.nextInt();

Arrays.sort(Numbers);

for(; x < Numbers.length; x++)

total += Numbers[x];

average = (total/Numbers.length);

for(int z = Numbers.length-1; z >= 0; z--){

if(z != 0){

if(Numbers[z] == Numbers[z-1]){

count++;

if(count > store){

store = count;

mode = Numbers[z];

}

else if(count == store)

mode = -1;

}

if(Numbers[z] != Numbers [z-1]){

count = 0;

}

}

}

for(; x < Numbers.length; x++)

System.out.print(Numbers[x] + ", ");

System.out.println();

System.out.println("The average is " + average);

if(mode != -1)

System.out.println("The mode is " + mode);

else

System.out.println("No mode");

}

}

However I get this error when I run, reffering to this one line in code.

Enter 10 integers:

1

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10

at Lab10.main(Lab10.java:16)

For this line in the code.

Numbers[x] = input.nextInt();

Any help? I also for this line,

for(int x = Numbers.length-1; x >= 0; x--){

if(x != 0){

It won't let me use X.

Help?