Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-09-2003, 12:08 PM   #1 (permalink)
Apodysophilia
Regular Contributor
 
Apodysophilia's Avatar
 
Join Date: Apr 2003
Location: noWhere, PA
Posts: 104
Apodysophilia is on a distinguished road
Send a message via AIM to Apodysophilia
array max num

omg, I feel so stupid


but,


Code:
     public int maxElt(int[] ary)
     {
          int asdf = 0;
          
          for( int aryIndex = 0; getSize() > aryIndex; aryIndex++) 
          { 
              if(ary[aryIndex] > ary[(aryIndex+1)])
              {
                         
                         asdf = ary[ ary[(aryIndex+1)] ];
             
               }
               else
               {
                         asdf = ary[aryIndex];
               }
          
          } 
          return asdf;
     }
the error message
Code:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
        at TestAry.maxElt(TestAry.java:73)
i know whats wrong it is trying to get one more then the array is, But for the life of me i can't figure out what is wrong (i think i need to sleep more)if i figure it out 10min after i post this i gonna hate myself

any help would be aprecaded, the method is supposed to return the highest value in an int array
Apodysophilia is offline   Reply With Quote
Old 04-09-2003, 12:49 PM   #2 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
ary[(aryIndex+1)]

this goes out of bounds when aryIndex is equal to (getSize() - 1)
joe_bruin is offline   Reply With Quote
Old 04-09-2003, 02:17 PM   #3 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
what is getSize() sposed to do? if you just want to loop through the array:

Code:
int ints[] = new int[100];
for(int i = 0; i < ints.length; i++){
   if(ints[i] == ....)
}
you won't get index out of bounds then.
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 04-09-2003, 06:19 PM   #4 (permalink)
Apodysophilia
Regular Contributor
 
Apodysophilia's Avatar
 
Join Date: Apr 2003
Location: noWhere, PA
Posts: 104
Apodysophilia is on a distinguished road
Send a message via AIM to Apodysophilia
Quote:
Originally posted by joe_bruin
ary[(aryIndex+1)]

this goes out of bounds when aryIndex is equal to (getSize() - 1)
i know that, that is what i am tryin to fix

Quote:
Originally posted by abc123
what is getSize() sposed to do? if you just want to loop through the array:
getSize() returns the number of used slots in the array, for example the person whants a 10int array but only fills it up to 6, i am trying to pick out the largest int value out of the arrray
Apodysophilia is offline   Reply With Quote
Old 04-09-2003, 08:08 PM   #5 (permalink)
saline
I am red.
 
saline's Avatar
 
Join Date: Feb 2003
Location: Cleveland, OH
Posts: 139
saline is on a distinguished road
Ok I'm writing this on the assumption that you wrote getSize(). If this is so how are you holding empty spaces? Like if the array were empty (getSize() should return zero) then whats in the ten spaces? My thinking is that getSize() isn't doing what you think it's doing and it's always returning that every space is used so you're always going to the end of the array and then purposefully going one beyond with your code.

So assuming the array has ten slots getSize() is returning ten, aryIndex gets up to [9] (the tenth spot) and then inside your first if statment you're trying to access [10] the non-existant eleventh space of the array here:

(I'm essentailly saying here that joe_bruin was right but perhaps too brief in saying why)

Code:
   if(ary[aryIndex] > ary[(aryIndex+1)])
              {
                         
                         asdf = ary[ ary[(aryIndex+1)] ];
             
               }
Maybe I'm missing something significant here but thats my best guess for now.

I would suggest restructuring the whole algorithm where instead of comparing the present against the next value you compare the present against the past highest value.

Code:
int biggest(int[] ary)
{
  int cur_space;
  int largest = 0;
//this assumes the first space is the largest for no reason
//what-so-ever.

  for(cur_space = 0; cur_space < ary.length; cur_space++)
  {
    if(ary[cur_space] > ary[largest])
    {
      largest = cur_space;
    }
  }

return ary[largest];

}
If I've had a massive brain failure and this doesn't work or you don't get how it does or is supposed to work let me know.
saline is offline   Reply With Quote
Old 04-09-2003, 09:01 PM   #6 (permalink)
Apodysophilia
Regular Contributor
 
Apodysophilia's Avatar
 
Join Date: Apr 2003
Location: noWhere, PA
Posts: 104
Apodysophilia is on a distinguished road
Send a message via AIM to Apodysophilia
saline your code work perfectly, thank you soo much, i now see what i was doing wrong, thank you once again:rock:
Apodysophilia is offline   Reply With Quote
Old 04-09-2003, 09:02 PM   #7 (permalink)
saline
I am red.
 
saline's Avatar
 
Join Date: Feb 2003
Location: Cleveland, OH
Posts: 139
saline is on a distinguished road
And on that oh so wonderful note, I'm going to bed, rock rockity rock rock.

out of curiosity was I right about the getSize() thing or did you not check?
__________________
http://home.cwru.edu/~cak19

It's my homepage with odd little bits of javascript.
saline is offline   Reply With Quote
Old 04-09-2003, 09:08 PM   #8 (permalink)
Apodysophilia
Regular Contributor
 
Apodysophilia's Avatar
 
Join Date: Apr 2003
Location: noWhere, PA
Posts: 104
Apodysophilia is on a distinguished road
Send a message via AIM to Apodysophilia
you where right about the getSize()
Apodysophilia is offline   Reply With Quote
Old 04-09-2003, 09:14 PM   #9 (permalink)
saline
I am red.
 
saline's Avatar
 
Join Date: Feb 2003
Location: Cleveland, OH
Posts: 139
saline is on a distinguished road
I couldn't have been more right, thats awesome, I was like right squared. It's like I'm psychic, I'm like the John Edwards of debugging, reading functions that I can't even see.

Somebody stop me before my ego explodes...
__________________
http://home.cwru.edu/~cak19

It's my homepage with odd little bits of javascript.
saline is offline   Reply With Quote
Old 04-10-2003, 06:36 AM   #10 (permalink)
Apodysophilia
Regular Contributor
 
Apodysophilia's Avatar
 
Join Date: Apr 2003
Location: noWhere, PA
Posts: 104
Apodysophilia is on a distinguished road
Send a message via AIM to Apodysophilia
Quote:
Originally posted by saline
I couldn't have been more right, thats awesome, I was like right squared. It's like I'm psychic, I'm like the John Edwards of debugging, reading functions that I can't even see.

Somebody stop me before my ego explodes...
lmao, i was reading this is the lab and i just started to laugh out loud for like 10min
Apodysophilia is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
working with Array Goong MS Technologies ( ASP, VB, C#, .NET ) 3 07-22-2004 12:14 PM
breaking down an array from a form metazai PHP 12 07-09-2004 06:18 AM
Return an array with register_globals = off bdl PHP 7 03-10-2003 05:37 PM
num editing sde Lounge 3 06-17-2002 10:34 AM
adding to an array in php sde PHP 1 06-12-2002 11:04 AM


All times are GMT -8. The time now is 02:41 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting