Click here to Skip to main content
15,887,966 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

What I have tried:

C++
#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();
}
Posted
Updated 12-Apr-21 20:02pm
v2

1 solution

Your code already finds and prints the average value: that's what the avg_rainfall function is there for.

It's a trivial matter to change it to a similar function that takes a second parameter:
C++
double avg_rainfall(double rainfalls[][12], int year)
and remove the outer loop. You wrote that code, it's not exactly complicated to take stuff out! :D

And modifying that code to produce similar "max" and "min" functions is simple too - just store a "current max) value (set it to the first element) and compare it to all the others. If the new element is bigger, set teh "current max" to the new element, and keep checking. At the end of the loop, you have the largest.
Very similar code for the minimum.

If you wrote that code, it shouldn't take you more than five minutes to write and text the new methods.
If you didn't, throw it all away and start again: that's pretty poor quality code even for a student exercise so whoever you copy'n'pasted it from doesn't know what he's doing any better than you do at this point!
 
Share this answer
 
v2
Comments
CPallini 13-Apr-21 2:02am    
'If you wrote that code...'
Do you doubt it, by any chance? :-D
My 5.
OriginalGriff 13-Apr-21 2:06am    
Would I do that? :D
Scayron 13-Apr-21 2:11am    
no in the case of avg rainfall i meant how would i take avg rainfall for year 1, then year 2 and year 3...? same for max and min... the code i gave here simply takes the average of all the three years since it's dividing by 36... so how would i take avg of the individual years??... i'm a newbie coder, began less than 2 months so not completely grasping what is "poor quality code" yet :3
OriginalGriff 13-Apr-21 2:39am    
As I said:
"It's a trivial matter to change it to a similar function that takes a second parameter:

double avg_rainfall(double rainfalls[][12], int year)

and remove the outer loop. You wrote that code, it's not exactly complicated to take stuff out! :D"

You wrote the code, you know how it works at the moment. So why can't you modify it when most of the modifications just involve removing code ... ?
Scayron 13-Apr-21 4:08am    
"You wrote the code, you know how it works at the moment. So why can't you modify it when most of the modifications just involve removing code ... ?"

how do i say this without sounding rude... well cuz as "I said" i'm a newbie at this :)... it takes me a good while to even wrap my head around what's even going on most of these coding exercises, but whatever, thanks for helping out!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900