Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 23rd, 2006, 4:49 PM   #1
starrboy
Newbie
 
Join Date: Apr 2006
Posts: 5
Rep Power: 0 starrboy is on a distinguished road
Recursion

I'm studying programming theory at the moment, and one of the questions on my next assignment is about recursion. I have to check if a vector is sorted and return true.

The function returns true if the vector is empty and also returns true if the vector has length = 1.

I need to do the check recursively, any pointers in the right direction.

Quote:
Function SORTED(v in Vector of Int)return in Bool
pre true.
Post If the vector v is sorted (so that the integers in v are in increasing order) then the returned value is true. If v is not sorted then the returned value is false. (If v is empty or just contains one item, then true is returned.)

If the vector is empty or just contains on item then true is returned, this is a stopping condition.
thats the specification of the function.

Any pointers in the right direction would be appreciated.
starrboy is offline   Reply With Quote
Old Apr 23rd, 2006, 4:57 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Have the function divide the vector into two and call itself for each half. We won't write the code for you. If you decide to post code or ask for additional help, be sure and read the forum's FAQ/rules first.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 23rd, 2006, 5:01 PM   #3
starrboy
Newbie
 
Join Date: Apr 2006
Posts: 5
Rep Power: 0 starrboy is on a distinguished road
Thanks for the pointer. I don't want the code written for me, ain't gonna learn much that way, just need a different way to look at the problem, which I think you may have provided for me. Thanks for the help

Starrboy
starrboy is offline   Reply With Quote
Old Apr 30th, 2006, 2:26 PM   #4
starrboy
Newbie
 
Join Date: Apr 2006
Posts: 5
Rep Power: 0 starrboy is on a distinguished road
Wink sorted

Sorted it out. Just had to check if the penultimate value in the vector was greater than the last value in the vector, then knock of the last value in the vector and use this as the argument when the function calls itself.


Starrboy
starrboy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:08 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC