The purpose of the program is to calculate the volume at each depth. The inputs are the radius and length and in this test case they are 2.1 and 5.6 respectively. I keep getting 0, 1, 2, 3, and 4 for my volume but that's not the right volume, the depth/height is correct so perhaps someone can shed light on whats wrong with my equation below?
该程序的目的是计算每个深度的体积。输入是半径和长度,在这个测试案例中它们分别是2.1和5.6。我的音量一直是0,1,2,3和4,但这不是正确的音量,深度/高度是正确的,所以也许有人可以解释下面我方程式的错误是什么?
This is the function that calculates the volume
这是计算音量的功能
int getVolume(double arrplotptr[][col], double *arr2ptr, char *nameptr)
{
double vol, h, diam, ctr, rad, len, x;
int i, j;
rad = arr2ptr[radius];
len = arr2ptr[length];
diam = (rad * 2);
ctr = diam / 100;
h = 0;
for (j = 0; j < 100; j++) {
h = h + ctr;
arrplotptr[0][j] = h;
}
h = 0;
for (i = 0; i < 100; i++) {
h = h + ctr;
x = (rad - h) / rad;
vol = ((rad * rad) * acos(x) - (rad - h) * (sqrt((2 * rad * h) - (h * h)))) * len;
arrplotptr[1][i] = vol;
}
}
2 个解决方案
#1
2
I see several issues in your code:
我在您的代码中看到了几个问题:
- Why do you use
ctr = diam / 100;
instead ofctr = rad / 100;
? - 为什么使用ctr = diam / 100;而不是ctr = rad / 100;?
- You do not return a value from
getVolume
, if the caller function relies on the return value, you invoke undefined behavior. - 您不从getVolume返回值,如果调用函数依赖于返回值,则调用未定义的行为。
- You store the volume of each slice but do not compute the total volume. You did not post the code that does that, maybe there are problems there too.
- 您存储每个切片的音量但不计算总音量。你没有发布那样做的代码,也许那里也有问题。
#2
0
As written by chqrlie, I think you should change
正如chqrlie所写,我认为你应该改变
ctr = diam / 100;
with
同
ctr = rad / 100;
And, as written by EOF, the function is defined as "int" but returns no value; You should redefine it as "void" or return an integer value.
并且,如EOF所写,该函数被定义为“int”但不返回任何值;您应该将其重新定义为“void”或返回整数值。
I add that it doesn't seem necessary to double loop: in each iteration you can calculate "h", "x", "vol" and save the two values of "arrplotptr".
我补充说,似乎没有必要进行双循环:在每次迭代中,您可以计算“h”,“x”,“vol”并保存“arrplotptr”的两个值。
I propose to simplify the function as follows
我建议简化功能如下
void getVolume (double arrplotptr[][col], double arr2ptr[])
{
double const rad = arr2ptr[radius];
double const len = arr2ptr[length];
double const ctr = rad / 100;
int i;
double h;
for ( i = 0, h = ctr ; i < 100 ; ++i, h+=ctr )
{
arrplotptr[0][i] = h;
arrplotptr[1][i] = ((rad * rad) * acos((rad - h) / rad)
- (rad - h) * (sqrt((2 * rad * h) - (h * h)))) * len;
}
}
#1
2
I see several issues in your code:
我在您的代码中看到了几个问题:
- Why do you use
ctr = diam / 100;
instead ofctr = rad / 100;
? - 为什么使用ctr = diam / 100;而不是ctr = rad / 100;?
- You do not return a value from
getVolume
, if the caller function relies on the return value, you invoke undefined behavior. - 您不从getVolume返回值,如果调用函数依赖于返回值,则调用未定义的行为。
- You store the volume of each slice but do not compute the total volume. You did not post the code that does that, maybe there are problems there too.
- 您存储每个切片的音量但不计算总音量。你没有发布那样做的代码,也许那里也有问题。
#2
0
As written by chqrlie, I think you should change
正如chqrlie所写,我认为你应该改变
ctr = diam / 100;
with
同
ctr = rad / 100;
And, as written by EOF, the function is defined as "int" but returns no value; You should redefine it as "void" or return an integer value.
并且,如EOF所写,该函数被定义为“int”但不返回任何值;您应该将其重新定义为“void”或返回整数值。
I add that it doesn't seem necessary to double loop: in each iteration you can calculate "h", "x", "vol" and save the two values of "arrplotptr".
我补充说,似乎没有必要进行双循环:在每次迭代中,您可以计算“h”,“x”,“vol”并保存“arrplotptr”的两个值。
I propose to simplify the function as follows
我建议简化功能如下
void getVolume (double arrplotptr[][col], double arr2ptr[])
{
double const rad = arr2ptr[radius];
double const len = arr2ptr[length];
double const ctr = rad / 100;
int i;
double h;
for ( i = 0, h = ctr ; i < 100 ; ++i, h+=ctr )
{
arrplotptr[0][i] = h;
arrplotptr[1][i] = ((rad * rad) * acos((rad - h) / rad)
- (rad - h) * (sqrt((2 * rad * h) - (h * h)))) * len;
}
}