# Swapping elements of an array help please

• 01-16-2009, 01:20 AM
ikillu
Swapping elements of an array help please
ok i have this program where i have to prepare a ten element array using the following numbers 17 38 12 19 58 92 13 16 91 76

it says i have to read each cell of the array; find and print the largest number in the array and its location.then swap the largest number in the array with the first element in the array. then print the new array list

output is
largest number 92
position 6
92 38 12 19 58..
PLease help me if you do i will do something for you this is what i have so far

import javax.swing.JOptionPane;
public class program56{
public static void main (String args[]){

int largest=0;
int c[10]={17,38,12,19,58,92,13,16,91,76}
System.out.println("Element" + "\t" + "value");
for(int x=0; x<c.length;x++)
{

System.out.print(+c[x]+" ");
if(c[x]>largest)
largest=c[x];
}

}
}
• 01-16-2009, 01:35 AM
MuslimCoder
what is ur question? :)
• 01-16-2009, 01:35 AM
ikillu
i dont know how to write the code for this program this is all i can do
• 01-16-2009, 01:37 AM
MuslimCoder
Ok. :) I would try to write it with some comments.
• 01-16-2009, 01:53 AM
CJSLMAN
Here we go...
• This is a Java forum. A forum helps people with their Java program problems. Therefore, you don't have to plead or beg for help.
• Being a forum, the help offered is voluntary and on a "as life permits" basis.
With that said, let's look at your program:
• It doesn't compile (you probably already know this)... why?
• The array is declared wrong. You don't have to specify the size of the array if you're defining the elements from the begining. For example:
Code:

`int myArray[]={1,3,5,7,9} //first odd numbers`
• Your array stament needs a semicolon (; ) at the end.

Go change the above and we'll look at how it runs later

Luck,
CJSL
• 01-16-2009, 02:03 AM
MuslimCoder
Code:

``` public class BigNumber {     public static void main(String args[])   {                 int[] n = {17, 38, 12, 19, 58, 92, 13, 16, 91, 76};     int t = 0; // temporary value holder     int largest[] = {0,0};         // largest[0] --- to store value     //largest [1] --- to store the position         // find the largest number     for(int i = 0; i <= n.length-1; i++)     {               //store value in t       t = n[i];       //compare and replace if greater       largest[0] = Math.max(t, largest[0]);       largest[1] = i; //store position                 }         //swap first with largest         t = n[0] ; //store the first value     n[0] = largest[0]; //replace first value     n[largest[1]] = t; // move first value to pos. of largest             System.out.println("Largest: " + n[0]);     System.out.println("Position: " + largest[1]);         //print array values.     for(int i = 0; i<= n.length-1; i++)     {     System.out.print(n[i]);     if(i + 1 != n.length)     {     System.out.print(" , ");     }     }                   }   }```
• 01-16-2009, 02:11 AM
CJSLMAN
Now for running...
So far your program prints the elemnts of the array and finds the highest value element.
To find the position of the highest value element (which you already know the value of)... you already have it...
Code:

`largest=c[x]; //x is the position of the highest value element`
Just like your saving off the largest value, at the same time you can save off x which is the position.
Code:

`largestPosition = x;`
Luck,
CJSL
• 01-16-2009, 02:17 AM
CJSLMAN
huh....
MC... no offense, but it's not a good idea to to give complete code solutions to a learning OP. OPs don't learn that way ... too easy to cut & paste & forget. It's much better to give them snippets, examples, links etc... and lots of explanations.

CJSL
• 01-16-2009, 02:20 AM
angryboy
logic error line 19: largest[1] = i; //store position
• 01-16-2009, 02:36 AM
MuslimCoder
true...

:)
• 01-15-2012, 09:36 PM
yesir
Re: Swapping elements of an array help please
i have to do a thing like an array{10,20,30,40,50,60,70,80,90}

and i have to swap the numbers 20 and 30 so it looks like

{10,30,20,40,50,60,70,80,90}

how do you do it?
• 01-15-2012, 09:49 PM
Fubarable
Re: Swapping elements of an array help please