我正在编写一个基本程序来使用C命令行参数来计算算术几何平均值。但是程序似乎没有识别我输入的任何内容。这是我的代码:
/*
*This program will calculate an arithmetic-geometric mean
*provided two numbers (x,y) and an epsilon (e) are entered.
*/
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
int main (int argc, char **argv) {
//Check for command line argument.
if (argc != 4) {
printf ("Please enter two numbers (x,y) and an epsilon (e)\n");
printf ("as command line arguments for an AGM calculation\n");
exit(1);
}
double e,x,y,an,gn;
x = atof (argv[1]); //First number x.
y = atof (argv[2]); //Second number y.
e = atof (argv[3]); //Number of repetitions e.
double absoluteAnMinusGn; //Continuation condition.
double a = (x + y) / 2; //Normal arithmetic mean.
double g = sqrt (x * y); //Normal geometric mean.
an = (a + g) / 2; //Iteration 1 for calculation arithmetic mean.
gn = sqrt (a * g); //Iteration 1 for calculation geometric mean.
absoluteAnMinusGn = an - gn; //Calculates continuation condition.
if (absoluteAnMinusGn < 0) {
absoluteAnMinusGn = absoluteAnMinusGn * (-1); //Ensures absolute value of continuation condition.
}
printf ("DEBUG IN: x%d, y%d, e%d, absoulteAnMinusGn%d, a%d, g%d, an%d, gn%d\n", x,y,e,absoluteAnMinusGn,a,g,an,gn);//DEBUG CODE
while (absoluteAnMinusGn > e) {
an = (a + g) / 2;
gn = sqrt (a * g);
a = an;
g = gn;
absoluteAnMinusGn = an - gn;
if (absoluteAnMinusGn < 0) {
absoluteAnMinusGn = absoluteAnMinusGn * (-1);
}
}
//printf ("The arithmetric-geometric mean is (%d,%d) for %d\n", a,g,e);
printf ("DEBUG OUT: x%d, y%d, e%d, absoulteAnMinusGn%d, a%d, g%d, an%d, gn%d\n", x,y,e,absoluteAnMinusGn,a,g,an,gn);//DEBUG CODE
return 0;
}
我在命令行中输入以下内容:agm.exe 3 4 5
我得到以下输出:
DEBUG IN: x0, y10742661112, e0, absoluteANMinusGn1074790400, a0, g1075052544, an-171951648, gn1057505593
DEBUG OUT: x0, y10742661112, e0, absoluteANMinusGn1074790400, a0, g1075052544, an-171951648, gn1057505593
我昨天制作了一个类似的程序,用于使用命令行输入计算积分,这些输入完全符合预期。代码就在这里:
/*
*This program will calculate a Riemann sum using the
*left hand rule for sin(x)/x.
*/
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
int main (int argc, char **argv) {
//Check for command line argument.
if (argc != 4) {
printf ("Please enter integral bounds (a,b) and number of intervals (n)\n");
printf ("as command line arguments for a Riemann sum calculation \n");
exit(1);
}
double a,b,i,n,h,riemann,rectangles,answer;
a = atof (argv[1]); //Lower bound of integral.
b = atof (argv[2]); //Upper bound of integral.
n = atof (argv[3]); //Number of intervals.
h = (b - a) / n; //Delta X.
i = 0; //Counts intervals.
//Calculation of Left Hand Riemann Sum.
while (i <= (n - 1)) {
if (a == 0 && i == 0) { //Stops from dividing by zero.
rectangles = 1;
i++;
}
riemann = (sin(a + (i * h))) / (a + (i * h));
rectangles += riemann;
i++;
}
//Finalize answer.
answer = rectangles * h;
printf ("Sin(x)/x for bounds (%f , %f) with %f intervals is approximately %f \n", a,b,n,answer);
return 0;
}
左手Riemann sum的上述程序正确输出,几乎与我对AGM的代码相同。有人可以帮我弄清楚这里出了什么问题吗?我到处搜索过,找不到解决办法。我知道AGM代码可能设置为输出错误答案,但我主要担心的是修复命令行参数识别。我可以稍后重做我的数学。
答案 0 :(得分:5)
打印double的格式说明符是%f
。如果你没有提供正确的格式说明符来处理double,并且将double传递给%d
格式说明符 - 它会导致未定义的行为。(%d
期望积分参数不是double)(在你的中)案例错误输出)。
来自§7.21.6.3p9 N1570(c11标准)
如果任何参数不是相应转换规范的正确类型,则行为未定义。
答案 1 :(得分:0)
只需用List<User>
(这是double的格式说明符)替换<{strong> List<User> temp = new ArrayList();
(这是整数的格式说明符),因为您使用的是类型变量加倍,不是整数。
答案 2 :(得分:0)
我没有测试你的算法,但我在你的代码中发现了至少三个问题。我现在列出如下:
Large Numbers
内存未定义数据的原因!printf
的格式错误。我已将我的固定代码放在此处,您可以对其进行测试。如果它可以像你期望的那样运行!
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
int main (int argc, char *argv[]) {
if (argc != 4) {
printf ("Please enter two numbers (x,y) and an epsilon (e)\n");
printf ("as command line arguments for an AGM calculation\n");
exit(1);
}
// argv is pointer which pointer to pointer
// and argv[1] is a pointer to a string.
// so we need to covert string to int
char *p=NULL;
int x = strtol(argv[1], &p, 10);
int y = strtol(argv[2], &p, 10);
int e = strtol(argv[3], &p, 10);
//Check for command line argument.
double an = 0.0,gn =0.0;
double absoluteAnMinusGn=0.0; //Continuation condition.
double a = (x + y) / 2; //Normal arithmetic mean.
double g = sqrt (x * y); //Normal geometric mean.
an = (a + g) / 2; //Iteration 1 for calculation arithmetic mean.
gn = sqrt (a * g); //Iteration 1 for calculation geometric mean.
absoluteAnMinusGn = an - gn; //Calculates continuation condition.
if (absoluteAnMinusGn < 0) {
absoluteAnMinusGn = absoluteAnMinusGn * (-1); //Ensures absolute value of continuation condition.
}
printf ("DEBUG IN: x%d, y%d, e%d, absoulteAnMinusGn=%f, a=%f, g=%f, an=%f, gn=%f\n", x,y,e,absoluteAnMinusGn,a,g,an,gn);//DEBUG CODE
while (absoluteAnMinusGn > e) {
an = (a + g) / 2;
gn = sqrt (a * g);
a = an;
g = gn;
absoluteAnMinusGn = an - gn;
if (absoluteAnMinusGn < 0) {
absoluteAnMinusGn = absoluteAnMinusGn * (-1);
}
}
printf ("DEBUG OUT: x=%d, y=%d, e=%d, absoulteAnMinusGn=%f, a=%f, g=%f, an=%f, gn=%f\n", x,y,e,absoluteAnMinusGn,a,g,an,gn);//DEBUG CODE
return 0;
}