Results 1 to 12 of 12
  1. #1
    ikillu is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Default 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];
    }

    }
    }

  2. #2
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    what is ur question? :)

  3. #3
    ikillu is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Default

    i dont know how to write the code for this program this is all i can do

  4. #4
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    Ok. :) I would try to write it with some comments.

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default 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.
    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:
    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,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    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(" , ");
        }
        }
        
        
        
        
      }
      
    }

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default 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...
    Java 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.
    Java Code:
    largestPosition = x;
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default 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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  9. #9
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    logic error line 19: largest[1] = i; //store position

  10. #10
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    true...

    :)

  11. #11
    yesir is offline Member
    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    Default 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?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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

  1. comparing elements in array
    By garyscott101 in forum New To Java
    Replies: 14
    Last Post: 12-10-2008, 03:01 PM
  2. comparing array elements
    By Jeremy720 in forum New To Java
    Replies: 2
    Last Post: 10-13-2008, 02:33 AM
  3. Replies: 8
    Last Post: 08-27-2008, 10:45 PM
  4. reference to elements in array
    By Igor in forum New To Java
    Replies: 1
    Last Post: 12-14-2007, 11:56 AM
  5. Help with array of elements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 05:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •