Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 22 of 22

Thread: Can somebody help me!

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can somebody help me!

    Can somebody tell me what actually does this in java 2d arrays :

    for (int i=0; i<RUNNERS; i++) {
    for (int j=i+1; j<RUNNERS; j++) {
    if (racer[j][0]<racer[i][0]) {
    // swap times and positions
    int temp = racer[j][0];
    int temp2 = racer[j][1];
    racer[j][0] = racer[i][0];
    racer[j][1] = racer[i][1];
    racer[i][0] = temp;
    racer[i][1] = temp2;

    thanks.
    Last edited by Burimselmani; June 2nd, 2014 at 11:21 AM.

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Can somebody help me!

    What do you think it does?

    What happened when you stepped through this with a piece of paper and a pencil?

    What happened when you wrote a little test program that runs this code?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can somebody help me!

    look sorry for not being so clearly, I have all the code of application and I know what it does but i dont know what does means those lines that i wrote above, I know its a part for sorting but i dont understand how it goes that , i need an explain .

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can somebody help me!

    i dont understand how it goes that , i need an explain
    Can you ask the author of the code to add some comments that say what the code is doing?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    BTW the ending }s are missing.

    To see what the code does, print out the contents of the array before the code in question executes and again after the code executes. The Arrays class's deepToString() method is useful.
    System.out.println("an ID "+ java.util.Arrays.deepToString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can somebody help me!

    Okej here is the code :
    import javax.swing.JOptionPane; 
     
    public class Race { 
     
    public static void main(String[] args) { 
    final int RUNNERS = 15; 
    int[][] racer = new int[RUNNERS][2]; 
     
    for (int i=0; i<RUNNERS; i++) { 
    racer[i][0] = Integer.parseInt( JOptionPane.showInputDialog( 
    "Enter racer ["+(i+1)+"] time in seconds")); 
     
    // positions 1 to 15 (unsorted times) 
    racer[i][1] = i+1; 
    } // end for 
     
    // sort array 
    for (int i=0; i<RUNNERS; i++) { 
    for (int j=i+1; j<RUNNERS; j++) { 
    if (racer[j][0]<racer[i][0]) { 
    // swap times and positions 
    int temp = racer[j][0]; 
    int temp2 = racer[j][1]; 
    racer[j][0] = racer[i][0]; 
    racer[j][1] = racer[i][1]; 
    racer[i][0] = temp; 
    racer[i][1] = temp2; 
    } // end if 
    } // end for j 
    } // end for i 
     
    System.out.printf("Winner: racer with ordinar number %d and time %3d%n", 
    racer[0][1], racer[0][0]); 
    System.out.printf("Second: racer with ordinar number %d and time %3d%n", 
    racer[1][1], racer[1][0]); 
    System.out.printf("Third: racer with ordinar number %d and time %3d%n", 
    racer[2][1], racer[2][0]); 
     
    } // end main() 
     
    } // end class Race

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can somebody help me!

    Can you print out the array before the code executes and again after it executes so we can see what the code does to the array's contents? See post#4.

    The code has lost its indentation formatting making it very hard to read.
    Please edit the code and add the proper indentations so it can be read.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can somebody help me!

    program chooses first second and third place on a race, the program needs to accepts mostly 15 racers , after it gets the time of all 15 racer it prints:
    winner: racer with ordinar number x and time xxx
    second: racer with ordinar number x and time xxx
    third: racer with ordinar number x and time xxx

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can somebody help me!

    Can you print out the array before the code in question executes and again after it executes so we can see what the code does to the array's contents?

    Also The code has lost its indentation formatting making it very hard to read.
    Please edit the code and add the proper indentations so it can be read.
    Note: I don't try to read messy code.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jun 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can somebody help me!

    The code is fine, compile it in your pc and u will see, theres nothing before the code executes , its simply 2d array with 15 racer each one with his own time (inputed by JOptionPane) , after gets all the times of racer it prints for example:

    winner: racer with ordinar number 4 and time 127
    second: racer with ordinar number 11 and time 130
    third: racer with ordinar number 2 and time 145

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can somebody help me!

    Sorry, I do not try to work with messy code. Can you fix the formatting?

    Add the println statements I suggested in post#4 and copy the output here. That will show you what the code is doing.

    Hint: for testing reduce the size of the array to 5 instead of 15.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Jun 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can somebody help me!

    //This application gets 15 racer and chooses first second and third place on a race 
    import javax.swing.JOptionPane; 
     
    public class Race { 
     
       public static void main(String[] args) { 
          final int RUNNERS = 5; 
          int[][] racer = new int[RUNNERS][2]; 
     
          for (int i=0; i<RUNNERS; i++) { 
             racer[i][0] = Integer.parseInt( JOptionPane.showInputDialog( 
                "Enter racer ["+(i+1)+"] time in seconds")); 
     
          // positions 1 to 15 (unsorted times) 
             racer[i][1] = i+1; 
          } // end for 
     
       // sort array 
          for (int i=0; i<RUNNERS; i++) { 
             for (int j=i+1; j<RUNNERS; j++) { 
                if (racer[j][0]<racer[i][0]) { 
                // swap times and positions 
                   int temp = racer[j][0]; 
                   int temp2 = racer[j][1]; 
                   racer[j][0] = racer[i][0]; 
                   racer[j][1] = racer[i][1]; 
                   racer[i][0] = temp; 
                   racer[i][1] = temp2; 
                } // end if 
             } // end for j 
          } // end for i 
     
          System.out.println("Winner: racer with ordinar number "+ (racer[0][1]) + " and time "+(racer[0][0]));
     
          System.out.println("Second: racer with ordinar number "+ (racer[1][1]) + " and time "+(racer[1][0])); 
     
          System.out.println("Third: racer with ordinar number " + (racer[2][1]) + " and time " + (racer[2][0]));   
       } // end main() 
     
    } // end class Race
    After compiled & runed outputed:
    Winner: racer with ordinar number 5 and time 23
    Second: racer with ordinar number 4 and time 34
    Third: racer with ordinar number 1 and time34

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can somebody help me!

    That formatting looks much better.
    Where is the print out showing the full contents of the array? See post#4

    In the two dim array, I assume there is a row per runner
    what are the two columns on each row for?

    Suggestion:
    change these variable names:
    temp2 to holdRunnerId
    temp to holdTime

    That will make the code more descriptive of what it is doing.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Jun 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can somebody help me!

    okej look with that suggestion you give me more imagine what those line does , i will see it to try making even much better than posting again . Thank you so much for the time , sorry that I wasnt so clear .

  14. #14
    Junior Member
    Join Date
    Jun 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can somebody help me!

    Hello guys ones again , Im talking for this code again this is an workout that my teacher gives to me, I cant send that code beacuse its to complicated can somebody help me or make it to me that code , workout said:
    Write a program that selects first, second and third place of a ski race.
    the application accepts mostly 15 skaters. The data of the racers are held on 2d array, first dimension the application needs to save the ordinal number and in the second dimension the time it took to finish the race in seconds via interactive input(JOptionPane) and finally the application shows 3 first places.

    I know maybe u cant write it from start for me but if u ll do it u ll save my life , thanks .

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can somebody help me!

    Do you have any specific java programming questions?

    How is this new problem different from the code that was posted here? It sounds the same.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Junior Member
    Join Date
    Jun 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can somebody help me!

    The problem is that code its to complicated , i need it more simple, can edit that code above to make it more simple/clearly to understand.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can somebody help me!

    Sorry, this is not a homework service. If you have any specific java programming questions, ask them.

    What part of the code is "too complicated"?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Junior Member
    Join Date
    Jun 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can somebody help me!

    This part sir :
       // sort array 
          for (int i=0; i<RUNNERS; i++) { 
             for (int j=i+1; j<RUNNERS; j++) { 
                if (racer[j][0]<racer[i][0]) { 
                // swap times and positions 
                   int temp = racer[j][0]; 
                   int temp2 = racer[j][1]; 
                   racer[j][0] = racer[i][0]; 
                   racer[j][1] = racer[i][1]; 
                   racer[i][0] = temp; 
                   racer[i][1] = temp2; 
                } // end if 
             } // end for j 
          } // end for i

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can somebody help me!

    One suggestion I made earlier(post#12) has been ignored:
    rename temp and temp2 to show what they are used for. That will make the code easier to understand.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Junior Member
    Join Date
    Jun 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can somebody help me!

    thats the case i dont even know

  21. #21
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Can somebody help me!

    There is a very useful comment in the code:
    // swap times and positions
    So I would guess that this is what the code is doing.

    You have a 2-dimensional array here, it is called "racer".
    From the given code fragment I would assume that it contains "times" and "positions" of runners.
    The code you have shown iterates over all times and positions and apparently "swaps" their values in a certain way.

    The result of all this swapping is that the times and positions of all runners are sorted in the end.
    You have to figure out now why they are sorted by this.
    I would advice you to look at an example with actual numbers and do the algorithm by hand, maybe on a piece of paper, writing down every step of the algorithm one at a time.

    If you have problems figuring it out you might want to read up on BubbleSort or other popular sorting algorithms. Last time I checked there were plenty of online tutorials (with animated examples) on the web.

  22. #22
    Junior Member
    Join Date
    Jun 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can somebody help me!

    Thanks all of you , U can close this , thanks again cheers