C用嵌套循环计算年降雨量总数,&匹配用户输入

时间:2017-07-27 23:07:22

标签: c

我最难理解为什么我的程序无法正确执行。我需要使用一个循环来计算每年的总降雨量,我遇到的问题是它不仅仅是2月份的用户输入,而是增加了前几年的总数(即2011年总数变为2012年2月的用户输入)。任何帮助将不胜感激。

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


#define NUMMONTHS 12
#define NUMYEARS 5

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

// Global variables
// These are available to all functions
float Raindata[NUMYEARS][NUMMONTHS];
float sum = 0.0;
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;
    // 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;
       }
    }
}

// Function to printdata
void printdata()
{
    // Print data
    printf ("year\t month\t rain\n");

    //Function to sum rainfall 
    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]);
             Raindata[year][NUMMONTHS+1] += Raindata[year][month]; 
        }
        printf("Total amount of rain for year %s: %5.2f\n", years[year], 
               Raindata[year][NUMMONTHS+1]); //prints the total amount of rain every year 
    }
}

这是输出:

Total amount of rain for year 2011: 40.00
2012     Jan      1.10
2012     Feb     40.00
2012     Mar      3.30

2 个答案:

答案 0 :(得分:1)

主要问题是,您尝试将每年的总数存储在一个不存在的变量中 - 一个额外的数组元素Raindata[year][month+1]。您只声明了一个5x12阵列,并且您尝试将其用作5x13阵列。由于数组存储在内存中的方式,您将[month+1]值存储在下一年的数据中。

相反,我添加了一个额外的数组(RainTotal[NUMYEARS]),并用它来存储每年的总数。

#include <stdio.h>


#define NUMMONTHS 12
#define NUMYEARS 5


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


// Global variables
// These are available to all functions
float Raindata[NUMYEARS][NUMMONTHS];
float RainTotal[NUMYEARS];
float sum = 0.0;
char years[NUMYEARS][5] = {"2011","2012","2013","2014","2015"};
char months[NUMMONTHS][4] =
  {"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;
  // Input Data
  for (int year=0;year < NUMYEARS; year++) {
    for (int month=0; month< NUMMONTHS; month++) {
      printf("Enter rain for %s, %s:\n", years[year], months[month]);
      scanf("%f",&Rain);
      Raindata[year][month]=Rain;
    }
  }
}

// Function to printdata
void printdata() {
  // Print data
  printf ("year\t month\t rain\n");

  //Function to sum rainfall
  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]);
        RainTotal[year] += Raindata[year][month];
    }
    printf("Total amount of rain for year %s: %5.2f\n", years[year],
      RainTotal[year]); //prints the total amount of rain every year
  }
}

我还做了一些其他的改变:

  • 您声明yearsmonths第二个数组元素似乎等于年数和月数。相反,它应该是char字符串的预期长度加1(对于空终止符)。

  • 我更改了您的打印语句以显示您声明的字符串,因此Enter rain for 2011, Jan:代替Enter rain for 1, 1:

  • 我修复了你的函数原型(也被另一个用户指出)

  • 清理您的格式 - 看看一些流行的编码标准 - 当您要求别人提供帮助时,这将使您的代码更具可读性:)

答案 1 :(得分:0)

#include <stdio.h>

#define NUMMONTHS 12
#define NUMYEARS 5

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

// global variables
float RaindataTotal[NUMYEARS];
float Raindata[NUMYEARS][NUMMONTHS];
float sum = 0.0;
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;

    while (1) {
        printf("Do you want to input Precipatation data? (y for yes)\n");
        scanf("%c", &enterData);

        if (enterData == 'y') {
            inputdata();
            printdata();
        }
        else if (enterData == 'n') {
            printf("No data was input at this time\n");
            break;
        }
    }
    return 0;
}


// function to inputdata
void inputdata() {
    float Rain = 1.0;
    for (int year = 0; year < NUMYEARS; year++) {
        for (int month = 0; month< NUMMONTHS; month++) {
            printf("Enter rain for %s, %s:\n", years[year], months[month]);
            scanf("%f", &Rain);
            Raindata[year][month] = Rain;
        }
        printf("\n");
    }
}

// Function to printdata
void printdata() {
    printf("year\t month\t rain\n");

    //Function to sum rainfall 
    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]);
            RaindataTotal[year] += Raindata[year][month]; 
        }
        printf("Total amount of rain for year %s: %5.2f\n\n", years[year], RaindataTotal[year]); //prints the total amount  ??? of rain every year 
    }
}