I am tring to get this preogram to sort an array.
/*
*This program sorts integers from lowest to highest
*/
public class syntaxfive{
public static void main(String[] args){
int[] a = new int[20];
//Initialize the array to random numbers between 0 and 99
for (int i = 0; i < a.length; i++)
a[i] = (int)(Math.random()*10);
print(a);
sort(a);
print(a);
}
public static void sort(int[] a){
int itemToInsert;
boolean stillLooking;
// On the kth pass, insert item k into its correct position
among
// the first k entries in array.
// On the kth pass, insert item k into its correct position
among
// the first k entries in array.
for (int k = 1; k < a.length; k--){
// Walk backwards through list, looking for slot to insert a[k]
int j = k - 1;
itemToInsert = a[k];
stillLooking = true;
while ((k >= 0) || stillLooking )
if (itemToInsert > a[j])
{
a[k + 1] = a[j];
k++;
}
else
stillLooking = false;
// Upon leaving loop, j + 1 is the index
// where itemToInsert belongs
a[k - 1] = itemToInsert;
}
}
static public void print(int[] a){
for (int i = 0; i < a.length; i++)
System.out.print(a[i]);
System.out.println("");
}
}
// Adam W. Bachorski
// 1. Added { to void sort.
// 2. Changed else to if.
// 3. Added (int) before math.random to make it an integer.
// 4. Added print(a) to print().
// 6. Changed double itemTo Insert to int.
// 7. Changed a[i] = (int)(Math.random()*10); to produce an int from
1-9.
// 8. Added brakcets to the if statement.
Sub: HELP PLEASE
1) Please don't SHOUT at us.
2) 'Sort an array' would be a much better subject line than
'help please'.
>I am tring to get this preogram to sort an array.
I will insert some println's to demonstrate some odd
things about this code..
>/*
>*This program sorts integers from lowest to highest
[quoted text clipped - 27 lines]
>
> for (int k = 1; k < a.length; k--)
//always enclose statements in loops, within quotes
// for the sake of code clarity. I recommend even
// for single lines statements
{
System.out.println("k:" + k);
> // Walk backwards through list, looking for slot to insert a[k]
> int j = k - 1;
> itemToInsert = a[k];
> stillLooking = true;
> while ((k >= 0) || stillLooking )
> if (itemToInsert > a[j])
> {
[quoted text clipped - 6 lines]
> // where itemToInsert belongs
> a[k - 1] = itemToInsert;
} // close the block of statements..
> }
> }
[quoted text clipped - 6 lines]
> }
>}
..actually, I suspect that will now show new and
different errors.
HTH

Signature
Andrew Thompson
http://www.athompson.info/andrew/
awbachorski@earthlink.net - 04 Apr 2007 07:14 GMT
> awbachor...@earthlink.net wrote:
>
[quoted text clipped - 84 lines]
>
> Message posted via JavaKB.comhttp://www.javakb.com/Uwe/Forums.aspx/java-general/200704/1
that didnt work
Andrew T. - 04 Apr 2007 08:49 GMT
..
>that didnt work
No. It sure did not work here, either. The output
was interesting, though, and hidden within it was
the source of at least the first errors in that code.
One thing that always works to help people read
your message, though, is to always use a single
Upper Case letter at the start of each sentence,
and end every one with a full-stop of question mark.

Signature
Andrew Thompson
http://www.athompson.info/andrew/
Ian Wilson - 04 Apr 2007 10:57 GMT
>> awbachor...@earthlink.net wrote:
>>> I am tring to get this preogram to sort an array.
>>
>> I will insert some println's to demonstrate some odd things about
>> this code..
<snip>
>> // always enclose statements in loops, within quotes
>> // for the sake of code clarity. I recommend even
>> // for single lines statements
>>
>> {
>> System.out.println("k:" + k);
<snip>
>> } // close the block of statements..
<snip>
>> actually, I suspect that will now show new and different errors.
> that didnt work
It wasn't intended to work (in that sense)! It was intended to HELP you
to find your errors! It demonstrates a common technique that programmers
use to find errors in code. It wouldn't hurt you to learn how to use
that technique.
I'd look at the output you get, think hard about it, and try to work out
why you are getting the output you do instead of the output you expect.
If you need help, I find it is almost always *essential* to include ...
- Exact compiler or run-time error messages (if any)
- actual output
- expected output
- some indication of which part you need help with.