问题
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;
}
回答1:
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.
回答2:
you have this (int i = 0; i < n; i++)
and you should have for (int i = 0; i < n; i++)
回答3:
#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;
}
回答4:
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;
}
来源:https://stackoverflow.com/questions/16303153/fibonacci-series-in-c-the-series-of-numbers-up-to-a-given-number