Closed. This question needs details or clarity. It is not currently accepting answers. Learn more
想改进这个问题吗?添加细节并通过editing this post澄清问题。
两年前关闭。
请原谅我在努力正确描述这个问题时的措辞。
我正在研究一个程序,创建跨若干年的每月降雨量摘要现在这个程序在5年内循环12个月,并将打印结果。
不幸的是,我不知道如何正确计算年度总数。例如:如果我为前12个值(即第1年)输入1,则总数为12如果我在第二年也这样做,那么总数就变成24。它看起来好像是在打印所有年份的值的总和,而不仅仅是一年内的总和。
这是我看到的情况的总结版本:
Do you want to input Precipitation data? (y for yes)
y
Enter rain for 1, 1:
1
Enter rain for 1, 2:
1
...
1
Enter rain for 1, 11:
1
Enter rain for 1, 12:
1
Total rainfall: 12.00
Enter rain for 2, 1:
1
Enter rain for 2, 2:
1
...
Enter rain for 2, 12:
1
Total rainfall: 24.00

当我尝试构建一个单独的循环(如下面的循环)时,值始终为0.00:
void inputdata() {
    /* variable definition: */
    float Rain = 1.0;
    float total = 0.0;

    // Input Data
    for (int year = 0; year < NUMYEARS; year++) {
      for (int month = 0; month < NUMMONTHS; month++) {
        printf("Enter rain for %d, %d:\n", year + 1, month + 1);
        scanf("%f", & Rain);
        Raindata[year][month] = Rain;
      }
      printf("Total rainfall: %.2f\t\n", total);
    }
    for (int month = 0; month < NUMMONTHS; month++) {
        total = total + Rain;
    }
  }

最大的挫折是,我几乎可以看到它是如何执行数学运算的,但只是看不到如何修复它们。
这是我的完整代码:
#define NUMMONTHS 12
#define NUMYEARS 5
#include <stdio.h>

// function prototypes
void inputdata();
void printdata();

// Global variables
// These are available to all functions
float Raindata[NUMYEARS][NUMMONTHS];
char years[NUMYEARS][5] = {
  "2011", "2012", "2013",
  "2014", "2015"
};
char months[NUMMONTHS][12] = {
  "Jan", "Feb", "Mar", "Apr",
  "May", "Jun", "Jul", "Aug",
  "Sep", "Oct", "Nov", "Dec"
};

int main() {
    char enterData = 'y';
    printf("Do you want to input Precipatation data? (y for yes)\n");
    scanf("%c", & enterData);
    if (enterData == 'y') {
      // Call Function to Input data
      inputdata();
      // Call Function to display data
      printdata();
    } else {
      printf("No data was input at this time\n");
    }
    printf("Please try the Precipitation program again. \n");
    return 0;
  }

// function to inputdata
void inputdata() {
    /* variable definition: */
    float Rain = 1.0;
    float total = 0.0;

    // Input Data
    for (int year = 0; year < NUMYEARS; year++) {
      for (int month = 0; month < NUMMONTHS; month++) {
        printf("Enter rain for %d, %d:\n", year + 1, month + 1);
        scanf("%f", & Rain);
        Raindata[year][month] = Rain;
        total = total + Rain;
      }
      printf("Total rainfall: %.2f\t\n", total);
    }
  }

// Function to printdata
void printdata() {
  // Print data
  printf("year\t month\t rain\n");
  for (int year = 0; year < NUMYEARS; year++) {
    for (int month = 0; month < NUMMONTHS; month++) {

      printf("%s\t %s\t %5.2f\n", years[year], months[month], Raindata[year][month]);
    }
  }
}

最佳答案

刚离开内部循环,但在外部循环中,通过将其设置为Total来重置0的值。

for (int year = 0; year < NUMYEARS; year++) {
  total = 0.0;
  for (int month = 0; month < NUMMONTHS; month++) {
    printf("Enter rain for %d, %d:\n", year + 1, month + 1);
    scanf("%f", & Rain);
    Raindata[year][month] = Rain;
    total = total + Rain;
  }
  printf("Total rainfall: %.2f\t\n", total);
}

这样,你每年从零开始重新开始。

关于c - 在给定的迭代次数后如何清除总和的值? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45062866/

10-11 22:10
查看更多