计算平均海平面变化

标签 c

这就是我正在尝试做的作业。 从http://climate.nasa.gov/vital-signs/sea-level下载海平面数据(链接到外部站点。)。创建一个执行以下操作的程序:

a.告诉用户该程序使用 NASA 的数据来预测 2020 年至 2050 年的海平面。

b.将海平面数据存储在数组中。您只需使用 1993 年至今每年的一个数据点。使用每行的最后一列(已删除年度和半年度信号的全局平均海平面 GMSL)。

c.求数据中指定的所有年份海平面的平均年变化。 (提示 - 使用循环将多年来的年度变化存储在数组中,然后使用循环计算平均年度变化)。

d.假设线性增加并计算 2020、2025、2030、2035、2040、2045 和 2050 年的预测海平面上升。将这些结果存储在自己的数组中。 (提示 - 只需使用您在 c 部分中计算的平均值作为 future 几年的年度变化)。

e.向用户显示结果,并确保引用数据文件中指定的数据集,以便用户知道数据来自何处。

示例输出:

预测的全局平均海平面为

2020年64.32

2025年68.98

2030年73.51

2035 78.12

2040 83.43

2045 88.12

2050 93.04

这些预测是使用 XXXXXXXXXX 提供的数据做出的

这是到目前为止的代码。然而,它似乎并没有使用数组中的所有数据来查找海平面的平均变化。

#include <stdio.h>
#include <stdlib.h>

int main()
{
//creates a file object to read data
FILE* infile = fopen("nasa.txt","r");

//checks if file exists
if(infile == NULL)
{
printf("File does not exist.\n");
return -1;
}

//create 2d array to store years and their sea levels
int level[50][2];
//number of elements in array
int n = 0,i;

char word[5];

//read data from file word by word
while(fscanf(infile, "%s", word) != EOF)
{
if(word != ' ' && word != '\n')
{

//convert string to int and store in array
level[n][0] = atoi(word);

//store sea level
fscanf(infile, "%s", word);
level[n][1] = atoi(word);

//increment n
n++;
}
}

//store avg change
float avg=0;

for(i=1;i<n;i++)
{
//add difference of consecutive elements
avg += level[i][1] - level[i-1][1];
}

//calculate mean
avg = (float)avg/n;

int c = 7; //number of predictions
//array to store results
float predictions[][2] = {{2020,0},{2025,0},{2030,0},{2035,0},
{2040,0},{2045,0},{2050,0}};


//predict future sea levels
for(i=0;i<c;i++)
{
//multiply avg change by number of years
predictions[i][1] = level[n-1][1] +
(predictions[i][0] - level[n-1][0])*avg;
}

//print avg change
printf("Average change in sea level year over year is: %f mm\n",avg);

//print predictions
for(i = 0;i<c;i++)
{
printf("Predicted sea level change since 1993 for the year %.0f: %.2f mm\n",
predictions[i][0],predictions[i][1]);
}
printf("These predictions were made using data provided by the National Aeronautics and Space Administration.");

return 0;
}

海平面变化数据

1993年4

1994年7月

1995年11月

1996年14月

1997年21月

1998年20

1999年19日

2000年22月

2001年27日

2002年31月

2003年34期

2004年36期

2005年40

2006年42

2007年43

2008年47

2009年48

2010年54

2011年53

2012年59

2013 65

2014年68

2015年75

2016 83

2017年85

2018年88

2019 94

最佳答案

However It seems to not use all of the data in the array to find the average change in sea level.

avg = (float)avg/n;avg = (float)(level[n-1][1] - level[0][1])/相同n

只有终点才是重要的。
累积差异的循环正在抵消中间年份的值。

在年中,如果加 +100,一年的差异就会多 +100,下一年的差异就会少 100。最终差异的运行总和不受+100 的影响。

所有年中值都可能为 0,并且会得到相同的平均值

关于计算平均海平面变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59341140/

相关文章:

c - 如何通过http请求向http服务器发送文件

c++ - 对具有指针的结构 vector 进行排序

c - Eclipse CDT 预处理器在条件下获取语句

c - 使用 CLOCK_REALTIME 的内核 4.X 的纳秒精度可变计时器

c - C语言中的最小值和最大值

c - 我们是否应该显式加入一个线程来完成

C动态结构数组,没有错误,运行时程序终止

在循环中创建带有数组的管道

c - 添加闪烁颜色但它不闪烁

c - 无法从终端设置 Ad-hoc 网络