-4

Could anyone possibly provide feedback on my code given below? I have done the Fibonacci series multiple times in other languages, but for some odd reason, it won't print the correct series when I code it in C. I can't seem to figure out what I did wrong.

#include <stdio.h>

int fibonacci (int n)
{
    (int i = 0; i < n; i++)
{
if (i == 0 || i == 1)
{
     printf("%d,", i);
else
{
     printf("%d,", ((i-1) + (i-2)));
}
}
}


int main () 
{
   int (*fnctPtr)(int number);
   fnctPtr = &fibonacci;
   fnctPtr(9);
   return 0;
}
user2336380
  • 5
  • 1
  • 2
  • 2
    "for" statement is missing ? – Paul Apr 30 '13 at 15:08
  • The logic in your program is all wrong, and I don't think it compiles. Fix the program so it compiles, see the output, and see if you can reason out why the output is wrong by stepping through the function you have written. Note that the traditional definition of Fibonacci uses a recurrence relation. – jxh Apr 30 '13 at 15:14
  • Thanks alot. I've worked on the code a bit more, and I've read some more about coding in C. I think I've got a gist of it now. – user2336380 May 02 '13 at 22:41

4 Answers4

0

you have this (int i = 0; i < n; i++) and you should have for (int i = 0; i < n; i++)

Luis Tellez
  • 2,785
  • 1
  • 20
  • 28
0

You are attempting to compute and print the first n fibbonaci numbers. The first fibbonaci number is 0, followed by 1, then every number after that is the sum of the prior two fibbonaci numbers.

Example: 0, 1, 1, 2, 3, 5, 8, 13, 21, ... You get the drift.

The problem with your code is that you are not walking down the fibbonacci path.

In your code: printf("%d,", ((i-1) + (i-2))); you are literally adding the indexes into an array, without having built an array or linked list.

A simple, efficient way to do this in C, without recursion or an array is this:

// Prime the first two fibbonaci numbers
int a = 0;  // prior fibbonnaci number
int b = 1;  // current fibbonacci number
// Special case, print up to first 2 fibbonacci numbers
if (n >= 1) printf("%d,", a);  // first fibbonacci number
if (n >= 2) printf("%d,", b);  // second fibbonacci number
// now for the rest of fibbonacci numbers
for (int i = 2; i < n; i++) {
    int next = a + b;
    printf("%d,", next);
    // rotate the numbers
    a = b;
    b = next;
 }
0
#include <stdio.h>
#include <stdlib.h>

void fibonacci (int n){
    int *k = malloc(n*sizeof(int));
    k[0]=0;k[1]=1;
    for(int i = 0; i < n; i++){
        if (i == 0 || i == 1)
            printf("%d,", k[i]);
        else
            printf("%d,", k[i] = k[i-1] + k[i-2]);
    }
    free(k);
}

int main () {
    void (*fnctPtr)(int number);
    fnctPtr = fibonacci;
    fnctPtr(9);
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
0

I am afraid you need to really clean up your code.The following is absolutely wrong in C.

(int i = 0; i < n; i++)

I am sure you intend a for loop here.But who knows what you had in mind? Then you have enclosed a solitary else in the if block in Fibonacci().There are mistakes galore in your program, so let me give you a program that creates the Fibonacci series based on how many numbers in the series the user wants (choice entered from keyboard).Please try to understand this program.It's very easy as the logic behind Fibonacci series is simple itself.It just that it's obvious you are very new to the C language.

#include<stdio.h>
#include<stdlib.h>

int main() 
{
    int i,l,x=1,y=1,z;
    printf("Enter how many Fibonacci numbers you want\n");
    scanf("%d",&l);

    printf("The Fibonacci series of %d numbers is\n",l);

   if(l==1)
   {
   printf("1");
   exit(0);
   }

   if(l==2)
   {printf("1+1");
   exit(0);
   }    

  printf("1+1+");

  for(i=3;i<=l;i++)
   {
   z=x+y;
   printf("%d",z);    
   x=y;
   y=z;
   if((i+1)<=l) printf("+");
   }    
}

Since you say you've implemented this series in other languages, you won't have problem started the series as 0,1,1,2,3,5....., My program goes 1,1,2,3,5... as initialized both x and y to 1 instead of initializing x as 0.Tweak the code to achieve it.

Rüppell's Vulture
  • 3,583
  • 7
  • 35
  • 49