Array sorting in Java

An array in Java that decides whether each value in the array is eithe

Page 1 of 1

14 Replies - 8901 Views - Last Post: 16 July 2010 - 08:18 PM Rate Topic: -----

#1 Wolfmanzak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 09-July 10

Array sorting in Java

Posted 16 July 2010 - 03:01 PM

Goal:
Write a program that accepts an array of ints from the user then calculates the average
and prints out each element of the array on its own line with an indication of if that number is
above or below the average.


Description:
Write a java program called Lab11.java that does the following. Prompt the user for the
number of numbers in the list. Get this list from the user one number at a time. Calculate and
display the average (truncated to an integer is fine). Then print off the list with an indication
next to each number about whether it is above, below or equal to the average. A couple of
sample runs are below (user input is blue, you cannot do this in your program.)


How many numbers: 3
number 1: 7
number 2: 9
number 3: 15
Average: 10
number 1: 7 (less)
number 2: 9 (less)
number 3: 15 (greater)
Another run:


How many numbers: 5
number 1: 10
number 2: 8
number 3: 6
number 4: 4
number 5: 2
Average: 6
number 1: 10 (greater)
number 2: 8 (greater)
number 3: 6 (equal)
number 4: 4 (less)
number 5: 2 (less)

I can already compute the average of the elements of the array as input from the user. My question is about having the program decide whether each element of the array is either above or below the computed average...as I've shown above. I think I should go about it with the use of possibly a for loop with an if statement in it but I'm not quite sure if that's the best way. Any and all suggestions are more than welcome.

Thanks,
Wolfmanzak

Here is my code so far:

import java.util.Scanner;

public class Lab11{

