【发布时间】:2020-08-10 15:13:02
【问题描述】:
一切都编译得很好,并且被大量评论以显示我想要完成的事情。 link 这里是一个 Excel 电子表格,用于查找最高平均风速,然后是最大的温度变化。我这样做是因为我的代码中没有正确返回结果。
#include <stdio.h>
#include <stdlib.h>
#define SITE_COUNT 3 // 3 sites 2001,3345,3819
#define DAY_RANGE 7 // 7 days total, between site_ids
typedef struct
{
int site_id_num;
int day_of_month; // Day of the month (not used)
int wind_speed;
int temperature;
} measured_data_t;
typedef struct
{
int site_id_num; // Storing the correct site ID
int lowest_temp; // Storing the lowest temp
int highest_temp; // Storing the highest temp
int avg_speed; // Wind speed, for finding avg wind speed
} calculated_data;
// File pointer
FILE *fptr;
int main(int argc, char *argv[]) {
int i=0, o=0; // Indexed for loops
int ARR_SIZ = SITE_COUNT*DAY_RANGE;
measured_data_t fileData[ARR_SIZ]; // Creates Struct array of 21, for 21 entries, 7 days for 3 sites
calculated_data processedData[SITE_COUNT]; // Created Struct array of 3, sites struct array
// Open File
if ((fptr = fopen("meteo.txt", "rt")) == NULL)
{
// Throw error if no file
printf("\n Error opening data file");
exit(1);
}
for(i=0; i<ARR_SIZ; i++)
{
// Read lines from file, site_id, day, wind speed, temp
fscanf(fptr, "%d %d %d %d", &fileData[i].site_id_num, &fileData[i].day_of_month, &fileData[i].wind_speed, &fileData[i].temperature);
}
for (i=0; i<SITE_COUNT; i++)
{
// Assign defaults to processedData
processedData[i].site_id_num = fileData[i*DAY_RANGE].site_id_num; // site_id_num position in array, 0..7..14
processedData[i].lowest_temp = -50;
processedData[i].highest_temp = 50;
processedData[i].avg_speed = 0;
}
for(i=0; i < ARR_SIZ; i++)
{
for(o=0; o < SITE_COUNT; o++)
{
if(processedData[o].site_id_num == fileData[i].site_id_num)
{
// If Low Temp > fileData Temp
if(processedData[o].lowest_temp > fileData[i].temperature)
processedData[o].lowest_temp = fileData[i].temperature;
// If High Temp > fileData Temp
if(processedData[o].highest_temp > fileData[i].temperature)
processedData[o].highest_temp = fileData[i].temperature;
}
// Get accumulated wind speed, process average later
processedData[o].avg_speed += fileData[i].wind_speed;
}
}
for(o=0; o< SITE_COUNT; o++)
{
// Get average wind speed
processedData[o].avg_speed = processedData[o].avg_speed / DAY_RANGE;
}
int high_avg_wind = 0; // Average windspeed
int high_var_temp = 0; // Variation of temperature
int high_avg_wind_site_id = 0; // Site ID for Wind
int high_var_temp_site_id = 0; // Site ID fir Temp
// Temp variation for 3 sites
int temperature_variation[SITE_COUNT];
for(o=0; o< SITE_COUNT; o++)
{
// Determine site code for average wind speed, among 3 sites
if(processedData[o].avg_speed > high_avg_wind)
{
high_avg_wind = processedData[o].avg_speed;
high_avg_wind_site_id = processedData[o].site_id_num;
}
// Get highest variation through subtraction
temperature_variation[o] = processedData[o].highest_temp - processedData[o].lowest_temp;
// Determine site code for greatest variation, among 3 sites
if(temperature_variation[o] > high_var_temp)
{
high_var_temp = temperature_variation[o];
high_var_temp_site_id = processedData[o].site_id_num;
}
}
printf("The site with the highest average wind speed: %d\nThe site with the greatest variation in temperature is: %d\n",high_avg_wind_site_id,high_var_temp_site_id);
// Close the file
fclose(fptr);
return 0;
}
我在最后一个循环中设置了断点,以确定在 3 个站点之间正确分配了数据,而且它看起来确实在测量,但它返回的站点代码为 2001,它实际上应该是 3345 和 3819。
这是数据的输入:
2001 10 11 30
3345 10 8 29
3819 10 17 27
2001 11 5 22
3345 11 5 23
3819 11 20 21
2001 12 18 25
3345 12 12 23
3819 12 22 21
2001 13 16 26
3345 13 14 24
3819 13 18 22
2001 14 14 26
3345 14 10 24
3819 14 15 22
2001 15 2 25
3345 15 9 22
3819 15 9 19
2001 16 14 22
3345 16 9 20
3819 16 12 18
【问题讨论】:
-
很棒的代码!请不要发布指向文本图像的链接。请将输入作为文本发布到问题中。请发布您当前获得的程序的输出。
its returning site code 2001, when it should really be 3345 and 3819.- 那么它在哪里 print 2001? 2001 是哪个值?一个数字 2001 怎么变成两个数字 3345 和 3819? -
我用需要处理的问题数据更新了线程。不知何故,high_avg_wind_site_id,high_var_temp_site_id 在程序结束时通过 printf() 第 113 行得出了相同的值 2001,但是在 excel 中运行公式后,它们在程序中的输出不正确。
-
processedData[i].highest_temp = 50;- 好吧,当所有值都低于然后是 50,那么......它们永远不会更大,所以用非常小的(负)值初始化或用第一个元素的值。processedData[o].avg_speed += fileData[i].wind_speed;似乎有点奇怪,它不在if(processedData[o].site_id_num == fileData[i].site_id_num)内部。 -
调整语句的位置 processedData[o].avg_speed += fileData[i].wind_speed;到内部 == 语句解决了 wind_speed 结果。但是,我将 processesData[i].highest_temp = 50 重新初始化为 processesData[i].highest_temp = fileData[i].temperature;但仍然返回不正确的值。现在的输出是:温度变化最大的站点是:2001
-
酷,你看
lowest_temp了吗?-50冻结了
标签: c arrays for-loop struct conditional-statements