提问人:Shaind 提问时间:11/10/2023 最后编辑:BarmarShaind 更新时间:11/10/2023 访问量:62
让某人给我一些关于如何使此代码工作的指示
Make someone give me some pointers on how to make this code work
问:
我的代码中有一些错误,我不知道要解决,我想我调用函数来主错误,但我的程序停止在 <sum = sum side(a)> 我无法自己解决,我尝试了所有方法,所以我来这里看看是否有人可以给我一些关于如何解决它的建议或指示
#include <cs50.h>
#include <stdio.h>
//is the size of the array
const int n = 3;
int sum_side(float x[]);
int main(void)
{
float sum;
int a[n];
sum = sum_side(a);
// is used to get 3 sides from the user all have to be positive
do
{
a[0] = get_int("First side: ");
a[1] = get_int("Second side: ");
a[2] = get_int("Third side: ");
}
while(a[0] <= 0 || a[1] <= 0 || a[2] <=0);
//is used to say if it can be a triangle or nor
if(sum_side(a[n]) == 1)
{
printf("Can be a triangle? ");
}
else
{
printf("Can't be a triangle? ");
}
}
int sum_side(float x[])
{
// Check that sum of any two sides greater than third
if ((x[0] + x[1] <= x[2]) || (x[1] + x[2] <= x[0]) || (x[2] + x[0] <= x[1]))
{
return false;
}
// if we passe the test, we're good!
return true;
}
答:
1赞
chux - Reinstate Monica
11/10/2023
#1
如果有人能给我一些关于如何解决它的建议或指示
#1 启用所有编译器警告。例
如果 OP 从这篇文章中学到了什么,那就是使用本地工具,就像许多编译器警告一样,以提高生产力并减少调试时间。
使用 OP 代码的示例警告:
warning: passing argument 1 of 'sum_side' from incompatible pointer type [-Wincompatible-pointer-types]
14 | sum = sum_side(a);
| ^
| |
| int *
note: expected 'float *' but argument is of type 'int *'
7 | int sum_side(float x[]);
| ~~~~~~^~~
warning: conversion from 'int' to 'float' may change value [-Wconversion]
14 | sum = sum_side(a);
| ^~~~~~~~
warning: passing argument 1 of 'sum_side' makes pointer from integer without a cast [-Wint-conversion]
26 | if(sum_side(a[n]) == 1)
| ~^~~
| |
| int
note: expected 'float *' but argument is of type 'int'
7 | int sum_side(float x[]);
| ~~~~~~^~~
warning: variable 'sum' set but not used [-Wunused-but-set-variable]
11 | float sum;
| ^~~
#2 我们使用的是浮点 (FP),而不是无限精度数学。
FP 添加可能会产生舍入误差。边缘情况很棘手。
类似代码与支持允许极端边缘情况通过。考虑大小:(1.0e30f、1.0e30f、1.0),OP 的代码在应该通过时会失败。x[0] + x[1] < x[2]
x[0] + x[1] <= x[2]
IMO,(0,0,0)的偶数边是有效的三角形。
在这种情况下,我们可以用来处理精度和范围问题。float
double
bool sum_side_alt(const float x[]) {
// Check that sum of any two sides greater than (or the same as) the third.
return ((0.0 + x[0] + x[1] >= x[2]) &&
(0.0 + x[1] + x[2] >= x[0]) &&
(0.0 + x[2] + x[0] >= x[1]));
// ^---------------^ addition uses double math
}
1赞
2 revschux - Reinstate Monica
#2
这仅适用于想要避免四舍五入问题的迂腐者
x[0] + x[1]
,通常会导致四舍五入,并且可能会溢出到无穷大,从而影响比较的正确性。为了避免这些问题,让我们尝试一种新的方法,而不使用更广泛的类型:
// Return true if valid values for a triangle.
bool triangle_test(float x[]) {
// Find the greatest
if (x[0] > x[1]) {
swap(&x[0], &x[1]);
}
if (x[1] > x[2]) {
swap(&x[1], &x[2]);
}
// x[2] now has the greatest.
if (x[0] > x[1]) {
swap(&x[0], &x[1]);
}
// x[0] now has the least.
// Optional test
if (x[0] <= 0.0) {
return false; // OP seems to want to exclude these.
}
/*
If x[1] is at least x[2]/2, then x[2] - x[1] will form an _exact_ difference without overflow.
Comparing the difference with x[2] will be exact.
If x[1] is smaller than 0.5*x[2], then x[2] - x[1] will at least x[2]/2,
even if rounded and larger than x[0] in all cases.
*/
float diff = x[2] - x[1];
return diff < x[0];
}
评论
const int n = 3;
int a[n];
a[n]
int sum_side(float x[]);
int
float
float_side(a[n])
float_side(a)
a
int a[n]
float a[n];
sum = sum_side(a);
在获取输入之前取消初始化数组时,没有多大意义。