	public static void main (String [] args){
		Scanner keyboard = new Scanner(System.in); 
		int sumnumbers = 0;
		int numbers = 0;
				
		System.out.print("How many numbers: ");
		int numbercount = keyboard.nextInt();
			
		for(int i = 0; i < numbercount; i++){
			System.out.print("What is number " + (i + 1) + " :");
			numbers = keyboard.nextInt();
			sumnumbers = sumnumbers + numbers;
		}		
			
		System.out.println("The average of the numbers was " + (sumnumbers / numbercount));
			
		for(int i = 0; i < numbercount; i++){
		if ((i + 1) > (sumnumbers / numbercount)){
		System.out.println("Number" + (i +1) + " :");	
		}	
		}
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Array sorting in Java

#2 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: Array sorting in Java

Posted 16 July 2010 - 03:10 PM

You should store your numbers in a Array of some type. Since you need to access them again.
I would use an ArrayList<Integer>.
Was This Post Helpful? 0
  • +
  • -

#3 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Array sorting in Java

Posted 16 July 2010 - 03:53 PM

@NoobKnight, althought your suggestion is an viable option, I do believe this task seems to be aimed at introducing the OP into arrays since it has tasks such as accessing arrays and printing elements of the array.

For the average - You should create a int type variable called average and add each element from the array to it. Once it is added, divide the average with the length of the array.

Checking each number against average - You will use the variable average and access each element from the array, you will then compare it using if statements and print out the following message for that condition.

For help using Arrays, heres a tutorial from DIC on Arrays or another tutorial here to check out.
Was This Post Helpful? 1
  • +
  • -

#4 _this  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-July 10

Re: Array sorting in Java

Posted 16 July 2010 - 04:02 PM

I think you're on the right track. As others have mentioned, try using an array of ints to hold the user's numbers.
Try something like...
 
int[] numbers = new int[numbercount];



Then in your last for loop you could compare the average number to each element in the array.
if ((numbers[i]) > (sumnumbers / numbercount))
	System.out.println("Number" + (i +1) + " :"+numbers[i] + " (Greater)");


Was This Post Helpful? 0
  • +
  • -

#5 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: Array sorting in Java

Posted 16 July 2010 - 04:41 PM

View Postm-e-g-a-z, on 16 July 2010 - 04:53 PM, said:

@NoobKnight, althought your suggestion is an viable option, I do believe this task seems to be aimed at introducing the OP into arrays since it has tasks such as accessing arrays

Ooops, ya, thanks megaz, I only read his question....
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10662
  • View blog
  • Posts: 39,590
  • Joined: 27-December 08

Re: Array sorting in Java

Posted 16 July 2010 - 04:56 PM

@Wolfmanzak: Do you have to write your own sorting routine, or can you use Arrays.sort()? If you have to write your own, Selection Sort is fairly easy to implement.
Was This Post Helpful? 0
  • +
  • -

#7 Wolfmanzak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 09-July 10

Re: Array sorting in Java

Posted 16 July 2010 - 07:02 PM

I would prefer to store each element entered from the user in some type of array and then take each one of them and compare it to the computed average.

I've got the average part figured out. If someone could tell me how to store each of the values in an array that would be great help. I know what I need to do I just don't know how to implement it into Java code.

import java.util.Scanner;

public class Lab11{

	public static void main (String [] args){
		Scanner keyboard = new Scanner(System.in); 
		int sumnumbers = 0;
		int numbers = 0;
				
		System.out.print("How many numbers: ");
		int numbercount = keyboard.nextInt();
			
		
			
		for(int i = 0; i < numbercount; i++){
			System.out.print("What is number " + (i + 1) + " :");
			numbers = keyboard.nextInt();
			sumnumbers = sumnumbers + numbers;
		}		
			
		System.out.println("The average of the numbers was " + (sumnumbers / numbercount));
			
		for(int i = 0; i < numbercount; i++){
			if ((numbers) > (sumnumbers / numbercount)){
			System.out.println("Number " + (i +1) + " :" + (numbers + 1) + "(Greater}");	
			}
			if ((numbers) < (sumnumbers / numbercount)){
			System.out.println("Number " + (i +1) + " :" + (numbers + 1) + "(Less}");	 		
			}
			if ((numbers) == (sumnumbers / numbercount)){
			System.out.println("Number " + (i +1) + " :" + (numbers + 1) + "(Equal}");	 		
			}
		}	
	}
}

Was This Post Helpful? 0
  • +
  • -

#8 Wolfmanzak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 09-July 10

Re: Array sorting in Java

Posted 16 July 2010 - 07:17 PM

View PostWolfmanzak, on 16 July 2010 - 06:02 PM, said:

I would prefer to store each element entered from the user in some type of array and then take each one of them and compare it to the computed average.

I've got the average part figured out. If someone could tell me how to store each of the values in an array that would be great help. I know what I need to do I just don't know how to implement it into Java code.

import java.util.Scanner;

public class Lab11{

	public static void main (String [] args){
		Scanner keyboard = new Scanner(System.in); 
		int sumnumbers = 0;
		int numbers = 0;
				
		System.out.print("How many numbers: ");
		int numbercount = keyboard.nextInt();
			
		
			
		for(int i = 0; i < numbercount; i++){
			System.out.print("What is number " + (i + 1) + " :");
			numbers = keyboard.nextInt();
			sumnumbers = sumnumbers + numbers;
		}		
			
		System.out.println("The average of the numbers was " + (sumnumbers / numbercount));
			
		for(int i = 0; i < numbercount; i++){
			if ((numbers) > (sumnumbers / numbercount)){
			System.out.println("Number " + (i +1) + " :" + (numbers + 1) + "(Greater}");	
			}
			if ((numbers) < (sumnumbers / numbercount)){
			System.out.println("Number " + (i +1) + " :" + (numbers + 1) + "(Less}");	 		
			}
			if ((numbers) == (sumnumbers / numbercount)){
			System.out.println("Number " + (i +1) + " :" + (numbers + 1) + "(Equal}");	 		
			}
		}	
	}
}



I basically just need help with the bottom for statement. I don't know how to store the elements that I enter into an array and then compare them to the average of the numbers in that array. Any examples of code or good hints are more than welcome.

Thanks
Was This Post Helpful? 0
  • +
  • -

#9 Codebug  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 244
  • Joined: 11-October 09

Re: Array sorting in Java

Posted 16 July 2010 - 07:26 PM

View PostWolfmanzak, on 16 July 2010 - 06:02 PM, said:

I would prefer to store each element entered from the user in some type of array and then take each one of them and compare it to the computed average.

I've got the average part figured out. If someone could tell me how to store each of the values in an array that would be great help. I know what I need to do I just don't know how to implement it into Java code.

			for(int i = 0; i < numbercount; i++){
			System.out.print("What is number " + (i + 1) + " :");
			numbers = keyboard.nextInt();
			sumnumbers = sumnumbers + numbers;
		        }		
			



I think you need to take a look at this^^ part. While this is a good way to get the sum of the numbers that you will need to calculate the average, you need to store each number separately. You can use the same for loop and Scanner that you created to add the numbers individually to an array. Try something like this:

for( int i = 0; i < numberCount; i++ )
{
      System.out.print( "What is number " + (i + 1) + " :" );
      numbers[ i ] = // code omitted
      sumNumbers = sumNumbers + numbers[ i ];
      System.out.println();
}



Based on the information on Arrays that was linked earlier, and you're Scanner, can you figure out what goes on the right hand side of the assignment operator (look for the commented code) in the above code?

This post has been edited by Codebug: 16 July 2010 - 07:35 PM

Was This Post Helpful? 0
  • +
  • -

#10 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Array sorting in Java

Posted 16 July 2010 - 07:37 PM

As for storing elements in an array, it's super easy.

//scan a length with my Scanner sc;
int length = sc.nextInt();
int[] array = new int[length];

for(int i = 0; i < array.length; i++){
     //my scanner that has already been instantiated
     array[i] = sc.nextInt();
}



now the numbers entered are stored in an array. I think Mac's question is the most prominent here. Can you use Arrays.sort() or do you need to make your own sorting method?
Was This Post Helpful? 0
  • +
  • -

#11 Wolfmanzak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 09-July 10

Re: Array sorting in Java

Posted 16 July 2010 - 07:49 PM

View PostLuckless, on 16 July 2010 - 06:37 PM, said:

As for storing elements in an array, it's super easy.

//scan a length with my Scanner sc;
int length = sc.nextInt();
int[] array = new int[length];

for(int i = 0; i < array.length; i++){
     //my scanner that has already been instantiated
     array[i] = sc.nextInt();
}



now the numbers entered are stored in an array. I think Mac's question is the most prominent here. Can you use Arrays.sort() or do you need to make your own sorting method?



I'd like to use the sorting method I'm trying to create at the bottom of the program if possible. With regard to the code you provided...where in my code should I insert it...everwhere I've entered it it causes my code to basically do nothing...it compiles but when I run it, there is no prompt for the user to enter any input.
Was This Post Helpful? 0
  • +
  • -

#12 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Array sorting in Java

Posted 16 July 2010 - 07:52 PM

Well, you'd have to restructure your program. It's not really a cut and paste solution. Look at your goals:

Quote

Write a java program called Lab11.java that does the following. Prompt the user for the
number of numbers in the list. Get this list from the user one number at a time.
Calculate and
display the average (truncated to an integer is fine). Then print off the list with an indication
next to each number about whether it is above, below or equal to the average. A couple of
sample runs are below (user input is blue, you cannot do this in your program.)



My tidbit of code does the first two things for you, assuming you implement a scanner correctly
Was This Post Helpful? 0
  • +
  • -

#13 Wolfmanzak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 09-July 10

Re: Array sorting in Java

Posted 16 July 2010 - 08:03 PM

View PostLuckless, on 16 July 2010 - 06:52 PM, said:

Well, you'd have to restructure your program. It's not really a cut and paste solution. Look at your goals:

Quote

Write a java program called Lab11.java that does the following. Prompt the user for the
number of numbers in the list. Get this list from the user one number at a time.
Calculate and
display the average (truncated to an integer is fine). Then print off the list with an indication
next to each number about whether it is above, below or equal to the average. A couple of
sample runs are below (user input is blue, you cannot do this in your program.)



My tidbit of code does the first two things for you, assuming you implement a scanner correctly



Yeah, I wasn't really trying to just copy and past it...I tried a little messing around with the structure but I couldn't quite get it for some reason.

If you could break it down a bit more as in step by step it would be great help. Sorry, it's hard for me to understand code unless I can see it work step by step, I usually don't get the full understanding of it until my professor helps me complete it and I can look at the finished result.
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10662
  • View blog
  • Posts: 39,590
  • Joined: 27-December 08

Re: Array sorting in Java

Posted 16 July 2010 - 08:12 PM

You might find some of the tutorials in the Java Tutorials Section to be more helpful here. TriggaMike has a good arrays tutorial, and NeoTifa's Basic Java For N00blets tutorials are great Java primers. In fact, I think she goes over Scanner in her Part II tutorial.
Was This Post Helpful? 0
  • +
  • -

#15 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Array sorting in Java

Posted 16 July 2010 - 08:18 PM

Oh no, I didn't mean it that way. Let me post it again in two pieces:
First Piece (Scanning for the length of the array)
//scan a length with my Scanner sc;

//our scanner
Scanner sc = new Scanner(System.in);

System.out.println("How many numbers will be entered?");
//store the length in [color="#0000FF"]int length[/color]
int length = sc.nextInt();
//now we create an [color="#00FF00"]int[] array[/color] with [color="#0000FF"]length[/color] amount of storage
int[] array = new int[length];


This accomplishes your first task

Second (Getting values and storing them in int[] array)
//loops through the length of array and asks for input until there are
//no storage spaces left

for(int i = 0; i < array.length; i++){
     //my scanner that has already been instantiated
     //is used to scan in user input
     array[i] = sc.nextInt();
}



The second task is finished, now you just have to sort them and calculate the average, then you're done :bigsmile:

This post has been edited by Luckless: 16 July 2010 - 08:18 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1