Velocity Reviews > C++ > returning the fibonacci string separated by comma

# returning the fibonacci string separated by comma

Gavin Deane
Guest
Posts: n/a

 02-13-2007
On 13 Feb, 13:25, "dasjotre" <(E-Mail Removed)> wrote:
> On 13 Feb, 12:54, "Gavin Deane" <(E-Mail Removed)> wrote:
> > If n>1, the OP's Recursive function calls Recursive(n-1) AND
> > Recursive(n-2), so unless the recursion logic has dissolved my brain
> > (a possibility), n == 0 will happen when Recursive(n) for n==2 calls
> > Recursive(n-2).

>
> > <snip>

>
> Yes, but the OP's sollution is completely wrong. My hinted
> sollution calls Recursive recursively only once per Recursive
> (my head hurts) so n will be 0 only if Recursive is initially
> called with 0.

Ah - sorry, I missed that you were talking about your solution not the
OP's.

Gavin Deane

Daniel T.
Guest
Posts: n/a

 02-13-2007
In article <(E-Mail Removed). com>,
"mac" <(E-Mail Removed)> wrote:

> Hi,
>
> I'm trying to write a fibonacci recursive function that will return
> the fibonacci string separated by comma. The problem sounds like this:
> -------------
> Write a recursive function that creates a character string containing
> the first n Fibonacci numbers - F(n) = F(n - 1) + F(n - 2), F(0) =
> F(1) = 1 -, separated by comma. n should be given as an argument to
> the program. The recursive function should only take one parameter, n,
> and should return the string. You will not use any extra function.
>
> Do not try to optimize for space or speed. Do not assume any maximum
> length for the result string. Also, please don't use global / static
> variables.
> -----------
>
> The code must be in C. I managed to create a function that returns the
> fibonacci value for the specified 'N' as a char*, but I didn't manage
> to get the entire string separated by comma.
> This is my function:
>
> char* Recursive(int n){
> char* a = malloc(n*sizeof(char));
> if(n == 0 || n == 1)
> sprintf(a, "%d", n);
> else
> sprintf(a, "%d", atoi(Recursive(n-1)) +
> atoi(Recursive(n-2)));
> return a;
> }
>
>
> How could I get the entire string?

Has to be in C not C++? The first thing I would do is create a dynamic
string module.

// dynamic_string.h

typedef struct
{
char* begin;
char* end;
} DynamicString;

DynamicString* createString( char* with );
void releaseString( DynamicString* s );
void prependToString( DynamicString* s, char* what );
void appendToString( DynamicString* s, char* what );
void printString( DynamicString* s );

(I'll leave you to implement the above functions in the cpp file.)

Then I would use that to help solve the recursive problem.

JLS
Guest
Posts: n/a

 02-13-2007
On Feb 13, 6:51 am, "mac" <(E-Mail Removed)> wrote:
> Hi,
>
> I'm trying to write a fibonacci recursive function that will return
> the fibonacci string separated by comma. The problem sounds like this:
> -------------
> Write a recursive function that creates a character string containing
> the first n Fibonacci numbers - F(n) = F(n - 1) + F(n - 2), F(0) =
> F(1) = 1 -, separated by comma. n should be given as an argument to
> the program. The recursive function should only take one parameter, n,
> and should return the string. You will not use any extra function.
>
> Do not try to optimize for space or speed. Do not assume any maximum
> length for the result string. Also, please don't use global / static
> variables.
> -----------
>
> The code must be in C. I managed to create a function that returns the
> fibonacci value for the specified 'N' as a char*, but I didn't manage
> to get the entire string separated by comma.
> This is my function:
>
> char* Recursive(int n){
> char* a = malloc(n*sizeof(char));
> if(n == 0 || n == 1)
> sprintf(a, "%d", n);
> else
> sprintf(a, "%d", atoi(Recursive(n-1)) +
> atoi(Recursive(n-2)));
> return a;
>
> }
>
> How could I get the entire string?
> Thanks in advance for help!

Sounds like a homework assignment.

Gavin Deane
Guest
Posts: n/a

 02-13-2007
On 13 Feb, 17:54, "JLS" <(E-Mail Removed)> wrote:
> On Feb 13, 6:51 am, "mac" <(E-Mail Removed)> wrote:
> > I'm trying to write a fibonacci recursive function that will return
> > the fibonacci string separated by comma. The problem sounds like this:
> > -------------
> > Write a recursive function that creates a character string containing
> > the first n Fibonacci numbers - F(n) = F(n - 1) + F(n - 2), F(0) =
> > F(1) = 1 -, separated by comma. n should be given as an argument to
> > the program. The recursive function should only take one parameter, n,
> > and should return the string. You will not use any extra function.

>
> > Do not try to optimize for space or speed. Do not assume any maximum
> > length for the result string. Also, please don't use global / static
> > variables.
> > -----------

<snip>

> Sounds like a homework assignment.

So what? The OP hasn't tried to hide that fact (assuming it is a fact
- it does appear that way to me too) and has posted their code with a
description of the problem. It's not a precise description but it's as
good as many other posts here.

I don't think this group is for discussing all C++ language questions
except those that come up in the context of homework.

Gavin Deane