提问人:Spongebuild 提问时间:10/14/2023 最后编辑:Vlad from MoscowSpongebuild 更新时间:10/14/2023 访问量:82
在 C 语言中获取函数正确计算和循环的麻烦
Trouble with getting a function to calculate and loop correctly in C
问:
我需要制作一个程序来将用户对迭代的输入转换为公式:PI=4(1/1 -(1/3)+(1/5) ...重复多次。到目前为止,我的问题是它没有准确地接受我的输入。
基本上,我的方法是尝试将分母中的分子和分母与 x 分开,并尝试让它加 2,但我什至还没有弄清楚 - + 旋转,因为我想确保我所拥有的首先起作用。
我的第一个问题是它得到的是随机数,但后来我将变量类型更改为浮点数,这解决了这个问题,但现在一切都是直 0。
int pi_formula(float numerator,int x,int n)
{
numerator=1.0;
x=1;
float fraction= numerator/x;
float total;
printf("%lf",&fraction);
for (int i = 1; i <= n; i++) // loop goes for amount of user input times
do
{
fraction;
return(total);
x+= 2;
} while (1);
printf("Estimated PI is : %f",&total);
}
当我运行此代码和行时
printf("%lf",&fraction);
将返回 0.000000。
我甚至无法判断我是否正确编写了循环,因为我似乎无法识别我的输入。
以下是原型和主要供参考。
int pi_formula(float numerator,int x,int n);
int main ()
{
// Problem 2
float numerator;
int x;
int n;
printf("ESTIMATE PI\n");
printf("Enter number of iterations:");
scanf("%i",&n);
pi_formula(numerator,x,n);
system("pause");
}
我曾尝试设置 printf 调用来检查输入,希望看到我输入的内容,但这也出现了 0。
答:
0赞
Vlad from Moscow
10/14/2023
#1
对于初学者来说,这个函数声明
int pi_formula(float numerator,int x,int n)
至少没有意义,因为它具有返回类型而不是浮动类型和冗余数量的参数。int
该函数至少应声明为
double pi_formula( unsigned int n );
你的函数什么都不返回,尽管它的返回类型不是 this while 循环void
do
{
fraction;
return(total);
x+= 2;
} while (1);
其中的类型也没有意义。total
float
还有其他错误。例如,在这次调用中printf
printf("Estimated PI is : %f",&total);
您正在尝试将指针输出为浮点数。
可以按以下方式定义函数
double pi_formula( unsigned int n )
{
double pi = 0.0;
for ( unsigned int i = 0; i < n; i++ )
{
if ( i % 2 == 0 )
{
pi += 1.0 / ( 2 * i + 1 );
}
else
{
pi -= 1.0 / ( 2 * i + 1 );
}
}
return 4 * pi;
}
在主中,你可以写
int main( void )
{
puts( "ESTIMATE PI" );
printf( "Enter number of iterations: " );
unsigned int n = 0;
if ( scanf( "%u", &n ) == 1 )
{
printf( "Estimated PI is : %f\n", pi_formula( n ) );
}
}
这是所有代码组合在一起
#include <stdio.h>
double pi_formula( unsigned int n )
{
double pi = 0.0;
for (unsigned int i = 0; i < n; i++)
{
if (i % 2 == 0)
{
pi += 1.0 / ( 2 * i + 1 );
}
else
{
pi -= 1.0 / ( 2 * i + 1 );
}
}
return 4 * pi;
}
int main( void )
{
puts( "ESTIMATE PI" );
printf( "Enter number of iterations: " );
unsigned int n = 0;
if (scanf( "%u", &n ) == 1)
{
printf( "Estimated PI is : %f\n", pi_formula( n ) );
}
}
程序输出为
ESTIMATE PI
Enter number of iterations: 9999
Estimated PI is : 3.141693
评论
1赞
Vlad from Moscow
10/14/2023
@WeatherVane谢谢。我没有注意这些表情:)
0赞
Spongebuild
10/14/2023
我以为你必须列出原型中的所有变量,这些变量稍后将在函数中使用?因此,为什么我在那里列出了 3 个。我们还没有了解“unsigned int”,这与将其列为 int n 是一样的;没有等号?
0赞
Vlad from Moscow
10/14/2023
@Spongebuild 所有其他变量都是函数的辅助局部变量。该函数只需要迭代次数。unsigned int 是非负整数值的类型。将负数传递给函数是没有意义的。
0赞
Spongebuild
10/14/2023
在进行一些更改后,我现在得到负输出,所以越来越接近。用户输入 9999 应至少获得 3。一些东西,我得到了-0.858507
0赞
Vlad from Moscow
10/14/2023
@Spongebuild 您似乎用自己的错别字复制了代码。我无法重现您的输出。请参阅我附加的答案。
评论
printf("%lf",&fraction);
-->printf("%f", fraction);
do/while
fraction;
return(total);
return(total);
for
do
int
float
float
double
printf("Estimated PI is : %f",&total);
printf("Estimated PI is : %f", total);
numerator