How to arrange and find average, max and min values in 2D array?

Hello, i have been assigned a coding exercise to use a 2d array to make a CPP program that stores rainfall data. I have to store rainfall data for the last three years so i initialized an array like --double rainfalls[3][12]-- Now how do i display the rainfall values for each year seperately? Like in a vertical list there will be 1st year values under the name 1st year, parallel to that there will 2nd year values under 2nd year and same for the 3rd year...
Also how do i find the average rainfall of each year? Minimum and max of each year?

If anyone can help out I will highly appreciate it. Thank you.
Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  #include <iostream>
#include <conio.h>
using namespace std;

void enter_rainfalls(double rainfalls[][12])
{
    for(int x=0; x<3; x++)
    {
        for(int y=0; y<12; y++)
        {
            cout<<"Please enter rainfall"<<endl;
            cin>>rainfalls[x][y];
        }
    }
}

void print_rainfalls(double rainfalls[][12])
{
    cout<<"\nPrinting rainfalls...\n"<<endl;

    for(int x=0; x<3; x++)
    {
        for(int y=0; y<12; y++)
        {
            cout<<rainfalls[x][y]<<" ";
            ;
       }cout<<endl;
    }   
}

double avg_rainfall(double rainfalls[][12])
{
    double sum=0.0;
    double avg=0.0;

    for(int x=0; x<3; x++)
    {
        for(int y=0; y<12; y++)
        {
            sum += rainfalls[x][y];
        }
    }
    avg = sum / 36;
    cout<<"\nThe average rainfall of the last three years = "<<avg;
    
    return avg;
}

int main(void)
{
    double rainfalls[3][12] = {{10.3, 12.4, 12.0, 15.5, 13.5, 12.6, 10.7, 8.6, 9.2, 8.0, 9.0, 10.0},
                               {9.3, 10.4, 8.0, 15.5, 11.5, 13.6, 11.7, 9.6, 9.6, 9.0, 11.0, 12.0 },
                               {10.5, 13.4, 10.0, 13.5, 12.5, 14.6, 9.7, 10.0, 10.2, 7.0, 10.0, 11.0}} ;

    enter_rainfalls(rainfalls);
    print_rainfalls(rainfalls);
    avg_rainfall(rainfalls);

    getch();
}
Last edited on
If you use y and m (for years and months) as your loop variables, it might make it a bit clearer.
1
2
3
4
5
6
7
8
9
10
11
12
void print_rainfalls(double rainfalls[][12])
{
    cout<<"\nPrinting rainfalls...\n"<<endl;
    for(int y=0; y<3; y++)
    {
        for(int m=0; m<12; m++)
        {
            cout<<rainfalls[y][m]<<" ";
       }
       cout<<endl;
    }   
}


Now compare with the loop orders swapped.
1
2
3
4
5
6
7
8
9
10
11
12
void print_rainfalls(double rainfalls[][12])
{
    cout<<"\nPrinting rainfalls...\n"<<endl;
    for(int m=0; m<12; m++)
    {
        for(int y=0; y<3; y++)
        {
            cout<<rainfalls[y][m]<<" ";
       }
       cout<<endl;
    }   
}

how do i exactly find the average rainfall per year here? like find average of year 1, year 2 and year 3 individually within the same avg_rainfall function...???
What do you think the inner loop calculates?
All you need is another variable, called say 'sum_for_year'


And rename your other sum to be 'sum_for_all_years'
do i remove the outer loop for this...?

double sum_for_year=0.0;
double avg_for_year=0.0;

for(int x=0; x<3; x++)
{
for(int y=0; y<12; y++)
{
sum_for_year += rainfalls[x][y];
}
}
avg_for_year = sum_for_year / 12;
//cout

or is this correct...?
Yeah, pretty much like that.

Don't forget that sum_for_year needs to be reset back to 0

Easy if it's like this.
1
2
3
4
5
6
7
8
9
    for(int x=0; x<3; x++)
    {
        double sum_for_year=0.0;
        for(int y=0; y<12; y++)
        {
            sum += rainfalls[x][y];
        }
        double avg_for_year = sum_for_year / 12;
    }

thanks very much, it worked!
Topic archived. No new replies allowed.