Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Need help with a array sequence


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 kingpinzs   Members   -  Reputation: 109

Like
0Likes
Like

Posted 12 February 2012 - 08:04 PM

This is what I need to do

// void advance( )
// Precondition: is_item returns true.
// Postcondition: If the current item was already the last item in the
// sequence, then there is no longer any current item. Otherwise, the new
// current item is the item immediately after the original current item.

And this is what I did


void sequence::advance( )
{

if(is_item())
{

if(current_index <= used)
{
++current_index;
}
}

but it goes passed the actual array data.


How can I fix that?

I'm using c++

Sponsor:

#2 e‍dd   Members   -  Reputation: 2105

Like
0Likes
Like

Posted 12 February 2012 - 08:18 PM

Think through the algorithm in your head with very small numbers. Does it behave correctly when current_index is 0 and used is 1, prior to the call?

A face-palm should follow shortly.

#3 kingpinzs   Members   -  Reputation: 109

Like
0Likes
Like

Posted 12 February 2012 - 10:39 PM

After thinking about it for a while I came up with this.




void sequence::advance( )
{

if(is_item())
{

if(current_index <= used-1)
{
++current_index;
}

}
}

but it is still advancing passed the array that has been created.

#4 rip-off   Moderators   -  Reputation: 8516

Like
0Likes
Like

Posted 13 February 2012 - 03:51 AM

Postcondition: If the current item was already the last item in the sequence, then there is no longer any current item

How do you plan to represent the highlighted condition? In the C++ standard library, this could typically be handled by a "one past the end" iterator - which sounds similar to the situation you are in. Another option is using a sentinel value like -1 for the current_index in this case.

The main design issue then is ensuring that such a non-existent element is not accessed - only tested for existence.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS