Hello welcome back.
In this lecture demonstration I want to provide a solution to
our weather program that I talked about in our previous challenge.
So let's start up code blocks create a project.
I’m going to name this project weather.
And again, just a reminder this application is going to allow us
to get yearly totals and monthly averages for rainfall amounts.
So right at the top, you want to provide comments telling
you what the program is.
Again, this program is going to be finding yearly totals, yearly average
and monthly average for several years of rainfall data, 5 years essentially.
So right at the top, I’m going to define a couple constants, and
we haven't gone over the define preprocessor directive in detail
yet, but you're familiar with this, it's not real complicated.
So I’m going to include it and I want to write nice quality code.
So I’m just going to define one that's called months can
be equal to 12 and a constant.
You can also use the constant modifier, and I’m going to also define
one that's called years equal to 5.
That's we're going to star.
And the first thing we really want to do in our main function is we
want to create our array which is a two-dimensional array and
we want to initialize the data.
So we're going to initialize rainfall amounts, rainfall data for
2010, 2011 and 2015 or something.
And so we know how to create a two-dimensional array, we just
specified the data type name and I think they should be a float
because floats have decimal spots and rainfall amounts can be decimal.
So we're going to just say float, we're going to name it rain and
then we're going to specify that we want to have 5 rows and 12 columns.
So we're just going to say years which is our constant and months, and
then we're going to initialize it.
We've declared it of that size and now we want to initialize it.
And you can initialize it by having brackets just like that and then you
can have inside brackets for each row.
We could also make this array constant if we wanted
to but we're not going to.
Now I have already done this data and I don't want to
type in what would this be.
This would be 10 times 50 or 12 times 5, that would be 60 elements.
I don't want to type in 60 elements right now because it's going to
take up time and you're just going to sit there and fall asleep.
So I’m going to copy and paste in my initialization
and initialize some data.
This is just some dummy data; it might not be correct.
But you'll see that we have brackets for each row and so we have 5 rows.
We're separating each row by a comma and we have these curly
braces inside there for each row.
And then inside the curly braces, we're separating each
monthly value by a comma.
So we're separating rows by commas and we're separating
values inside rows by commas.
Here's a bunch of yearly rainfall amounts.
So for example this first row would represent year 2011 and the
first column would be January.
So in January of 2011, we had 4.3 inches of rain.
Down here if we were to look at 6.7, this would be for the year 2012, 13.
For the month of march we had 6.7 inches of rain.
So you can see how this represents years and months for rainfall.
Now that we've initialized all of our data, we can actually initialize
or create our other local variables.
So we're going to just create a year and a month because
this will help keep our years straight and our months straight.
So I’m just say year month I’m going to create them on the same
line, and I’m also going to create a couple floats subtotal and total.
Subtotal will be yearly totals; total will be the total rainfall so
that I can get the yearly average.
And we'll also figure out how to get monthly averages.
So the first thing I need to do is I need to have a for loop
that's going to print out our rainfall for a particular year.
So first I’m going to print out on a single line.
I’m just going to print out year, and I’m going to add a tab...
add a tab to it make it better formatted.
Let's add 2 tabs, we'll see how this looks once I display it.
And then I’m going to put rainfall, and I’m going to put that in inches
and I’m going to add a line feed.
And again, you would want to test this to see what it looks like.
And then I’m going to go into my loop.
So what do I want to do in order to calculate the subtotals for each
year display them, I want to go through all of the rows, all of the
years, and then for each year, I want to go through all the months
because I need to add the months up.
So our outer loop is going to start off with the first year, which is
2011 but it's actually 0 inside the array because arrays are 0 indexed.
And I’m also going to initialize another local variable inside here.
Remember, for loops are very flexible.
You can actually create other variables in the for statement.
I didn't need to create a variable that's used outside of the for
statement on lines 19 or 20.
So here I’m just going to create a variable called total or
initialize my total variable.
And then I’m going to go through until my year is less than my years which is
constant, and I’m going to increment my counter loop control variable year.
So the year and the month are loop control variables on lines 19.
And this is going to represent my outer loop.
It's two-dimensional array, so I’m going to need an inner loop as well.
And my inner loop is going to go through each month.
So here I’m going to say month equals 0, subtotal for each month
is also equal to 0 initialize it.
And I’m going to go through until I’m done with all my months.
And I’m going to increment my loop control variable, and
this will be my inner loop.
So in my inner loop I can actually determine the subtotals, I
don't need to display anything.
So all I’m going to do in my inner loop is I’m going to add
to my subtotal for each year, my subtotal is my yearly total.
And here I’m just going to access the array.
My array name is going to be rain.
So I can just say rain sub year, so this would be all for
2011 initially because my outer loop and then for each month.
And I’m adding to it.
So I’m saying subtotal equals subtotal plus rain plus the array value.
So the inner loop go through all the months for that year
create a subtotal of it.
Now I want to display the subtotal outside of the loop
because I only want to display the subtotal 5 times for 5 years.
So here I can a nice little print statement that helps
format the data a bit and also helps display it properly.
Now this will be the year and the subtotal.
So we'll probably want to add to the subtotal, I would say a
couple tabs, but we'll see what it looks like, so slash t slash t.
And then the only other thing we want to do is we want to add
to the total because we need to have the total for all years.
So here we can just say total plus equals subtotal.
And then you'll notice the subtotal is actually getting re-initialized
every time you go to the inner loop, so we don't have to set that back.
And here we just want to make sure the indentation is good.
Again, notice my indentation for each blocks, and I actually don't
need the brackets on 27 and 29 since it's a single statement
but I’m going to leave them there.
Now that I’ve done that after I printed out the yearly rainfall or
the rainfall for each year, I can now display the yearly average because
I have enough data, I have the total rainfall in each month rainfall.
So here I’m just going to display the yearly average which is just
the total divided by the years.
So add another print statement here.
Again, another good place to test.
So let's go ahead and build this, make sure we don't have any
errors since it is a lot of code.
And we have an undeclared variable year.
When you see a compiler error, it's sometimes misrepresentative
of what the actual error.
So in this case it told me I had an error on line 19, but really
what was causing the error was the previous statement that
didn't have an ending semicolon.
Let's run this and see if we can see the yearly rainfall,
see how it's formatted.
So here we go year 2010 rainfall in inches.
So we're probably adding too many tabs after when we displayed the subtotals,
then the yearly average is 39.4.
So we probably want to fix that by just getting rid of a tab here.
Remember, you can add a tab in your output by using
escape sequence slash t.
So that looks pretty good at least.
Now we have to have another loop that's going to display
the monthly averages.
So before I display the monthly averages I want to print
everything out on a single line.
So I’m just going to print out some information using printf.
I’m going to print out my monthly averages and add 2-line
feeds so it's formatted better.
Nnd then I’m going to print out my other information.
We're just going to separate the months by a few spaces
and then add a new line.
So we're going to do the same when we go through our for loop.
Now we need another for loop now to get the monthly averages.
So in this for loop, the outer loop is going to start at month.
Instead of going by each row, what I’m going to do here is I’m going
to go through each column because I need to get the monthly average.
So I’m going to start and I’m going to go through each column
which is January February March represent columns.
And for each column, I’m going to go through all the years for that
column and then I can use the subtotal and the total again variables.
So here I’m going to say month equals 0, month is less than
months and month plus plus.
And then my inner loop is going to go through the years.
So here I’m just going to say for year equals 0.
And remember that I also want to reinitialize my subtotal.
So here I’m going to say subtotal equals 0 because I want to start at
0 once I get my new year, go through all the years and then increment
the year, the leap control variable.
And inside here I want to put in my subtotal which is
just going to be equal to my array for a year and months.
And so it's important to understand what's going on here
is we switch the way that we're identity through the array.
And the first loops we iterated by going through each row
and looking at each column.
In this one we're actually going through each column and looking
at that specific row because we're calculating monthly averages.
So we get the subtotal, and then we just want to print out our subtotal
divided by years for that month.
So that it matches up with our output on line 37.
So we're just going to do something like this, and we're
going to do it with 4.1 format, subtotal divided by years.
And we add a couple spaces so that try to line it up as best, we
might want to add another space.
And then after we've printed everything out, we can just add a line
feed for readability, and we're done.
And that will give us the monthly averages because we
got all the subtotals then we divided it by the years.
Now let's build this, no errors and run it.
And now we'll see additional output for monthly totals
or monthly averages.
So here we see that monthly averages and the format
doesn't line up real good.
So it all depends because of the character's output.
We actually probably want to decrease the space, only add one space there,
let's see if that does a better job.
Now when we run it see if it looks better because you want to be
able to tie up each month with the averages, that's a little bit better.
We still get a little off because the months are 3 characters
and the integers are 2.
We mainly got screwed up between October and November
because there's only one space.
Now I can fix that by just going like that.
So that should make the format all nice.
But you can see how we're just printing out rainfall amounts
for years and monthly averages.
I hope this got you familiar with two-dimensional arrays, how to
iterate through them, how to assign arrays and how to access data.
Thank you.