Thread: Looking for help in a C code!

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    2

    Looking for help in a C code!

    What I am trying to understand, is how do I add a function to summarize the rainfall for each year, using the following code as the foundation. What would I need to add, and where in order to accomplish this? Also if you could please explain briefly as well it would be highly appreciated!


    Thank you so much!!!


    The code:

    insert
    Code:
    // C 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]);
     }
     }
    }
    Last edited by mindriotus; 10-08-2018 at 06:05 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What you need to add is a function called say summarisedata()

    Where you need to add it, is everywhere you see printdata()

    Eg.
    Code:
    // function prototypes
    void inputdata();
    void printdata();
    void summarisedata();
    Have you learnt anything about programming yet, or does your tutor just give you 'fill in the blank' exercises.
    In other words, where did you come by this code?
    Because if you wrote it yourself, the question is stupidly simple for you to solve.

    Edit:
    Oh, here's where you found your code.
    Overlooking Something in an Array
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2018
    Posts
    2
    Yeah, unfortunately I was given 'fire and forget' type of material and I was given a fill in the blank exercise. I work 50 hours a week and nights so it's hard to get help, forums are the best way if at all possible.

    I've been playing with it this morning and this is where I am not now...

    Code:
    #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];
    int sum = 0;
    int avgsum = 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
        sum = 0;
    
        printf ("year\t month\t rain\n");
        for (int year=0;year < NUMYEARS; year++)
        {
            for (int month=0; month < NUMMONTHS; month++)
            {
                sum += Raindata[year][month];
                printf("%s\t %s\t %5.2f\n", years[year],months[month],Raindata[year][month]);
            }
        }
    
        printf("Total rain of all five years combined: %d\n", sum);
        printf("Average sum of each individual year: %d\n", avgsum);
    }
    
    So now I have the sum of all the months functioning properly... how can I get the individual sum for each individual month 1-5?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well there's nothing wrong with re-ordering the loops.
    Code:
        for (int month=0; month < NUMMONTHS; month++)
        {
            for (int year=0;year < NUMYEARS; year++)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 01:58 PM
  2. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  3. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  4. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

Tags for this Thread