HELP PLEASE

A

awbachorski

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 = (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);
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 = (int)(Math.random()*10); to produce an int from
1-9.
// 8. Added brakcets to the if statement.
 
A

Andrew T.

(e-mail address removed) wrote:

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
*/

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 = (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--)


//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])
{
a[k + 1] = a[j];
k++;
}
else
stillLooking = false;
// Upon leaving loop, j + 1 is the index
// where itemToInsert belongs
a[k - 1] = itemToInsert;

} // close the block of statements..
}
}

static public void print(int[] a){
for (int i = 0; i < a.length; i++)
System.out.print(a);
System.out.println("");

}
}


..actually, I suspect that will now show new and
different errors.

HTH

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200704/1
 
A

awbachorski

(e-mail address removed) wrote:

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
*/
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 = (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--)

//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])
{
a[k + 1] = a[j];
k++;
}
else
stillLooking = false;
// Upon leaving loop, j + 1 is the index
// where itemToInsert belongs
a[k - 1] = itemToInsert;
} // close the block of statements..
static public void print(int[] a){
for (int i = 0; i < a.length; i++)
System.out.print(a);
System.out.println("");


.actually, I suspect that will now show new and
different errors.

HTH


that didnt work
 
A

Andrew T.

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.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200704/1
 
I

Ian Wilson

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top