1

This is the code I wrote, but instead of showing a total for each line, it shows:

line 1: sum of line 1 (this shows correctly)
line 2: sum of line 1 and line 2
line 3: sum of line 1 thru 3
line 4: sum of line 1 thru 4
line 5: sum of line 1 thru 5

Can someone tell me which part I did it wrong?

#include <stdio.h>

#define YEARS 5
#define MONTHS 12

int main() {
    int years;
    int months;
    int TotalYearlyRainfall = 0;
    int rain[YEARS][MONTHS] = {
        {1,1,1,1,1,1,1,1,1,1,1,1},
        {2,2,2,2,2,2,2,2,2,2,2,2},
        {3,3,3,3,3,3,3,3,3,3,3,3},
        {4,4,4,4,4,4,4,4,4,4,4,4},
        {4,4,4,4,4,4,4,4,4,4,4,4}
    };
    
    printf("Total Rainfall\n");
    
    for (years = 0; years < YEARS; years++) {
        for (months = 0; months < MONTHS; months++)
            TotalYearlyRainfall+=rain[years][months];
        
        printf("%i\n", TotalYearlyRainfall);
    }

    return 0;
}
4
  • 4
    Don't describe the output. Just show it exactly. That is, give the exact expected output and actual output. Commented Jan 10, 2022 at 0:52
  • 1
    Also, years, months, and TotalYearlyRainfall can be locally scoped to your loops. Commented Jan 10, 2022 at 0:57
  • 2
    you need to reset your total rainfall variable. your code is currently added all the values up. Commented Jan 10, 2022 at 1:21
  • IMHO, I suggest that the variable's name TotalYearlyRainfall be changed to MonthlyRainfall, because TotalYearlyRainfall is kind of misleading. Commented Jan 10, 2022 at 1:34

2 Answers 2

3

You have to initialize TotalYearlyRainfall to 0 at the start of each year.

After your first for loop, set:

TotalYearlyRainfall = 0;
Sign up to request clarification or add additional context in comments.

1 Comment

Better yet would be to locally scope this variable to the outer loop.
2

The variable's name is TotalYearlyRainfall, which makes me think it should have the sum of all the twelve months. If you would like to have the sum of each month separately, I suggest that the variable's name be MonthlyRainfall or something similar.

The problem with the original code is that the variable TotalYearlyRainfall or MonthlyRainfall should be reset to zero after each inner for loop, because it acts as the counter of the sum of each month.

Remember to use this form : int main(void) or int main(int argc, char *argv[]) to be standard compliant.

The following code is C89 compliant.

#include <stdio.h>

#define YEARS 5
#define MONTHS 12

int main(void) {
    int years;
    int months;
    int MonthlyRainfall = 0;
    int rain[YEARS][MONTHS] = {
        {1,1,1,1,1,1,1,1,1,1,1,1},
        {2,2,2,2,2,2,2,2,2,2,2,2},
        {3,3,3,3,3,3,3,3,3,3,3,3},
        {4,4,4,4,4,4,4,4,4,4,4,4},
        {4,4,4,4,4,4,4,4,4,4,4,4}
    };
    
    printf("Total Rainfall\n");
    
    for (years = 0; years < YEARS; years++) {
        /* This variable should be reset to zero after each inner loop */
        MonthlyRainfall = 0; 
        for (months = 0; months < MONTHS; months++)
            MonthlyRainfall+=rain[years][months];
        
        printf("%i\n", MonthlyRainfall);
    }

    return 0;
}

At this time, C99 has been widely supported by compilers, maybe, you would like to consider the following code also.

#include <stdio.h>

#define YEARS 5
#define MONTHS 12

int main(void) {
    int rain[YEARS][MONTHS] = {
        {1,1,1,1,1,1,1,1,1,1,1,1},
        {2,2,2,2,2,2,2,2,2,2,2,2},
        {3,3,3,3,3,3,3,3,3,3,3,3},
        {4,4,4,4,4,4,4,4,4,4,4,4},
        {4,4,4,4,4,4,4,4,4,4,4,4}
    };
    
    printf("Total Rainfall\n");
    
    for (int years = 0; years < YEARS; years++) {
        // This variable should be reset to zero after each inner loop
        int MonthlyRainfall = 0; 
        for (int months = 0; months < MONTHS; months++)
            MonthlyRainfall+=rain[years][months];
        
        printf("%i\n", MonthlyRainfall);
    }

    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.