The Solution4:07 with Jay McGavren
Now that you've coded your solution to the practice problem, I'll show you how I did it.
Here's my code:
a = 12 b = 7 c = 5 d = 10 puts (a + b + c + d) / 4.0
Try writing programs that calculate the perimeter or area of a rectangle or triangle.
Your goal was to build a simple Ruby program, 0:00 that calculates the average of some numbers. 0:02 Here's my solution. 0:04 It's okay if yours is slightly different, but if you see something interesting in my 0:05 code, you should consider borrowing it to improve your own program. 0:09 Okay, so to calculate an average, we need to add the four values in the A, B, C and 0:13 D variables together, and then divide by 4 that's the total number of values. 0:18 Then we need to print the result out. 0:22 So to print it, we're gonna need to call the puts method. 0:24 Now the first thing you might have tried was to simply add a + b + c + d together. 0:27 And then divide the result by 4. 0:35 Unfortunately, that won't work because 0:37 what it will do is it will add a + b + c + d / 4. 0:41 It'll divide d by 4 first and then add it to a, b, and c, 0:47 which is not what you want. 0:50 Instead, we need to take order of operations into account. 0:52 Ruby allows you to put math operations within parenthesis so 0:58 that they take place first. 1:03 So by putting a + b + c + d in parentheses here. 1:05 And then that will perform all those additional operations first. 1:10 Then take the results of that and divide that by 4. 1:14 That's what we're going to want for calculating the average. 1:17 Let's try running this again. 1:19 Oops, didn't save my work first. 1:23 One second. 1:25 Okay, there we go. 1:28 We're closer to what we want, we got the number 8. 1:30 However, as I mentioned here in the comments, 1:32 if you get 8 its because we're dividing by a fixnum. 1:35 And unfortunately, if you divide a fixnum by a fixnum, 1:39 ruby will truncate any fractional value from the number to get fixnum result. 1:43 So what we need is we need to turn one of these numbers into a float so 1:49 that we get a float result. 1:53 To convert it to a float that's as simple as adding a decimal on to the end. 1:55 So we'll take this 4 and turn it into a 4.0. 1:58 That way it doesn't matter even after the results of this addition 2:01 here is a fixed number the results of this the vision will still be a float. 2:06 And that way our number won't get truncated. 2:11 So let's save this, run it again. 2:13 Okay, and now we're getting the result that we expect, 8.5. 2:17 Now for the extra credit. 2:23 So we said that we could prompt the user to enter values for these four 2:25 variables by calling gets and that we would get a string value from gets. 2:31 So in order to convert that to a numeric value, 2:36 we'll need to call to_f on the value in gets. 2:40 So let's make that change up here real quick. 2:44 We'll say, First we'll need to print a prompt for the users. 2:47 So we'll say puts "Please enter four numbers". 2:53 And now we'll say gets.to_f and 3:01 that will convert the string that we get back from gets to 3:05 a float number which it will then be stored in the a variable. 3:09 And then we'll just do the same for these four remaining lines so 3:15 I'll just copy and paste. 3:18 Okay there we go we should now have user entry stored in those four variables and 3:22 then will calculate an average of those user entries. 3:28 So let's try running our program again please and her phone numbers and 3:32 we'll just do the same ones we did previously 12, 7, 5, and 10. 3:37 And we get a result of of 8.5. 3:41 Now lets try averaging lets say, 2, 2, 3:46 4 and 5, and we get an average of 3.25. 3:50 So not only is our average working, 3:55 we seem to be able to accept input from the user as well. 3:57 I hope you gotten some good review practice, see the teachers notes for 4:01 some other experiment you might trying. 4:04 Have fun. 4:06
You need to sign up for Treehouse in order to download course files.Sign up