How to sort an array in ascending order

repost. i guess i seemed needy.

Page 1 of 1

11 Replies - 3765 Views - Last Post: 16 November 2009 - 04:24 PM Rate Topic: -----

#1 lbaby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 27-October 09

How to sort an array in ascending order

Posted 15 November 2009 - 05:37 PM

Well im doing an arrays project and well these are the instructions "The elevators in our building have an 1100 lb. load limit. Determine which people in the list above get on the elevator. Print their names, weights, total weight, and how many got on." its an array of people and their weights my problem is when i run it the weight goes over the max weight which is 1100... my question is can anyone hint me on what i can do to fix this??? thanks very much!!! here is my code so far
import java.util.Scanner;

public class Arrays
{
	public static void main( String [] args)
	{
		int [] weight = {30,150,305,225,135,160,80,200,165,90,100,120};
		String [] people = {"Anne","Bob ","Ralph","Tim ","Barbara","Jane","Steve","Tom ","Mike","Shirley","Pam ","Frank"};
		for (int i=0;i<weight.length;i++)
			System.out.println(people[i] + "\t\t" + weight[i]);
		   
			int i = 0;
			int tweight= 0;
			while(tweight <1100)
			{
				tweight += weight[i];
				i++;
			}
			System.out.println();
			System.out.println( "Number of people: " + i + " With total weight: " + tweight);
	}
}






the elevators in our building have an 1100 lb. load limit. Determine which people in the list above get on the elevator. Print their names, weights, total weight, and how many got on.

what is some advice to make that happen.


all im printing is the total weight.
im guessing my brain is freezing up from trying to figure this out.
i dont want anyone to do it for me.
i just need advice.

Is This A Good Question/Topic? 0
  • +

Replies To: How to sort an array in ascending order

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: How to sort an array in ascending order

Posted 15 November 2009 - 05:43 PM

You should change this segment:
while(tweight <1100)
			{
				tweight += weight[i];
				i++;
			}



to something more like this:

while((tweight + weight[i])<1100)
			{
				tweight += weight[i];
								i++;

			}



That should guarantee that the 1,100lb weight limit isn't exceeded

Edit: woops typo

This post has been edited by erik.price: 15 November 2009 - 05:46 PM

Was This Post Helpful? 0
  • +
  • -

#3 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 311
  • Joined: 30-June 08

Re: How to sort an array in ascending order

Posted 15 November 2009 - 05:44 PM

It's your while statement, let's say the total weight is 1097, it will run the while statement again.
Thus taking tweight over 1100.

Yeah, what he ^ said, beat me to it by a second. :P

This post has been edited by jimdandy75: 15 November 2009 - 05:46 PM

Was This Post Helpful? 0
  • +
  • -

#4 lbaby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 27-October 09

Re: How to sort an array in ascending order

Posted 15 November 2009 - 05:47 PM

that was amazing advice thank you


now my next task is to do this

Rearrange these people in ascending sequence by weight and print the two arrays. Determine again how many may ride the elevator, printing out their names, weights, total weight and the number of how many people got on.

Rearrange these people in ascending sequence by name (USE A DIFFERENT SORT ALGORITHM THAN THE ONE YOU USED ) and print the two arrays. Determine again how many may ride the elevator, printing out their names, weights, total weight and the number of how many people got on.

Have the program determine which method allowed the most people to get on the elevator.


any advice. again i do not want anyone to do this for me.
i need advice.
Was This Post Helpful? 0
  • +
  • -

#5 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 311
  • Joined: 30-June 08

Re: How to sort an array in ascending order

Posted 15 November 2009 - 05:50 PM

View Postlbaby, on 15 Nov, 2009 - 04:47 PM, said:

that was amazing advice thank you


now my next task is to do this

Rearrange these people in ascending sequence by weight and print the two arrays. Determine again how many may ride the elevator, printing out their names, weights, total weight and the number of how many people got on.

Rearrange these people in ascending sequence by name (USE A DIFFERENT SORT ALGORITHM THAN THE ONE YOU USED ) and print the two arrays. Determine again how many may ride the elevator, printing out their names, weights, total weight and the number of how many people got on.

