Results 1 to 12 of 12
- 01-16-2009, 12:20 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 2
- Rep Power
- 0
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;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class program56{
public static void main (String args[]){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
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, 12:35 AM #2
Senior Member
- Join Date
- Jan 2009
- Posts
- 119
- Rep Power
- 0
what is ur question? :)
- 01-16-2009, 12:35 AM #3
Member
- Join Date
- Jan 2009
- Posts
- 2
- Rep Power
- 0
i dont know how to write the code for this program this is all i can do
- 01-16-2009, 12:37 AM #4
Senior Member
- Join Date
- Jan 2009
- Posts
- 119
- Rep Power
- 0
Ok. :) I would try to write it with some comments.
- 01-16-2009, 12:53 AM #5
Here we go...
First some comments:
- 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.
- 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:
Java 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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-16-2009, 01:03 AM #6
Senior Member
- Join Date
- Jan 2009
- Posts
- 119
- Rep Power
- 0
Java 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, 01:11 AM #7
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...
Just like your saving off the largest value, at the same time you can save off x which is the position.Java Code:largest=c[x]; //x is the position of the highest value element
Luck,Java Code:largestPosition = x;
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-16-2009, 01:17 AM #8
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.
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-16-2009, 01:20 AM #9
logic error line 19: largest[1] = i; //store position
- 01-16-2009, 01:36 AM #10
Senior Member
- Join Date
- Jan 2009
- Posts
- 119
- Rep Power
- 0
true...
:)
- 01-15-2012, 08:36 PM #11
Member
- Join Date
- Jan 2012
- Posts
- 1
- Rep Power
- 0
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?
-
Re: Swapping elements of an array help please
Please don't hijack and resurrect an old thread but instead re-ask your question in a new thread. Thank you for your cooperation in this.
Similar Threads
-
comparing elements in array
By garyscott101 in forum New To JavaReplies: 14Last Post: 12-10-2008, 03:01 PM -
comparing array elements
By Jeremy720 in forum New To JavaReplies: 2Last Post: 10-13-2008, 02:33 AM -
How to check whether two elements are available in an array?
By venkatteshb in forum New To JavaReplies: 8Last Post: 08-27-2008, 10:45 PM -
reference to elements in array
By Igor in forum New To JavaReplies: 1Last Post: 12-14-2007, 11:56 AM -
Help with array of elements
By zoe in forum New To JavaReplies: 1Last Post: 07-24-2007, 05:33 PM


LinkBack URL
About LinkBacks

Bookmarks