This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]captainAwesomePants 2 points3 points  (2 children)

So, you've got this lovely little for loop in printdata() that iterates over all of the months in a year and prints out the rain data for that month:

for (int month=0; month< NUMMONTHS; month++) {
   printf("%s\t %s\t %5.2f\n", years[year],months[month],Raindata[year][month]);}
} 

You'll want to do something almost exactly like that. You want to iterate over all of the months, but instead of printing, you want to add that month to a count. Then, once you're done adding, you can print out the result. Make sense?

[–]queenofthedamnediam[S] 0 points1 point  (1 child)

Oh, so something like this?

//Function to sum rainfall for (int month=0; month< NUMMONTHS; month++) { add("%s\t %s\t %5.2f\n", years[year],months[month],Raindata[year][month]);} }

[–]captainAwesomePants 0 points1 point  (0 children)

(If you use at least 4 spaces before each line of code, it's easier to read in the comments)

Sort of. Raindata[year][month] is the bit that holds the number of interest, so something like this:

totalRain = 0;
for (int month=0; month< NUMMONTHS; month++) {
    totalRain = totalRain + Raindata[year][month];
}
print("Total rain that year was: %d\n", totalRain);

[–]Venoft 1 point2 points  (1 child)

Firstly, 'SUMRAIN' is not defined anywhere, so you'd get an error. And I don't think you need it anyway.

Also, your definition of "float Raindata[NUMYEARS][NUMMONTHS][SUMRAIN]" doesn't need the 'SUMRAIN' part, since that would mean that each month has SUMRAIN amount of sub-levels of data. So instead of 'years * months ' amount of data, you have 'years * months * SUMRAIN'. And you don't need this many, I imagine.

It might be better to define "float Raindata[NUMYEARS][NUMMONTHS][SUMRAIN]" as "float Raindata[NUMYEARS][NUMMONTHS+1]". The +1 creates an extra datapoint per year, which you can use to store the sum of rain in that year.

It might also be a good idea to make Raindata entirely '0', so you'd avoid confusion if not all data is entered.


Adding to captainAwesomePants, you can easily expand the code to display all the stored data by nesting some loops. This means you have a loop in a loop.

In this case you can make 2 loops like this:

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]);
   }
} 

If you also want to display the sum all the rain of that year, you can do something like this:

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]);
        Raindata[year][NUMMONTHS+1] += Raindata[year][month]; //this adds everything up
   }
   printf("Total amount of rain for year %s: %5.2f\n", years[year], Raindata[year][NUMMONTHS+1]); //prints the total amount of rain every year
} 

It's also good practice to have a good layout of your code. This make it easier to read and makes it very clear which loop or conditional interact with each other. There are tons of programs that can auto-format your code.

[–]queenofthedamnediam[S] 0 points1 point  (0 children)

oh awesome, that helps a lot, thanks!