Have the program determine which method allowed the most people to get on the elevator.


any advice. again i do not want anyone to do this for me.
i need advice.

Oh crap, now it gets hard !!!!! I'm out, gotta watch the Colts game. Good Luck :D
Was This Post Helpful? 0
  • +
  • -

#6 lbaby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 27-October 09

Re: How to sort an array in ascending order

Posted 15 November 2009 - 06:00 PM

helllpppp

helllpppp
Was This Post Helpful? 0
  • +
  • -

#7 lbaby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 27-October 09

Re: How to sort an array in ascending order

Posted 15 November 2009 - 07:19 PM

import java.util.Scanner;



public class Elevator
{
	public static void main( String [] args)
	{
		int [] weight = {30,150,305,225,135,160,80,200,165,90,100,120};
		String [] people = {"Anne","Bob ","Ralph","Tim ","Barbara","Jane","Steve","Tom ","Mike","Shirley","Pam ","Frank"};
		for (int i=0;i<weight.length;i++)
			System.out.println(people[i] + "\t\t" + weight[i]);
		   
			int i = 0;
			int tweight= 0;
			while((tweight + weight[i++])<1100)
			{
				tweight += weight[i];

			}				 
			
			
			System.out.println();
			System.out.println( "Number of people: " + i + " With total weight: " + tweight);
				
	}
}



how do you sort this in ascending order
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: How to sort an array in ascending order

Posted 15 November 2009 - 07:47 PM

The Cone Snippet is full of sorts of all sorts
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: How to sort an array in ascending order

Posted 15 November 2009 - 08:14 PM

Topics merged
You have 4 posts with almost the same question
Was This Post Helpful? 0
  • +
  • -

#10 chaos386  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 28
  • Joined: 27-February 09

Re: How to sort an array in ascending order

Posted 15 November 2009 - 08:20 PM

this may help i dont realy under stand what you are asking for but this with sort by waits

import java.util.Scanner;



public class Elevator
{
	//holds the people and there waits in one object
	private People[] peps = new People[12];
	//this is your stuff i'm just lazy
	private int[] weight = {30,150,305,225,135,160,80,200,165,90,100,120};
	private String[] people = {"Anne","Bob ","Ralph","Tim ","Barbara","Jane","Steve","Tom ","Mike","Shirley","Pam ","Frank"};
	   
	//main methods this is so you dont run into static problems
	public Elevator(){
		//new stuff
		//sets up the array
		popArray();
		//prints the array
		printArray();
		System.out.println();
		//sorts the array / names
		sortByNumber();
		//prints it again
		printArray();
		System.out.println();
		
		
		 for (int i=0;i<weight.length;i++)
			System.out.println(people[i] + "\t\t" + weight[i]);
		   
			int i = 0;
			int tweight= 0;
			while((tweight + weight[i++])<1100)
			{
				tweight += weight[i];

			}				 
			
			
			System.out.println();
			System.out.println( "Number of people: " + i + " With total weight: " + tweight);
		
		
	}
	public void popArray(){
		for(int i = 0; i<peps.length; i++){
			peps[i] = new People(weight[i],people[i]);
		
		}
	}
	public void sortByNumber(){
		People temp;
		People[] tempArray = new People[peps.length];
		for(int i=0; i<peps.length; i++){
			//temp holding place 
			temp = peps[i];
			
			for(int j=i; j<peps.length; j++){
				//comapiers
				if(peps[j].weight<temp.weight){
					//puts the smallest in the temp holding
					peps[i] = peps[j];
					peps[j] = temp;
					temp = peps[i];
					
				}
				
			}
			tempArray[i] = temp;	
		}
		peps = tempArray;
	}
	public void printArray(){
		for(int i= 0; i<peps.length;i++)
		System.out.println(peps[i].name+"\t\t"+peps[i].weight);
	}
	
	public static void main( String [] args)
	{
		 new Elevator();	  
	}
}





and you make this node to work with it so you will need to comapile this file to make it run right :)

public class People{
	public int weight;
	public String name;
	
	public People(int w, String name){
			
		weight = w;
		this.name = name;
	}

}



