【发布时间】:2014-04-15 17:50:56
【问题描述】:
所以我正在尝试编写一个函数,该函数将返回一个包含多个值的数组。目前,它运行正常,但只输出最终计算值。我将如何使其输出包含所有计算值?
我的代码如下所示:
//Practice to output an array of structs
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct boat_params {
double V, Uc, Vc;
};
struct boat_params submerged_volume(double L1, double L2, double Lavg, double H) {
struct boat_params volume;
double V_sub, Uc_sub, Vc_sub;
V_sub = 0;
//Boat description
double C, delta;
double theta, theta_rad, theta_min, theta_min2, theta_lim, theta_lim2, theta_lim_deg;
double Ug1, Ug2, Vg1, Vg2, V1, V2;
double pi;
pi = 4*atan(1);
C = sqrt(L1*L1 + L2*L2);
delta = acos(L1/C);
theta_lim = asin(H/L1);
theta_lim_deg = (theta_lim/pi) * 180.0;
theta_min = asin(H/C) - delta;
theta_min2 = 0;
//Calculating the submerged volume and centre of gravity for each different angle
for (theta = 0; theta <= 10; theta ++) {
//**Note: I've taken out the actual calculations of V_sub, Uc_sub, and Vc_sub for brevity**
volume.V = V_sub;
volume.Uc = Uc_sub;
volume.Vc = Vc_sub;
}
return volume;
}
int main () {
double L1, L2, Lavg, H;
struct boat_params volume;
L1 = 17.6;
L2 = 3;
Lavg = 4;
H = 4.5;
volume = submerged_volume(L1, L2, Lavg, H);
printf("V = %lf\nUc = %lf\nVc = %lf\n", volume.V, volume.Uc, volume.Vc);
return 0;
}
我可以让它正确输出最后计算的值(对于 theta = 10),但这是我得到的唯一值。我将如何计算每个 theta 值的 V_sub、Uc_sub 和 Vc_sub?并输出每个值。我假设这意味着将结构转换为数组并用该结构的值填充数组的每个元素,但我不知道该怎么做!
非常感谢任何帮助,并提前感谢您。
另外:如果可能的话,我想避免使用指针,但请理解这可能是不可能的!我还是新手,不擅长使用它们!
【问题讨论】: