# Arrays in java:

• February 14th, 2013, 06:19 AM
puny prince
Arrays in java:
A program that analyzes a set of numbers can be very useful. Create an Analysis application that prompts the user for numbers in the range 1 through 50, terminated by a sentinel, and then performs the following analysis on the numbers
Determine the average number
Determine the maximum number
Determine the range (maximum - minimum)
Displays a histogram that shows the numbers in each five-unit range.

--- Update ---

how do i go about solving this problem?
this is what i have so far:
import java.util.Scanner;

public class Arrays {
public static void main (String [] args){

Scanner scan = new Scanner (System.in);
int array [] = new int[0];
int num;
System.out.println("Enter a number between 1 - 50 inclusive OR -1 if u wish to end");
array = num.nextInt();

//loop with sentinel value
while( num != -1)
array[0]++;

// maximum value
int maximum;
for( num=0; num<=50; num++){
if (maximum< array[num]){
maximum= array[num];
}
}

// minimum value
int minimum;
for (num=0; num >=50; num++){
if(minimum>array[num]){
minimum=array [num];
}
}

// average
int average;
int sum;
for(num=0; num<=50;num++){
sum=sum+array[num];
}
average = sum/50;

int range;
range = maximum-minimum;

}

}
• February 14th, 2013, 07:05 AM
beer-in-box
Re: Arrays in java:
I am not sure, but I think your array holds no values in it. And you try to compare the number with the values within the array, which do not exist.
And besides that, array = num.nextInt(); gives error.

To get input from user, you should do this (using your variable names);

num = scan.nextInt();

Once you define an array (int array [] = new int[0]; ) you can't change its length. It seems you are trying to do that as well.