and its as esay as that o stated working on this when you first put it up so i miised out on the post after it :) hope it still helps
Was This Post Helpful? 0
  • +
  • -

#11 lbaby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 27-October 09

Re: How to sort an array in ascending order

Posted 16 November 2009 - 10:46 AM

Okay. I do need a main and a driver and apparently they now HATE EACH OTHER.

i tried to combine.
error

here are my two codes:

import java.util.Arrays;


public class testElevator
{
	public static void main( String [] args)
	{
		private int [] weight = {30,150,305,225,135,160,80,200,165,90,100,120};
		private String [] names = {"Anne","Bob ","Ralph","Tim ","Barbara","Jane","Steve","Tom ","Mike","Shirley","Pam ","Frank"};
		for (int i=0;i<weight.length;i++)
			System.out.println(names[i] + "\t\t" + weight[i]);
				System.out.println();
		   
			int i = 0;
			int tweight= 0;
			while((tweight + weight[i++])<1100)
			{
				tweight += weight[i];

			}
			
				
			System.out.println( "Number of people: " + i + " With total weight: " + tweight);
				System.out.println(names[i] +"\t\t" + weight[i]);
				
			
			   
	Arrays.sort(names);
		System.out.println();
		  System.out.println("In Ascending order by WEIGHT:");
		  System.out.println();
	 
			Arrays.sort(weight);
		//System.out.println(Arrays.toString(weight));
			for(int x=0; x<12; x++){
					 
				System.out.println(names[x] + "\t\t" + weight[x]);
			}
				
			Arrays.sort(weight);
		System.out.println();
		  System.out.println("In Ascending order by Names:");
		  System.out.println();
	 
			Arrays.sort(weight);
		//System.out.println(Arrays.toString(names));
			for(int y=0; y<12; y++){
					 
								 
				System.out.println(names[y] + "\t\t" + weight[y]);
			}
				
		
	}
}



public class People{
	public int weight;
	public String name;
   
	public People(int w, String name){
		   
		weight = w;
		this.name = name;
	}

}




This is what I get:

how do i make them work together

----jGRASP exec: javac -g C:\Users\LauraJane\Documents\testElevator.java

testElevator.java:9: illegal start of expression
private int [] weight = {30,150,305,225,135,160,80,200,165,90,100,120};
^
testElevator.java:10: illegal start of expression
private String [] names = {"Anne","Bob ","Ralph","Tim ","Barbara","Jane","Steve","Tom ","Mike","Shirley","Pam ","Frank"};
^
2 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.



ALSO...how do i get the load limit to print their names of who fits in the elevator.
it's right here in the code

 
int i = 0;
			int tweight= 0;
			while((tweight + weight[i++])<1100)
			{
				tweight += weight[i];

			}
			
				
			System.out.println( "Number of people: " + i + " With total weight: " + tweight);
				System.out.println(names[i] +"\t\t" + weight[i]);


This post has been edited by lbaby: 16 November 2009 - 10:46 AM

Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10771
  • View blog
  • Posts: 40,113
  • Joined: 27-December 08

Re: How to sort an array in ascending order

Posted 16 November 2009 - 04:24 PM

First, you cannot declare local variables (any variable declared in a method) as public, private, protected or static. Second, if you are trying to use parallel arrays to correlate people to their respective weights, then understand that using Arrays.sort() on each array will destroy this order.

If you want to maintain the order, then I suggest creating your own Person class containing the attributes name and weight. Make sure your class Person implements the Comparable interface if you want Arrays.sort() to work. Like so:
public class Person implements Comparable{
	 private String name;
	 private double weight;

	 public Person(String n, double w){
		name = n;
		weight = w;
	   }

	public String getName(){return name;}
	public double getWeight(){return weight;}
	
	public int compareTo(Person p){
		  if(weight > p.getWeight()) return 1;
		  else if(weight == p.getWeight()) return 0;
		  return -1;
		  }
}



Then all you have to do is use an array of Persons (Person[]) and call the Arrays.sort() method on it. And because you implement Comparable in your Person class, Arrays.sort() knows to compare the Persons based on their weights only.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1