# Thread: Help me clean this up please.

1. ## 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. 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];```

3. 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. Yeah that was actually a typo. Sorry...

5. > but the code is ugly.
Guaranteed way of making fewer people look at it then.
Next post please.

6. 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. %lf is double or long float if you will.

8. 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. It runs fine for me. Try clicking on the executable, or in your IDE press the Run button.

10. 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. Don't create a project, just go New > Source File, then paste your code, then press F9.

12. 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. Are you getting errors at the bottom of your screen?

14. 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. 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.

Popular pages Recent additions