
October 7th, 2008, 11:14 PM
#1
add int to array from smallest to largest
I have a lab that I have to add a number into an array in the correct place to have it be in the array from smallest to largest. Not just print out sorted. I did as much as I could and a guy from my class helped me on the logic on adding and sorting the array but it is not working the way it should. Please take a look at what I do have and let me know where I am going wrong. Thanks! Pam
<CODE>
public class SortedIntList extends IntList
{
protected boolean placedInArray = false;
protected int indexInArray = (numElements  1);
//
// Constructor  creates an integer list of a given size.
//
//"super" uses the constructor from IntList and really only has a print line on it
public SortedIntList(int size)
{
super(size);
}
//
// Adds an integer to the list. If the list is full,
// prints a message and does nothing.
//
public void add(int value)
{
if (numElements == list.length)
{
System.out.println("Can't add, list is full");
}
else
{
while (placedInArray == false)
{
//does this if it has walked through the array and found it to be the smallest number
if (indexInArray < 0)
{
list[0] = value;
placedInArray = true;
}
//does this if the number entered is the largest number
else if (value > list[indexInArray])
{
list[indexInArray+1] = value;
placedInArray = true;
}
//unsure if this should be just else or else if
//does this to move the current numbers over to the next [index] until it places number
else if (value <= list[indexInArray])
{
list[indexInArray+1] = list[indexInArray];
indexInArray;
}
}
}
list[numElements] = value;
numElements++;
}
}
</CODE>
<CODE>
// ****************************************************************
// ListTest.java
//
// A simple test program that creates an IntList, puts some
// ints in it, and prints the list.
//
// ****************************************************************
public class ListTest
{
public static void main(String[] args)
{
SortedIntList myList = new SortedIntList(10);
myList.add(100);
myList.add(50);
myList.add(200);
myList.add(25);
System.out.println(myList);
}
}
</CODE>
Last edited by pslr2301; October 7th, 2008 at 11:18 PM.
Reason: more info

October 7th, 2008, 11:44 PM
#2
Re: add int to array from smallest to largest
A couple of things for you to think about:
1) You are careful to check to see if the list is full at the beginning of the add() function, but look at what you are doing at the end of that function, regardless of what happens above.
2) Think about what value indexInArray and placedInArray have when you call add() the first time on your instantiated sorted list class; then think about what value they have the 2nd time you call add(), the 3rd time, etc. Remember that you cannot assume the order in which users will add the numbers  you don't know whether they are going to go before or after any previous entries.
Hope this helps point you in the right direction. If in doubt, use System.out.println() to print out information on what value your variables have at each step of the way so that you can track what it is doing.

October 8th, 2008, 10:54 AM
#3
Re: add int to array from smallest to largest
As well as the points raised by JTeagle you have another problem in that you aren't inserting the new number, you are overwriting the existing number. When you find where the new number is supposed to go you have to move all the values from that point one index position along to make a space for the new value.
I did as much as I could and a guy from my class helped me on the logic on adding and sorting the array
My advice is to throw away what you've been told to do and work it out for yourself. If you don't understand the logic of your code, debugging it becomes virtually impossible. Not to mention what will happen if your tutor asks you to explain how it works.
The problem is a lot easier than you think. Get some paper and a pencil and draw a row of boxes (to represent your array) and then work through what you need to do when writing a series of values in the boxes. For example: the first value, say 10 goes in the first box because the box is empty. The second value, say 15 is bigger than 10 so goes after it, the next box is empty so 15 goes in it. The next value, say 12 is bigger than 10 so goes after it, but it is not bigger than 15 so it has to go before 15. There is no empty box between 10 and 15 so in order to place 12 between 10 and 15 you need it move 15 to the next box. Once you have moved 15 you can place 12 in the box 15 was in. Now keeping adding numbers and see how the logic works. Once you are sure you have the rules required to solve any series of numbers then, and only then, translate it to code.
If you have any questions just ask.
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
OnDemand Webinars (sponsored)
