Welcome back.
In this video we're going to calculate the average temperature that we have.
And therefore we're going to use a separate method.
So we're going to create an extra method for that.
And I would highly recommend that you try this yourself.
So create a method that takes in the temperature and then calculates the temperature the average temperature
and then returns it okay.
So you can pause the video and try that.
So I hope you did actually try it.
So it's going to be static double because we want to return a double.
And the method name will be calculate average.
And then it will need the temperatures okay.
Well it says every or double temperature.
But in our case it's actually going to be int temperature because that's what we used here.
So we didn't use a double as the temperature here.
But we used an int as the number as which we store the temperature.
So here in double it should be int.
Now this method needs to return a double okay.
So it needs to return whatever the temperature is.
Now I'm going to however calculate the temperatures average before returning it.
So this is just so that I get rid of the error which is uh consistently there.
So first of all I'm going to get a variable that will be the sum.
And the sum will hold all the temperatures.
Now let's go over all the temperatures that we have inside of this array.
So you can see we can pass an array as a argument.
Or we can use an array as a parameter here.
Now we're going to use a for loop.
So for int I I less than temperature dot length.
And this is one way you could use a for each loop here as well.
Either way is fine I plus plus and then here I'm going to say sum is going to be plus equal temperature
at position I.
So this way we go through the entire loop.
All right.
Alternatively we could have made a for each int I in temperature and sum plus equal I okay.
So this would also be an option here right.
Either way it's fine.
Use whichever you prefer.
So I'm going to comment this out.
And now we can take the sum and we can divide it by the length of the array.
So however many items we have so we can say average.
So double average is going to be some divided by temperature dot length.
So the IDE is doing my job here.
It's crazy.
So yeah basically we're taking whatever the sum value is because now we filled it up with all the different
temperatures combined.
And then we divide it by the amount of items that we have inside of temperature dot length.
So don't forget temperature dot length is the amount of items inside of temperature.
Good.
And now we can return that average.
So this method now will calculate the average of the values of an integer array.
And now let's print it out.
Let's actually get the average temperature.
So what I'm going to do is I'm going to show you how we can use that inside of a string.
So we can use methods directly inside of a string.
So this calculate average method, we can use it inside of the main method by just calling it.
So we could just say average temp is going to be a calculate average for our temperature.
So for this integer array however we can also use this directly in here by using curly brackets.
So these curly brackets just allow us to run some code inside of it and use the result in the string.
So in this case this method will return a double and the string will just display the double.
So we can just say here average temperature is and then calculate average temperature.
So we don't need this variable here.
This was just for you to show you that you can use this method that we created outside of the main method.
Right.
You can now use that method inside of the main method for it to be executed.
Now you however also see how you can use it inside of a string using the dollar sign at the beginning
of the string.
Here you can see that dollar sign, and that is string manipulation as we have seen a few chapters.
Before.
I just wanted to show you this once again.
Okay, so now let's run this.
And therefore I need to make sure that I have a console dot read key here in order to keep my console
awake.
Let's run this again.
Let's enter ten days and it says the average temperature is 12.
Okay, let me run this again just to see that it actually gives me different values.
Now the average temperature was 15.
Now let's iterate it for ten times again.
Now it's 14.
So we are getting different values that may actually add a breakpoint down here.
So now let's run this and see what the average temperature will be.
Or the temperatures will be for ten temperatures.
So we have this temp variable.
And let's hover over the variable and see what the ten values are.
So you see zero 32 for 28 and so forth.
So a bunch of values.
And now the average of that is 19.
Now you can run the test and add all of these numbers up.
And you should see that it is the result that we are getting.
Now when running you might notice that we don't get a double as a result.
Now if you look closely you see that we do get a whole number as a result.
So we get an INT instead of a double as a result.
There's a very quick fix if we want to fix it.
So if you want to actually like show the decimal points here as well, what you can do is you can instead
of making the sum a int, you can make it a double.
And then you will get the average as a double result.
So you see here 17.2 as the average temperature.
That is because of this line here where when we divide an integer by an integer we are going to get
an integer result.
So even though we store it inside of a double, we're still going to get an integer result.
That is something that I want to show you in the next video real quickly, because it is something to
consider.