C斐波那契和方程问题与我的代码

  • 本文关键字:问题 代码 方程 c
  • 更新时间 :
  • 英文 :


大家好,我是C编程新手,这是我的C测试问题。我真的很努力,但我找不到解决方案或我的代码有什么问题。这个方程块不能用。如有任何帮助,不胜感激

编写一个C程序,显示下面的菜单并执行相应的操作,可根据所选菜单项进行。为每个菜单项编写单独的函数,并根据选择。菜单:(1)计算输入数字的斐波那契值;(2)计算公式(从用户处获取R值);(3)从用户处获取4个数字,按升序排序输入您的选择:

当用户输入数字(1、2或3)时,将调用相应的函数执行操作。如果用户想继续,则输入-1菜单将再次出现在屏幕上。

问题方程Pic

#include <stdio.h>
#include <stdlib.h>
int fibonacci(int numForFibonacci)
{
int i = 0;
int j = 1;
int k = 1;
for (int b = 1; b <= numForFibonacci; b++)
{
k = i + j;
printf("%d n", i);
i = j;
j = k;
printf("%d ", k);
}
printf("n");
return k;
}
float theEquation(float numR)
{
float multipl, result = 1;
for (int i = 1; i <= numR; i++)
{
multipl = (2i + 4) / (i * i);
result *= multipl;
}
printf("Result is %f", result);
return result;
}
int ascending(int n)
{
int k = 0, i = 0, j = 0, number[4];
for (int w = 0; w < n; w++)
scanf("%d", &number[w]);
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] > number[j])
{
k = number[i];
number[i] = number[j];
number[j] = k;
}
}
}
for (i = 0; i < n; ++i)
printf("n%d n", number[i]);
return i;
}
int main()
{
int choice, choiceFibo;
float choiceR;
do
{
printf("[1]Fibonacci calculationn");
printf("[2]calculating the equation: (2k+4)/(k*k)n");
printf("[3]sorting numbers in ascending ordern");
scanf("%d", &choice);
if (choice == 1)
{
printf("enter a number for fibonaccin ");
scanf("%d", &choiceFibo);
fibonacci(choiceFibo);
}
else if (choice == 2)
{
printf("[2]enter a number for (2k+4)/(k*k)n");
scanf("%f", &choiceR);
theEquation(choiceR);
}
else
{
printf("[3]enter four numbers to sort them in ascending ordern");
ascending(4);
}
printf("If you want to do another calculation enter '-1'.n");
scanf("%d", &choice);
} while (choice == -1);
return 0;
}

TL;DR:两个主要问题:

  • <integer>i在GNU C中表示复数,不是乘法。
  • 整数除法不是你所期望的

你的问题在这一行:

multipl = (2i + 4) / (i * i);

您可能认为2i会计算为2 * i,但在C中,2i是一个复整型。

可以在这里看到:

#include <stdio.h>
int main()
{
int i=1;
printf("%d", 2i);
return 0;
}

您希望它打印2,但我们得到一个警告。

使用onlinegdb编译:

warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘complex int’

第二个问题:整数除法。另一个答案已经很好地涵盖了它,但是为了自给自足的答案:

在C语言中,整数除法找到整数解并丢弃余数。

2/5 = 0 
6/5 = 1
10/5 = 2
10/4 = 2
10.0 / 4 = 2.5
(double)10/4 = 2.5

你需要键入"coerce"分子或分母中的一个值为float/double,这将强制整个分数为float/double。

您可以使用2.0*i(double)2*i来执行此操作。两种方法都可以,但是第二种方法对于可读性来说更显式。

要解决这个问题,只需将2i更改为(double)2*i

只需要三次编辑就可以获得干净的编译:

  • int main()更改为int main(void)
  • (2i + 4) / (i * i)更改为(2*i + 4) / (i * i)
  • int ... number[4];更改为int ... number[4]={0};

如果在编译时没有看到类似以下内容,则打开所有警告:

>  s0_15.c - 4 warnings   
>       28, 28    warning: implicit conversion discards imaginary component: '_Complex int' to 'float'    
>       45, 17    warning: variable 'number[i]' may be uninitialized when used here 
>       38, 5     note: variable 'number' is declared here   
>       54, 27    warning: variable 'number[i]' may be uninitialized when used here 
>       38, 5     note: variable 'number' is declared here   
>       58, 5     warning: function declaration isn't a prototype.

为了确保计算准确,请参见您帖子下的另一个答案中的整数除法。

最新更新