Click here to Skip to main content
15,031,085 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!
   
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!
OriginalGriff 13-Apr-21 4:39am
   
If you write code to do two nested loops, how can it be hard to "wrap your heard around" taking one of them out?
Scayron 13-Apr-21 6:34am
   
changing the function to add more parameters prints "too many arguments declared"...
removing one outer loop to show one year avg only prints that year in that function, even though i have more code in the same function below to find cumulative avg-- this doesn't show in the terminal...
same problem occurs for max and min

there mate... these are few parts where i have to wrap my head around all this to even understand what i need to change etc... :)
OriginalGriff 13-Apr-21 6:57am
   
So let's get this right: did you write the original code?
And be honest: if you try to give us the impression that you know more than you do then our answers are based on that impression, and may not be helpful to you ...
Scayron 13-Apr-21 7:17am
   
i definitely don't know more... i understand the fundamentals of this 2D array concept but in terms of manipulating this 2D array for uses in function, function parameter passing or trying to access individual rows/columns(like trying to access individual years) is where i'm at a complete loss
OriginalGriff 13-Apr-21 7:41am
   
So how did you write the original code? Because my suspicion is growing that you found that somewhere, thought "that'll do", and now want somebody to fix it so it matches your homework and can be handed in without you learning anything from the experience - and we aren't about that here.
Scayron 13-Apr-21 12:29pm
   
our class teacher showed us the input part and told us to write the output parts ourselves which i did on my own... though at the time i had no idea how to syntax multidimensional arrays, but learned it after going through quite a few websites... i'm trying to learn the best i can but lots of these are still head-spirallingly difficult as this whole subject is still so foreign to me...
OriginalGriff 13-Apr-21 12:46pm
   
What part of the array is giving you problems? Maybe I can explain it better ...
Scayron 13-Apr-21 14:14pm
   
fixed most of the problem...
still stuck at the avg part...
done with the total avg of three years, but how do i find the average of individual row here(per year)?
this is currently giving me a headache...

OriginalGriff 13-Apr-21 15:06pm
   
Just look at the code you have for calculating the total average.
It has two loops: the outer one (x) for years, the inner one (y) for months. And you access the array using [x][y] to get a particular month of a particular year.

So what would happen if x didn't change? What if it was passed into the function as a parameter and the outer loop wasn't there?
Hint: stop using single character variable names. Change "x" to "year" and "y" to "month" and it should be more obvious.

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