Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Now that you've coded your solution to the practice problem, I'll show you how I did it.
One Solution
Here's my code:
a = 12
b = 7
c = 5
d = 10
puts (a + b + c + d) / 4.0
Additional Experimentation
Try writing programs that calculate the perimeter or area of a rectangle or triangle.

0:00
Your goal was to build a simple Ruby program,

0:02
that calculates the average of some numbers.

0:04
Here's my solution.

0:05
It's okay if yours is slightly different, but if you see something interesting in my

0:09
code, you should consider borrowing it to improve your own program.

0:13
Okay, so to calculate an average, we need to add the four values in the A, B, C and

0:18
D variables together, and then divide by 4 that's the total number of values.

0:22
Then we need to print the result out.

0:24
So to print it, we're gonna need to call the puts method.

0:27
Now the first thing you might have tried was to simply add a + b + c + d together.

0:35
And then divide the result by 4.

0:37
Unfortunately, that won't work because

0:41
what it will do is it will add a + b + c + d / 4.

0:47
It'll divide d by 4 first and then add it to a, b, and c,

0:50
which is not what you want.

0:52
Instead, we need to take order of operations into account.

0:58
Ruby allows you to put math operations within parenthesis so

1:03
that they take place first.

1:05
So by putting a + b + c + d in parentheses here.

1:10
And then that will perform all those additional operations first.

1:14
Then take the results of that and divide that by 4.

1:17
That's what we're going to want for calculating the average.

1:19
Let's try running this again.

1:23
Oops, didn't save my work first.

1:25
One second.

1:28
Okay, there we go.

1:30
We're closer to what we want, we got the number 8.

1:32
However, as I mentioned here in the comments,

1:35
if you get 8 its because we're dividing by a fixnum.

1:39
And unfortunately, if you divide a fixnum by a fixnum,

1:43
ruby will truncate any fractional value from the number to get fixnum result.

1:49
So what we need is we need to turn one of these numbers into a float so

1:53
that we get a float result.

1:55
To convert it to a float that's as simple as adding a decimal on to the end.

1:58
So we'll take this 4 and turn it into a 4.0.

2:01
That way it doesn't matter even after the results of this addition

2:06
here is a fixed number the results of this the vision will still be a float.

2:11
And that way our number won't get truncated.

2:13
So let's save this, run it again.

2:17
Okay, and now we're getting the result that we expect, 8.5.

2:23
Now for the extra credit.

2:25
So we said that we could prompt the user to enter values for these four

2:31
variables by calling gets and that we would get a string value from gets.

2:36
So in order to convert that to a numeric value,

2:40
we'll need to call to_f on the value in gets.

2:44
So let's make that change up here real quick.

2:47
We'll say, First we'll need to print a prompt for the users.

2:53
So we'll say puts "Please enter four numbers".

3:01
And now we'll say gets.to_f and

3:05
that will convert the string that we get back from gets to

3:09
a float number which it will then be stored in the a variable.

3:15
And then we'll just do the same for these four remaining lines so

3:18
I'll just copy and paste.

3:22
Okay there we go we should now have user entry stored in those four variables and

3:28
then will calculate an average of those user entries.

3:32
So let's try running our program again please and her phone numbers and

3:37
we'll just do the same ones we did previously 12, 7, 5, and 10.

3:41
And we get a result of of 8.5.

3:46
Now lets try averaging lets say, 2, 2,

3:50
4 and 5, and we get an average of 3.25.

3:55
So not only is our average working,

3:57
we seem to be able to accept input from the user as well.

4:01
I hope you gotten some good review practice, see the teachers notes for

4:04
some other experiment you might trying.

4:06
Have fun.
You need to sign up for Treehouse in order to download course files.
Sign up