Hello and welcome back.
I’d like to provide you with another challenge, another assignment.
This assignment is going to deal with multi-dimensional arrays,
get a little more familiar with that, mainly a 2D array as it is
something that you should understand.
So in this challenge you're going to create a c program
that uses a two-dimensional array and a weather program.
This program will find the total rainfall for each year, the
average yearly rainfall and the average rainfall for each month.
So those 3 things are going to be output.
The input is going to be a two-dimensional array with
hard-coded values for rainfall amounts for the past 5 years.
So what this means is you should create a 2D array that
has 5 rows and 12 columns.
The rows are going to represent the past 5 years and then the columns
are going to represent the months.
So how much rain you got in January February march April for each year.
And the rainfall amounts should be floating point numbers.
Because you often get you know 7.8 inches for rain for a month, they're
going to need to be floating points.
When you create this two-dimensional array, make sure that it's a float.
The output for the program is going to look something like this.
Here I just formatted it a certain way, but what you should
first display as output is everything that's in the 2D array.
So you should have just a simple loop that goes through the 2D
array, displays the year for the array and the year is just going
to be whether it's the first row, the second row, the third row,
you can just create an integer.
And increment it and then the rainfall is going to be all of
the rainfall for all the months.
So you have to add them as you go.
So what you should first do is you should actually have a for loop and
an inner for loop and go through it to calculate the total rain for each year
and then have a loop to print it out.
And also, in that first loop or actually you probably want
to create a second loop with an inner loop that goes through
all the months for each year and calculates the monthly averages.
So again, the way that I envision the design for this program is
2D array floating point numbers, you're going to need to initialize
it with all hard-coded data.
You're going to have a first loop which is going to go through,
and it's going to calculate the totals for the entire year.
And then then it's easy to get the yearly average because you just
take the totals and divide it by the number of years which is 5.
So the yearly average, you don't have to calculate in a loop.
But you will need to calculate the total rainfalls for
each year in the loop.
And so, I would have a loop.
You're going to have to have an inner loop because every dimensional
array you have to have an inner loop, two dimensions have one inner loop.
So an outer loop and an inner loop, a nested loop, I should say.
And you go through your first array.
You start at the year and so let me go through my tips actually
I think I put this in the tips.
But this is the output, you can format it any way you want.
But what I’m seeing here again is yearly rainfall, yearly average
and then monthly averages.
So you want to first you want to initialize your 2D array
with hard-coded rainfall months.
We know how to initialize an array.
For a 2D array, you can initialize rows.
So you can have brackets inside of the brackets to represent
each row and each row has 12 columns so you would have 12
values, 12 floating point values.
And then you separate each row by commas and you just
create new brackets, we should know how to do that.
And then again just a reminder to iterate through a two-dimensional
array, you're going to have to have a nested loop, basically one inner loop.
If it was a 3D array, you'd have to have 2 inner loops.
The key to this solution will be to visualize a 2D array
and understand how to iterate through one via nested loop.
If you know how to iterate through a nested array or a 2D
array, you're going to be fine.
As you are iterating you can keep a running total which is our first
set of loops, an outer loop will iterate by the year in the inner
loop or iterate by the month and you can keep a running total to get
the total rainfall for all years.
So your first goal should get you to just get that
total rainfall for all years.
And as you're getting the total rainfall, you can just print
out the yearly total as well.
Then after you're out that loop, you can print out the yearly average.
Then you're going to need another loop to display the monthly average.
And to display the monthly average has outer loop for each month and
the inner lip goes through each year.
I think this will give you a monthly average.
So the yearly ones can be in that first loop and the
monthly will be in the second.
So that should say to get them to get the monthly average rainfalls.
So you're going to learn all about initializing arrays,
you're going to understand how to iterate through a two-dimensional array,
a lot of important concepts here.
Good luck, and I’ll provide a solution in the next lecture. Thank you.