如何用数组中的值替换 Nan 值?

How do I replace the Nan values with values in my array?

我尝试了不同的方法并参考了提供的源代码,但似乎永远无法从数组中获取值。如果我不在 for 语句中包含 int,我将得到 Nan 值.使用 int 语句,该值变为 0.00 但它没有意义,因为浮点数现在是 int 类型。请帮我看看我的代码哪里出错了。谢谢!

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

#define Years 5
#define Months 12

int main(void)
{
    int year = 0, month = 0;
    float monthlytotal = 0, yearlytotal = 0;

    printf("\aWelcome. This is a generated simple weather analysis of Aruba's weather for the last 5 years.\n");

    float rainfall[Years][Months] = {
        { 126.6, 186, 6.2, 89.8, 193.8, 162.8, 168.6, 139.2, 118.9, 181, 290.2, 292.6 },//2016
        { 197.6, 158.4, 136.2, 208.6, 190, 106, 79.6, 84.2, 124.4, 120.8, 268.6, 371.2 },//2017
        { 287, 14.8, 44.6, 61.2, 132.2, 182.6, 143.2, 121.6, 144.4, 234.4, 169.6, 172.6 },//2018
        { 63.6, 31.6, 72.2, 174.8, 69, 173.8, 12.2, 11.8, 22.8, 176.8, 137.4, 421.5 },//2019
        { 88.4, 65, 108.8, 188, 255.6, 233.8, 140.8, 103.4, 150.2, 78.8, 220.6, 253.2 }//2020
    };

    printf("Based on the data collected from WEBBA, here are the results.\n");
    printf("Year\t\t\tAmt of rain\n");

    for (year = 0, yearlytotal = 0; year < Years; year++) {
        for (month = 0, monthlytotal = 0; month < Months; month++) {
            monthlytotal += rainfall[Years][Months];
        }

        printf("%d\t\t%15.2f\n", 2016 + year, monthlytotal);
        yearlytotal += monthlytotal;
    }

    return 0;
}
monthlytotal += rainfall[Years][Months];

应该是

monthlytotal += rainfall[year][month];

请注意 clang-Warray-bounds(在 clang 中默认打开)会在编译时发现问题。

请注意,gccclang-fsanitize=address 会在 运行 时发现问题。