Click to See Complete Forum and Search --> : a very stupid question


ybh6336
01-14-2003, 02:05 PM
Hello guys
I'm having trouble with the basics of C programming. I have the following code (which I had to write to check the problem in a larger program):

#include<stdio.h>

int fibonacci(int);

int main()
{
int result, i;
for(i=0; i<=4; i++)
{
result = fibonacci(i);
printf("%d ", result);
}
return 0;
}

int fibonacci(int num)
{
int result;
if(num < 2)
{
result = 1;
}
else
{
result = fibonacci(num-1) + fibonacci(num-2);
}
return(result);
}

I want to make function 'fibonacci()' return an integer pointer instead of an integer, something like an array which has the whole fibonacci sequence. What changes do i need to make?

Thanks
-ybh6336

Stuka
01-14-2003, 06:26 PM
First, when you post code, please use the [ code] and [ /code] tags (without the spaces). Second, if you want the fibonacci function to return an array, what you'll probably need is a prototype like so:void fibonacci(int size, int* array); where the size is the number of fibonacci numbers you want returned, and array is a pointer to an array declared in the calling function (or a global, but you should avoid those when possible). Then have your function loop through the size given, filling the array with the numbers as appropriate. I'd recommend an iterative, rather than recursive, approach, as it will simplify your code in this case.

ybh6336
01-14-2003, 08:26 PM
Thanks a lot

-ybh6336