Help me clean this up please.

This is a discussion on Help me clean this up please. within the C Programming forums, part of the General Programming Boards category; Query the user for rainfall numbers for a city for the 12 months of this year as well as last ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    21

    Help me clean this up please.

    Query the user for rainfall numbers for a city for the 12 months of this year as well as last year. Display this output in table form along with the total rainfall and the average rainfall for each year. The programming basically accomplishes the task, but the code is ugly.

    Code:
    #include <stdio.h>
      #include <string.h>
      #define max_number 12
      #define months 12
    
      int main(void)
     {double current[max_number][12],
             current_rain[12],
             last[max_number][12],
             last_rain[12],
             last_sum,
             current_sum,
             last_average,
             current_average;
      int i;     
          for(i=0;i<12;i++)
           {printf("Enter this year's rainfall for the current month:  ");
            scanf("%lf", &current_rain[i]);
            printf("Enter last year's rainfall for the current month:  ");
            scanf("%lf", &last_rain[i]);
            printf("Now assume the next month is the current month.\n");
           }
    
          
    current_sum=current_rain[0]+current_rain[1]+current_rain[2]+current_rain[3]+current_rain[4]+current_rain[5]+current_rain[6]+current_rain[7]+current_rain[8]+current_rain[9]+current_rain[10]+current_rain[11];
    last_sum=last_rain[0]+last_rain[1]+last_rain[2]+last_rain[3]+last_rain[4]+last_rain[5]+last_rain[6]+last_rain[7]+last_rain[8]+last_rain[9]+last_rain[10]+last_rain[11];
    current_average=current_sum/months;
    last_average=last_sum/months;
    printf("                      Table of Monthly Rainfall\n");
    printf("           January  February    March    April    May    June  July\n");
    printf("This year: %lf      %lf       %lf     %lf    %lf    %lf   %lf\n", 
    current_rain[0], current_rain[1], current_rain[2], current_rain[3], 
    current_rain[4], current_rain[5], current_rain[6]);   
    printf("Last year: %lf      %lf       %lf     %lf    %lf    %lf   %lf", 
    last_rain[0], last_rain[1], last_rain[2], last_rain[3], last_rain[4], 
    last_rain[5], last_rain[6]);
    printf("        August    September   October   November   December\n");
    printf("This year: %lf       %lf        %lf       %lf        %lf\n", 
    current_rain[7], current_rain[8], current_rain[9], current_rain[10], 
    current_rain[11]);
    printf("Last year: %lf       %lf        %lf       %lf        %lf\n", 
    last_rain[7], last_rain[8], last_rain[9], last_rain[10], last_rain[11]);
    printf("Total rainfall this year: %lf\n", current_sum); 
    printf("Total rainfall last year: %lf\n", last_sum);
    printf("Average rainfall this year: %lf\n", current_average);
    printf("Average rainfall last year: %lf\n", last_average);
    return 0;
    }
    Any suggestions? Also, when the program displays the rainfall data, it runs the number off about 5 decimal places. For instance, a value of 3.6 would be displayed as 3.60000. This is offsetting the values so that they aren't being displayed under the appropriate month. How do I reduce the number of decimal places displayed?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Things like this:
    Code:
    current_sum=current_rain[0]+current_rain[1]+current_rain[2]+current_rain[3]+current_rain[4]+current_rain[5]+current_rain[6]+current_rain[7]+current_rain[8]+current_rain[9]+current_rain[10]+current_rain[11];
    look alot prettier in simple loops.

    Code:
    for (i = 0; i < 12; i++)
         current_sum += current_rain[i];
    Last edited by SlyMaelstrom; 11-30-2005 at 06:31 PM.
    Sent from my iPadŽ

  3. #3
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Quote Originally Posted by SlyMaelstrom
    Things like this:
    Code:
    current_sum=current_rain[0]+current_rain[1]+current_rain[2]+current_rain[3]+current_rain[4]+current_rain[5]+current_rain[6]+current_rain[7]+current_rain[8]+current_rain[9]+current_rain[10]+current_rain[11];
    Code:
    for (i = 0; i < 11; i++)
         current_sum += current_rain[i];
    The canonical idiom for looping from 0 to 11 would be:
    Code:
    for (i = 0; i < 12; i++)
         current_sum += current_rain[i];
    or (probably not as good):
    Code:
    for (i = 0; i <= 11; i++)
         current_sum += current_rain[i];

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Yeah that was actually a typo. Sorry...
    Sent from my iPadŽ

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > but the code is ugly.
    Guaranteed way of making fewer people look at it then.
    Next post please.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    sorry if this post is old and dead, but whats %lf mean ? ive had %d and %s but not %lf.

    sorry but it confused me, i understand the rest of the code.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    %lf is double or long float if you will.
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    cheers mate, dint make sense at first lol.

    btw, how do u get it to run, this code compiles but dont run, nothing will run that i do so i try a code thats been used by ppl n it still dont run.. ne suggestions?

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    It runs fine for me. Try clicking on the executable, or in your IDE press the Run button.
    Sent from my iPadŽ

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    still nothing.

    in Dev C++... i click NEW, PRJECT.. shall i choose WINDOWS APPLICATION or something else?

    i compile fine, run... nothing happens, even if i click the ex in folder.

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Don't create a project, just go New > Source File, then paste your code, then press F9.
    Sent from my iPadŽ

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    yeh, it compiles but no run...

    even in my docs, where i save it, there isnt an ex, its just a source file... how do i run it?

    reece.

  13. #13
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Are you getting errors at the bottom of your screen?
    Sent from my iPadŽ

  14. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    it works not, but it dont say table of monthly rainfall.

    it says enter this months, then last months... then assume this month is last month.. keeps repeating....

  15. #15
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Try it 12 times... that's what the loop says to do. This program isn't very user friendly with the info it gives in the outputs.

    Also add a getchar() or two at the end of the program so it doesn't close on you.
    Sent from my iPadŽ

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Establishing 'make clean' with GNU make
    By Jesdisciple in forum C Programming
    Replies: 9
    Last Post: 04-11-2009, 09:10 AM
  2. how to know that the buffer is not clean??
    By transgalactic2 in forum C Programming
    Replies: 5
    Last Post: 01-21-2009, 08:56 PM
  3. prepro. clean tool / #id#def mess
    By tomsky in forum Linux Programming
    Replies: 2
    Last Post: 09-08-2005, 07:18 AM
  4. clean out a buffer
    By threahdead in forum C Programming
    Replies: 8
    Last Post: 02-23-2003, 12:04 PM
  5. make clean
    By Jaguar in forum Linux Programming
    Replies: 5
    Last Post: 12-27-2002, 05:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21