#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define Pi 3.14159265358979323846
#define MAX_DATA 120
double Find_GrowthRate(double Data_6hr[MAX_DATA],double Data_24hr[MAX_DATA]);
void Find_DoublingTime(double GrowthRate[MAX_DATA]);
int main()
{
int i;
double t1;
double Data_6hr[MAX_DATA] = {1.77,1.58,1.78,1.82,1.59,1.63,1.74,1.76,1.76,1.56,1.52,1.53,1.75,1.64,1.41,1.72,1.55,1.71,1.74,1.55,
0.53,0.51,0.56,0.57,0.53,0.62,0.52,0.60,0.52,0.62,0.52,0.61,0.55,0.59,0.50,0.59,0.58,0.50,0.57,0.61,
0.20,0.21,0.21,0.23,0.24,0.21,0.24,0.21,0.20,0.24,0.23,0.23,0.21,0.24,0.23,0.22,0.25,0.21,0.24,0.21,
1.46,1.32,1.54,1.25,1.25,1.55,1.23,1.40,1.28,1.52,1.45,1.41,1.21,1.23,1.38,1.43,1.50,1.23,1.21,1.39,
0.74,0.88,0.90,0.77,0.82,0.85,0.71,0.90,0.81,0.77,0.81,0.72,0.83,0.87,0.84,0.72,0.71,0.75,0.74,0.75,
0.91,0.98,1.03,0.95,0.83,0.89,0.80,1.03,1.03,0.88,0.84,0.95,0.95,0.88,0.89,0.94,0.89,1.03,0.97,1.00};
double Data_24hr[MAX_DATA] ={7.60,7.39,7.57,7.60,7.59,7.25,7.52,7.54,7.61,7.24,7.51,7.28,7.45,7.39,7.27,7.59,7.44,7.59,7.62,7.19,
2.67,2.51,2.68,2.65,2.57,2.63,2.65,2.69,2.66,2.72,2.64,2.65,2.58,2.60,2.62,2.64,2.64,2.62,2.61,2.74,
1.02,1.06,1.03,1.09,1.06,1.05,1.06,1.06,1.02,1.05,1.09,1.04,1.04,1.08,1.05,1.08,1.10,1.05,1.08,1.07,
6.61,6.47,6.45,6.28,6.32,6.38,6.18,6.29,6.23,6.49,6.49,6.47,6.09,6.34,6.20,6.57,6.43,6.04,6.31,6.23,
3.70,3.86,3.79,3.60,3.71,3.69,3.68,3.77,3.72,3.59,3.76,3.72,3.81,3.88,3.85,3.56,3.55,3.59,3.59,3.75,
4.13,4.26,4.35,4.22,4.12,4.10,4.24,4.25,4.30,4.28,4.05,4.35,4.24,4.23,4.14,4.37,4.18,4.29,4.27,4.25};
Find_GrowthRate(Data_6hr,Data_24hr);
double *GrowthRate;
printf("Growth Rates\n");
for(i=0;i<MAX_DATA;i++){
printf("%lf \n",*(GrowthRate+i));
}
Find_DoublingTime(GrowthRate);
free(GrowthRate);
return 0;
}
double Find_GrowthRate(double Data_6hr[MAX_DATA], double Data_24hr[MAX_DATA])
{
int i;
double *GrowthRate;
GrowthRate = (double*)malloc(MAX_DATA*sizeof(double));
for(i=0;i<MAX_DATA;i++){
double PopulationSize_t1 = (Pi * pow((Data_6hr[i]/ 2),2));
printf("%lf\n",PopulationSize_t1);
double PopulationSize_t2 = (Pi * pow((Data_24hr[i]/ 2),2));
printf("%lf\n",PopulationSize_t2);
double x = ((PopulationSize_t2 - PopulationSize_t1) / PopulationSize_t1);
*(GrowthRate+i) = x;
}
printf("Growth Rates\n");
for(i=0;i<MAX_DATA;i++){
printf("%lf \n",*(GrowthRate+i));
}
return *GrowthRate;
}
void Find_DoublingTime(double *GrowthRate)
{
int i;
double x, sum=0, t1, t2, t3, t4, t5, t6;
printf("Growth Rates\n");
for(i=0;i<MAX_DATA;i++){
printf("%lf \n",*(GrowthRate+i));
}
printf("Average Doubling Population Time : \n");
for(i=0;i<20;i++){
x = (log(2)/ log (1+*(GrowthRate+i)));
sum += x;
}
t1 = sum / 20;
printf("%lf", t1);
sum=0;
for(i=20;i<40;i++){
x = (log(2)/ log (1+*(GrowthRate+i)));
sum += x;
}
t2 = sum / 20;
printf("%lf", t2);
sum=0;
for(i=40;i<60;i++){
x = (log(2)/ log (1+GrowthRate[i]));
sum += x;
}
t3 = sum / 20;
printf("%lf", t3);
sum=0;
for(i=60;i<80;i++){
x = (log(2)/ log (1+GrowthRate[i]));
sum += x;
}
t4 = sum / 20;
printf("%lf", t4);
sum=0;
for(i=80;i<100;i++){
x = (log(2)/ log (1+GrowthRate[i]));
sum += x;
}
t5 = sum / 20;
printf("%lf", t5);
sum=0;
for(i=100;i<MAX_DATA;i++){
x = (log(2)/ log (1+GrowthRate[i]));
sum += x;
}
t6 = sum / 20;
printf("%lf", t6);
}
原来没有&#34;错误&#34;在这段代码中,我猜测它与我使用指针有关。
代码的要点是提供一个总体的平均倍增时间,主要问题是我似乎无法将数组从我的Find_GrowthRate
函数传递给我的Find_DoublingTime
函数。
感谢任何帮助,我对C来说是全新的,所以更改为代码的示例会很好。
如果某人真的可以使用有效的答案运行此代码,那么我相信数据就在那里。
答案 0 :(得分:1)
这里有两个问题。首先,您从double *
返回Find_GrowthRate
,但声明该函数返回double
。将返回类型更改为double *
。
double *Find_GrowthRate(double Data_6hr[MAX_DATA], double Data_24hr[MAX_DATA])
其次,您拨打Find_GrowthRate
但不要将返回值分配给任何内容。在下一行,您声明GrowthRate
,然后使用它而不指定任何内容。将返回值分配给GrowthRate
。
double *GrowthRate = Find_GrowthRate(Data_6hr,Data_24hr);
答案 1 :(得分:1)
问题是你的函数Find_GrowthRate返回double,而它应该返回一个double *(你正在通过malloc创建的那个)。此外,您需要在main上设置GrowRate变量(也许您认为它是全局的或其他什么?)。
这是完整的工作:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define Pi 3.14159265358979323846
#define MAX_DATA 120
double* Find_GrowthRate(double Data_6hr[MAX_DATA],double Data_24hr[MAX_DATA]);
void Find_DoublingTime(double GrowthRate[MAX_DATA]);
int main()
{
int i;
double t1;
double Data_6hr[MAX_DATA] = {1.77,1.58,1.78,1.82,1.59,1.63,1.74,1.76,1.76,1.56,1.52,1.53,1.75,1.64,1.41,1.72,1.55,1.71,1.74,1.55,
0.53,0.51,0.56,0.57,0.53,0.62,0.52,0.60,0.52,0.62,0.52,0.61,0.55,0.59,0.50,0.59,0.58,0.50,0.57,0.61,
0.20,0.21,0.21,0.23,0.24,0.21,0.24,0.21,0.20,0.24,0.23,0.23,0.21,0.24,0.23,0.22,0.25,0.21,0.24,0.21,
1.46,1.32,1.54,1.25,1.25,1.55,1.23,1.40,1.28,1.52,1.45,1.41,1.21,1.23,1.38,1.43,1.50,1.23,1.21,1.39,
0.74,0.88,0.90,0.77,0.82,0.85,0.71,0.90,0.81,0.77,0.81,0.72,0.83,0.87,0.84,0.72,0.71,0.75,0.74,0.75,
0.91,0.98,1.03,0.95,0.83,0.89,0.80,1.03,1.03,0.88,0.84,0.95,0.95,0.88,0.89,0.94,0.89,1.03,0.97,1.00};
double Data_24hr[MAX_DATA] ={7.60,7.39,7.57,7.60,7.59,7.25,7.52,7.54,7.61,7.24,7.51,7.28,7.45,7.39,7.27,7.59,7.44,7.59,7.62,7.19,
2.67,2.51,2.68,2.65,2.57,2.63,2.65,2.69,2.66,2.72,2.64,2.65,2.58,2.60,2.62,2.64,2.64,2.62,2.61,2.74,
1.02,1.06,1.03,1.09,1.06,1.05,1.06,1.06,1.02,1.05,1.09,1.04,1.04,1.08,1.05,1.08,1.10,1.05,1.08,1.07,
6.61,6.47,6.45,6.28,6.32,6.38,6.18,6.29,6.23,6.49,6.49,6.47,6.09,6.34,6.20,6.57,6.43,6.04,6.31,6.23,
3.70,3.86,3.79,3.60,3.71,3.69,3.68,3.77,3.72,3.59,3.76,3.72,3.81,3.88,3.85,3.56,3.55,3.59,3.59,3.75,
4.13,4.26,4.35,4.22,4.12,4.10,4.24,4.25,4.30,4.28,4.05,4.35,4.24,4.23,4.14,4.37,4.18,4.29,4.27,4.25};
double *GrowthRate = Find_GrowthRate(Data_6hr,Data_24hr);
printf("Growth Rates\n");
for(i=0;i<MAX_DATA;i++){
printf("%lf \n",*(GrowthRate+i));
}
Find_DoublingTime(GrowthRate);
free(GrowthRate);
return 0;
}
double *Find_GrowthRate(double Data_6hr[MAX_DATA], double Data_24hr[MAX_DATA])
{
int i;
double *GrowthRate;
GrowthRate = (double*)malloc(MAX_DATA*sizeof(double));
for(i=0;i<MAX_DATA;i++){
double PopulationSize_t1 = (Pi * pow((Data_6hr[i]/ 2),2));
printf("%lf\n",PopulationSize_t1);
double PopulationSize_t2 = (Pi * pow((Data_24hr[i]/ 2),2));
printf("%lf\n",PopulationSize_t2);
double x = ((PopulationSize_t2 - PopulationSize_t1) / PopulationSize_t1);
*(GrowthRate+i) = x;
}
printf("Growth Rates\n");
for(i=0;i<MAX_DATA;i++){
printf("%lf \n",*(GrowthRate+i));
}
return GrowthRate;
}
void Find_DoublingTime(double *GrowthRate)
{
int i;
double x, sum=0, t1, t2, t3, t4, t5, t6;
printf("Growth Rates\n");
for(i=0;i<MAX_DATA;i++){
printf("%lf \n",*(GrowthRate+i));
}
printf("Average Doubling Population Time : \n");
for(i=0;i<20;i++){
x = (log(2)/ log (1+*(GrowthRate+i)));
sum += x;
}
t1 = sum / 20;
printf("%lf", t1);
sum=0;
for(i=20;i<40;i++){
x = (log(2)/ log (1+*(GrowthRate+i)));
sum += x;
}
t2 = sum / 20;
printf("%lf", t2);
sum=0;
for(i=40;i<60;i++){
x = (log(2)/ log (1+GrowthRate[i]));
sum += x;
}
t3 = sum / 20;
printf("%lf", t3);
sum=0;
for(i=60;i<80;i++){
x = (log(2)/ log (1+GrowthRate[i]));
sum += x;
}
t4 = sum / 20;
printf("%lf", t4);
sum=0;
for(i=80;i<100;i++){
x = (log(2)/ log (1+GrowthRate[i]));
sum += x;
}
t5 = sum / 20;
printf("%lf", t5);
sum=0;
for(i=100;i<MAX_DATA;i++){
x = (log(2)/ log (1+GrowthRate[i]));
sum += x;
}
t6 = sum / 20;
printf("%lf", t6);
}