Archives
Categories
AlexaRank
Follow Us on Twitter:

    Write a program called rainfall.c that reads in rainfall data from the current year and the previous year, and prints these values as well as summary statistics for both years. The summary statistics should include total annual rainfall and average monthly rainfall. Your program will read the numbers from standard input, one month per line (line 1 is January, line 2 is February, and so on), with the current year being in the first column and the previous year in the second column. Do not prompt the user for input; instead assume that the input is in a file that will be redirected to standard input:
    ./rainfall < input.txt
    If the input file looks like this:
    3.2 4
    2.2 1.6
    .
    .
    .
    and so on, then your program's output should resemble:
    Table of Monthly Rainfall
    Jan Feb ...
    This year 3.2 2.2
    Last year 4.0 1.6
    Total rainfall this year: 34.8
    Total rainfall last year: 42.8
    Average monthly rainfall for this year: 2.9
    Average monthly rainfall for last year: 3.6
    Your program should have a function that reads the input into the arrays, a function that computes the total rainfall for a single year (which you'll use for each of the two years separately), and a function that prints the table.

    I haven't been really good at C programming and have been working on this the whole day. Here is what I have and have no clue of what I'm doing. I need some help. Thanks you.

    #include

    #define SUMMARY_rainfall “rainfall.txt”
    #define NUM_MONTHS 12
    #define NUM_YEARS 2

    typedef enum
    {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec}
    monthly_t;

    typedef enum
    {This year, Last year}
    year_t;

    int scan_table(double rainfall[][NUM_MONTHS], int num_rows);
    void sum_rows(double row_sum[], double rainfall[][NUM_MONTHS], int num_rows);
    void avg_rows(double row_avg[], double rainfall[][NUM_MONTHS], int num_rows);
    void display_table(double rainfall[][NUM_MONTHS], const double year_total[], const double year_avg[], int num_rows);

    int main(void)
    {
    double rainfall[NUM_MONTHS][NUM_YEARS];
    double year_total[NUM_YEARS];
    double year_avg[NUM_YEARS];

    status = scan_table(rainfall, NUM_YEARS);
    if (status == 1)
    {
    sum_rows(year_total, rainfall, NUM_YEARS);
    avg_rows(year_avg, rainfall, NUM_YEARS);
    display_table(rainfall, year_total, year_avg, NUM_YEARS);
    }
    return 0;
    }

    int scan_table(double rainfall[][NUM_MONTHS], int num_rows);
    {
    double rain_amt;
    int rain_year;
    int month;
    FILE *rainfall_filep;
    int valid_table = 1;
    int status;
    char ch;

    initialize(rainfall, num_rows, 0.0);

    rainfall_filep = fopen(RAINFALL_FILE, “r”);
    for(status = fscanf(rainfall_filep, “%d%d%lf”, &rain_year, &month, &rain_amt);
    status == 11 && valid_table;
    status = fscanf(rainfall_filep, “%d%d%lf”, &rain_year, &month, &rain_amt);
    if(jan <= month && month <= dec && rain_amt >= 0 && rain_amt < num_rows)
    {
    stat[rain_amt][month] += rain_amt;
    }
    else
    {
    printf("Invalid year or month -- \n");
    printf(" month is ");
    display_month(rain_month);
    printf(", year is ");
    display_year(year);
    printf("\n\n");
    valid_table = 0;
    }
    if(!valid_table)
    {
    status = 0;
    }
    else if(status = EOF)
    {
    printf("Error in rainfall date format. Revise data.\n");
    printf("ERROR HERE >>> “);
    for (status = fscanf(rainfall_filep, “%c”, &ch);
    status == 1 && ch != ‘\n’;
    status = fscanf(rainfall_filep, “%c”, &ch))
    printf(”%c”,ch);
    printf(” <<<\n”);
    status = 0;
    }
    return status;
    }

    void initialize(double rainfal[][NUM_MONTHS], int num_rows, double value)
    {
    int row;
    monthly_t month;

    for(row = 0; row for(month = jan; month <= dec; month++)
    rainfall[row][month] = value;
    }

    Share and Enjoy:
    • Print
    • Digg
    • Sphinn
    • del.icio.us
    • Facebook
    • Mixx
    • Google Bookmarks
    • Blogplay

    Post to Twitter Tweet This Post

    Related Products:

    One Response to “HELP ME on C Programming PLEASE!!!?”

    • Nick T:

      Since the question reads that “Your program will read the numbers from standard input,” you dont need any file handling capability.

      You define This year and Last year, but enums cannot have spaces in them
      All of your functions are passing by value, i.e. a copy of the data not the original, so nothing will ever be updated, either use pointers or make the data variables global.

      You are over complicating things, you only need 2 arrays of 12, one for this year and one for last year. A better approach would be a structure containg the monthly data, total rainfall and average however incase that is to advanced here is the pseudo code for a simple version.
      You could simplify the code using functions to handle all of the common functionality such as printing the arrays, calculating total rainfall etc.

      PSEUDO CODE

      define ThisYear as floating point array size 12
      define LastYear as floating point array size 12

      define TotalThisYr as floating point value 0
      define TotalLastYr as floating point value 0

      define AverageThisYr as floating point value 0
      define AverageLastYr as floating point value 0

      LOOP FOR 0 to less than 12
      ..get ThisYear[loop index] and LastYear[loop index]
      END LOOP

      output Table of Monthly Rainfall
      output (tab) (tab) JAN (tab) FEB (tab) MAR ….
      output This Yr (tab) (no newline)
      LOOP FOR 0 to less than 12
      ..output ThisYear[loop index] (tab) (no newline)
      ..TotalThisYr = TotalThisYr + ThisYear[loop index]
      END LOOP
      output (newline)

      output Last Yr (tab) (no newline)
      LOOP FOR 0 to less than 12
      ..output LastYear[lop index] (tab) (no newline)

      ..TotalLastYr = TotalLastYr + LastYear[loop index]
      END LOOP
      output (newline)

      AverageThisYr = TotalThisYr / 12.0
      AverageLastYr = TotalLastYr / 12.0

      output Total rainfall this year: (TotalThisYr)
      output Total rainfall last year: (TotalLastYr)
      output Average monthly rainfall for this year: (AverageThisYr)
      output Average monthly rainfall for last year: (AverageLastYr )

    Leave a Reply

    Get Adobe Flash playerPlugin by wpburn.com wordpress themes

    Powered by Yahoo! Answers