I need to modify my code to sum the rainfall per year?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mpalmer1995
    New Member
    • Oct 2018
    • 11

    I need to modify my code to sum the rainfall per year?

    Code:
    // 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;  
       // 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");
       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]);         
          }      
       }
    }
    This is what I've started need help to finish it
    Code:
    // Function to sum toatal rainfall per year
    void printdata(){
    // Print data
    	printf ("rain+month\n");
    	for (int month=0; month< NUMMMONTHS; month++) {
    		
    	}
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Have you considered using a struct for Raindata?

    Code:
    struct Raindata
    {
       int year;
       int RainByMonth[12];
    }
    This would simplify your code. To get the yearly rain amount you would just add the elements of the RainByMonth array.

    What do you think?

    Comment

    • mpalmer1995
      New Member
      • Oct 2018
      • 11

      #3
      where would I place this?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You were going to use global variables to avoid passing arguments to functions. While this is not a good idea, it does work. So you could:

        Code:
        struct Raindata
        {
        
        
        int year;
        
        int RainByMonth{12];
        
        };
        
        
        
        
        Raindata RainByYears[10];
        
        
        int main()
        {
        
        }
        Every function can now access the RainByYears array. I just made up the 10.

        Comment

        • mpalmer1995
          New Member
          • Oct 2018
          • 11

          #5
          would you mind showing me where to place it in my code please and thank you

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            The compiler has got know your types and your functions before you use them in the code.

            All those #include are to provide this information.

            You would place the struct definition above main() but after the #includes.

            You would place the Raindata array after the struct definition but before main().

            Look at my last post.

            Comment

            • mpalmer1995
              New Member
              • Oct 2018
              • 11

              #7
              Code:
              #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;  
                 // 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");
                 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]);         
                    }      
                 }
              }
              So where would I place the sum of rainfall per year

              Comment

              • mpalmer1995
                New Member
                • Oct 2018
                • 11

                #8
                Code:
                #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 Raindata; 
                 int Years;
                 int sum = Raindata + Years;
                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");
                   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]);         
                      }      
                   }
                 //Function to sum rainfall
                	sum = rain + year;
                	printf("rainfall per year");
                }
                I'm getting a bunch of errors

                prog.c:14:6: error: conflicting types for ‘Raindata’
                int Raindata;
                ^~~~~~~~
                prog.c:11:7: note: previous declaration of ‘Raindata’ was here
                float Raindata[NUMYEARS][NUMMONTHS];
                ^~~~~~~~
                prog.c:16:12: error: initializer element is not constant
                int sum = Raindata + Years;
                ^~~~~~~~
                prog.c: In function ‘inputdata’:
                prog.c:44:19: error: subscripted value is neither array nor pointer nor vector
                Raindata[year][month]=Rain;
                ^
                prog.c: In function ‘printdata’:
                prog.c:54:73: error: subscripted value is neither array nor pointer nor vector
                printf("%s\t %s\t %5.2f\n", years[year],months[month],Raindata[year][month]);
                ^
                prog.c:58:8: error: ‘rain’ undeclared (first use in this function)
                sum = rain + year;
                ^~~~
                prog.c:58:8: note: each undeclared identifier is reported only once for each function it appears in
                prog.c:58:15: error: ‘year’ undeclared (first use in this function)
                sum = rain + year;

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  The rule is that you fix only the first error. Often,the first error also causes other errors that will go away when the first error is fixed.

                  In this case, the first error is caused by having two Raindata variables. One is an array of float and the other is an int.

                  Fix that, recompile, and repeat fixing first errors until your errors go away.

                  Comment

                  Working...