• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with parallel arrays when sorting in decending order

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I have most of the program running. The goal is to have a class with array grades and names. I need to sort those in descending order. All of the tests pass except 3 and 4, which have two name and two grades. Ill post the class and the test prog. Thanks

Class



Im just wondering why it works with everything but two names.
 
Ranch Hand
Posts: 96
Eclipse IDE Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Jim!

Could you start by editing your post and putting the code within code formatting tags? Is much more readable that way, thanks. CR beat us to it :-)

I don't see much wrong yet with your code but will investigate a little more.

Cheers
Wim
 
Ranch Hand
Posts: 54
Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you get the required output for the test 5,6, and 7,8? Please post the Tester Class too .. I'll try to sort the issue.

Note: Please enclose code modules using Code tags in future...
 
Wim Vanni
Ranch Hand
Posts: 96
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's your (first) for-loop. Check how many times it runs in the case of length = 2. Possibly the other tests were succesful by accident :-)

And may I suggest to always use {} brackets when you use statements like for. If you add to the code, you'll run into trouble in no time if you don't add them even if it's only 1 line of code that's in the loop.

Cheers,
Wim
 
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Who gave you parallel arrays? That is a potential nightmare. If at all possible, change the arrays so you have one array and set up a Comparator to sort with.
You have been well-taught if you test the sizes of the arrays and throw Exceptions, but you need those tests before you try any sorting. Actually the test for empty arrays is probably unnecessary; if you pass a 0-length array, your outer for loop will look like this:

for (o = 0 - 1; o (== -1) > 1 (ie false); o--) ...

so the loop will never start, and nothing will go wrong.
I would recommend you go through the loop with a pencil and paper and see what the values are for each iteration, like that, when you are passing two elements. Then see whether you have an out-by-one error anywhere. An out-by-one will cause the loop to run once too often, or once too few times.

Also write your loops in the conventional manner
for (int o = myArray.length - 1; o > 1; o--) ...
Declare the loop indices inside the (), not before the loop.

If you can't visualise the numbers inside the loops, add some print statements inside the loops at their start. Then you can see which values of i and o are being used, but if the loop never starts, you will obviously see nothing.
 
Jim Stevens
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for all the replies. Unfortunately i cannot change from parallel arrays. Not to sure why he wants it like this.... I went through my array a few times cannot get it going. Ill keep working any let yall know.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still think it might be an out-by-one error. Count carefully with a pencil and paper how many times you go through your outer loop when you pass a 2-member array.
 
Jim Stevens
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok so i have my Sort class properly sorting the grades in descending order. I know what the problem is now. I need to somehow put the string names[] and int grades[] into the same index. I just am not sure of how to do that. Any ideas? Here is the new code
 
Jim Stevens
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
GREAT NEWS I have all tests passing except for the last one. Which is odd because it is a test to see if both arrays are equal...
I'll keep trying

edit: the error i am getting is an ArrayIndexOutofBoundsException


 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, if you are going beyond the end of an array you will suffer an ArrayIndexOutOfBoundsException. And remember a 5-element array has indices 0 1 2 3 4, not 5.

You appear to know already how to test whether two arrays are the same length; you had something in your first post. You need something like that, only at the beginning of your method.
 
Jim Stevens
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Yes, if you are going beyond the end of an array you will suffer an ArrayIndexOutOfBoundsException. And remember a 5-element array has indices 0 1 2 3 4, not 5.

You appear to know already how to test whether two arrays are the same length; you had something in your first post. You need something like that, only at the beginning of your method.



So are you saying i need another



before my for loop?
This is confusing me to no belief haha thanks.
 
Jim Stevens
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
GOT IT thank you very much....

 
reply
    Bookmark Topic Watch Topic
  • New Topic