# Assignment 2 - Q3, Q5

Posted by Anonymous User
Announcements Last Post
: Programming Students at UNISA School of Computing 06/19/2019 02:01PM
SoC Curricula 09/30/2017 01:08PM
Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
School of Computing Short Learning Programmes 11/24/2014 08:37AM
Unisa contact information 07/28/2011 01:28PM
 Anonymous User Assignment 2 - Q3, Q5 May 14, 2008 07:52PM Rating: 0
There's allot of posts going out on assignment 2, and here's mine...

Can't help thinking there is something amiss with Q3, the recursive function does seem to achieve anything.

running funcRec(5,'A') simply prints B - I get it, it converts (casts) the char v into a int, add 1 and cast this into a char before displaying it.

the recursive call manages to decrement u each round. Anyway, lemme know if there supposed to be more...

In terms of Question 5, optimise fibonacci recursive by taking the thinking behind the itterative approach. The problem here is that fibonacci requires tracking to two values. 0 1 1 2 3 5 8 13 21 ... you need to have 0+1 = 1, and 1+1=2, we all know the sequence. But to keep track of two variables when only 1 is returned by a function is a little tricky? hence the original recursive calculates previous and the one before the previous.

I've tried a few approaches and have not solved this one yet, maybe this is something stupid like

int fibonacci( int, int * )

and the function can now "return" two variables... will see what I can come up with - not looking to be spoonfed the answer, just looking for a hint...
 Re: Assignment 2 - Q3, Q5 May 16, 2008 05:17PM Registered: 11 years ago Posts: 13 Rating: 0
Q3 : Yes, As far as I can see the function is a bit stupid.

Q5 : I hate how this question was asked. I'm really not happy with the sample code. Hint from my solution... *cough* array *cough*.
 Anonymous User Re: Assignment 2 - Q3, Q5 May 16, 2008 05:37PM Rating: 0
This is what I managed to come up with... but it's skewed, each result is a fibonacci number but off by n*2...

I gave up and submitted like as is. Agree, array will work...

//=======================================
// recursive function
//=======================================
int fibonacci( int n, int &oldprev )
{
int prev;

if( n == 0 )
{
oldprev = 0;
return 0;
}
if( n == 1 )
{
oldprev = 1;
return 1;
}

prev = fibonacci( n-1, oldprev );
oldprev = oldprev+prev;

return oldprev+prev;
}

//=======================================
// driver function
//=======================================
int fibonacci( int n )
{
int oldprev;
return fibonacci( n, oldprev );
}
Sorry, only registered users may post in this forum.