• Post Reply Bookmark Topic Watch Topic
  • New Topic

Swapping array indexes  RSS feed

 
Michael Pickens
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I apologize for posting so much code. I am trying to figure this problem out for my homework. I am supposed to swap the location of the smallest and largest in an array of 10 randomly generated integers ranging from 0 - 99 inclusively. I am only having trouble getting it to swap the indexes and reprint the list. It is supposed to look something like this:

Before Swapping:
myScore[0]= 85
myScore[1]= 84
...
myScore[9]= 75

The largest number is 86 and at [8]
The smallest number is 19 and at [4]

After Swapping:
myScore[0]= 85
...
myScore[4]= 86
...
myScore[8]= 19
myScore[9}= 75



Thanks for your help.
 
Michael Pickens
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I tried adding a method to take care of the swap, but when I call it from the main method it ends up not working right.... and outputting the information 3 times.

again, thanks.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Can you tell us what have you tried so far? From the code that you presented, there is nothing there that actually swaps any array elements.

Henry

Michael Pickens wrote:I apologize for posting so much code. I am trying to figure this problem out for my homework. I am supposed to swap the location of the smallest and largest in an array of 10 randomly generated integers ranging from 0 - 99 inclusively. I am only having trouble getting it to swap the indexes and reprint the list. It is supposed to look something like this:

Before Swapping:
myScore[0]= 85
myScore[1]= 84
...
myScore[9]= 75

The largest number is 86 and at [8]
The smallest number is 19 and at [4]

After Swapping:
myScore[0]= 85
...
myScore[4]= 86
...
myScore[8]= 19
myScore[9}= 75



Thanks for your help.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Pickens wrote:


What do you think each of those lines does?

You called your swap method, and it didn't do what you wanted it to. So clearly those lines of code do not do what you think they do. If examining it and walking through it "manually" doesn't show you where your error lies, you need to observe what's actually happening, so add println statements at each step to show the relevant values. Compare those to what you think they should be. Where the actual values differ from what you expect, you've found your error.

when I call it from the main method it ends up not working right


Note that ItDoesntWorkIsUseless.(⇐click) You need to TellTheDetails.(⇐click) Although in this case I think it's fairly easy to spot what the problem is.
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, please do not do too much of somebody’s work. It does them no good to be given too much of a solution, and might in fact do more harm than good. Note what it says on the title page of this forum:
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
I left your code because the error in it could be instructive.
Actually there are two errors: the other being to do with how many parameters you have.
 
Michael Pickens
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of trying to add a new method in, I simply performed the swap within the main method. Adding a println statement after each line really helped me a lot.

Thank you again for the advice on here, you guys are really helpful.

Cheers
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Pickens wrote: . . . I simply performed the swap within the main method.
Not at all a good idea, I am afraid. The best thing to do is create a utility class, like here, only you need a swapTwoElementsInArray() method. Like this:-You may wish to overload that method for the other 8 kinds of array (the last being Object[]).
Michael Pickens wrote: Adding a println statement after each line . . .
That’s a really good idea.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!