Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我需要帮助调试一个基本的C程序来计算斐波那契数_C_Loops_Infinite Loop_Fibonacci - Fatal编程技术网

我需要帮助调试一个基本的C程序来计算斐波那契数

我需要帮助调试一个基本的C程序来计算斐波那契数,c,loops,infinite-loop,fibonacci,C,Loops,Infinite Loop,Fibonacci,如果有人能帮忙,我将不胜感激 取下皮肤;在int main之后-应该注意编译器错误 while1代替for-将运行无限循环这是一种可能的方法 "prog.c:3:1: error: expected identifier or '(' before '{' token { ^" 修改为使用无符号整数,并根据注释更改比较 Bobby这是斐波那契级数的工作代码 #include <stdio.h> int main() { unsigned int a; unsig

如果有人能帮忙,我将不胜感激

取下皮肤;在int main之后-应该注意编译器错误


while1代替for-将运行无限循环

这是一种可能的方法

"prog.c:3:1: error: expected identifier or '(' before '{' token
 {
 ^"
修改为使用无符号整数,并根据注释更改比较


Bobby

这是斐波那契级数的工作代码

#include <stdio.h>
int main()
{
    unsigned int a;
    unsigned int b;
    unsigned int c;
    a = 0;
    b = 0;
    c = 1;
    while(1)
    {
        if (c < b)
            break;
        printf("%u\n",c);
        a = b;
        b = c;
        c = a + b;
    }
}
例如

/*斐波那契级数*/

包括

int main

{


如何读取此特定编译器错误:

程序c:3:1:错误:应为标识符或在“{”标记{^

这说明错误发生在第3行:

long int a =0;

 long int b=1;

 long int c=1;

  int n,d=1;


   printf(" Enter the number of 

   terms ");

   scanf("%d",&n);

   do

     {


       printf("%d\n",c);

              c=b+a;

              a=b;

              b=c;

              d=d+1;


       }

            while(n>=d);

             return 0;


  }
如果你忘了在最里面的循环上加上右大括号,编译器不会说你忘了在最里面的循环上加上右大括号;相反,它会在某种程度上抱怨一行,要么是因为它到达了程序的末尾,要么是下一个函数定义,而没有看到前一个函数的}右大括号

我如何使它成为一个无限循环


无限循环的语法是while1或for;;。无论使用哪种,编译器都应该为这两种循环生成相同的代码。不过,正如其他人所说,斐波那契数很快变得非常大,对于本机整数数据类型来说太大了。计算第50个F数左右。

您的代码有一些错误,main后面的分号错误,您的算法没有输出fib序列,for循环键入错误

for (...)
{
  for (...)
  {
    for (...)
    {

顺便说一下,您应该限制for循环,否则它将溢出int变量。长无符号a,sum,last将为您提供更多的存储位。

for->whiletrue可能会解决这两个问题。还有int main;->intmain@frslm实际上,C++中只支持C++,除非使用STDBOOL。无法处理无限范围的Fibonacci数。32位int将在第50个项之前中断。这只是一个输入错误。有符号整数溢出没有很好的定义。如果变量是除<0之外的无符号int,则该操作将有效,该值应小于上一个值。
long int a =0;

 long int b=1;

 long int c=1;

  int n,d=1;


   printf(" Enter the number of 

   terms ");

   scanf("%d",&n);

   do

     {


       printf("%d\n",c);

              c=b+a;

              a=b;

              b=c;

              d=d+1;


       }

            while(n>=d);

             return 0;


  }
1: #include <stdio.h>
2: int main();
3: {           // <------ compiler is complaining about this line
for (...)
{
  for (...)
  {
    for (...)
    {
#include <stdio.h>
int main()  //;<-- doesn't contain semicolon
{
    /* use a better variable name for clarity
    int a; --> a
    int b; --> last number
    int c; --> sum
    a = 1;
    b = 0;
    */
    int a = 1;
    int last = 0;
    int sum;

    for (;;) // infite for loop needs 2 semicolons to work
    {
        printf("fib : %d\n", last);
        sum = a + last;
        last = a;
        a = sum;
    }
}