1 00:00:00,360 --> 00:00:02,412 Your goal was to build a simple Ruby program, 2 00:00:02,412 --> 00:00:04,525 that calculates the average of some numbers. 3 00:00:04,525 --> 00:00:05,950 Here's my solution. 4 00:00:05,950 --> 00:00:09,280 It's okay if yours is slightly different, but if you see something interesting in my 5 00:00:09,280 --> 00:00:12,100 code, you should consider borrowing it to improve your own program. 6 00:00:13,480 --> 00:00:18,080 Okay, so to calculate an average, we need to add the four values in the A, B, C and 7 00:00:18,080 --> 00:00:22,657 D variables together, and then divide by 4 that's the total number of values. 8 00:00:22,657 --> 00:00:24,620 Then we need to print the result out. 9 00:00:24,620 --> 00:00:27,310 So to print it, we're gonna need to call the puts method. 10 00:00:27,310 --> 00:00:35,010 Now the first thing you might have tried was to simply add a + b + c + d together. 11 00:00:35,010 --> 00:00:37,080 And then divide the result by 4. 12 00:00:37,080 --> 00:00:41,977 Unfortunately, that won't work because 13 00:00:41,977 --> 00:00:47,286 what it will do is it will add a + b + c + d / 4. 14 00:00:47,286 --> 00:00:50,482 It'll divide d by 4 first and then add it to a, b, and c, 15 00:00:50,482 --> 00:00:52,820 which is not what you want. 16 00:00:52,820 --> 00:00:56,721 Instead, we need to take order of operations into account. 17 00:00:58,721 --> 00:01:03,199 Ruby allows you to put math operations within parenthesis so 18 00:01:03,199 --> 00:01:05,500 that they take place first. 19 00:01:05,500 --> 00:01:10,350 So by putting a + b + c + d in parentheses here. 20 00:01:10,350 --> 00:01:14,105 And then that will perform all those additional operations first. 21 00:01:14,105 --> 00:01:17,500 Then take the results of that and divide that by 4. 22 00:01:17,500 --> 00:01:19,827 That's what we're going to want for calculating the average. 23 00:01:19,827 --> 00:01:21,339 Let's try running this again. 24 00:01:23,743 --> 00:01:25,750 Oops, didn't save my work first. 25 00:01:25,750 --> 00:01:26,284 One second. 26 00:01:28,969 --> 00:01:30,360 Okay, there we go. 27 00:01:30,360 --> 00:01:32,880 We're closer to what we want, we got the number 8. 28 00:01:32,880 --> 00:01:35,240 However, as I mentioned here in the comments, 29 00:01:35,240 --> 00:01:39,010 if you get 8 its because we're dividing by a fixnum. 30 00:01:39,010 --> 00:01:43,900 And unfortunately, if you divide a fixnum by a fixnum, 31 00:01:43,900 --> 00:01:49,950 ruby will truncate any fractional value from the number to get fixnum result. 32 00:01:49,950 --> 00:01:53,590 So what we need is we need to turn one of these numbers into a float so 33 00:01:53,590 --> 00:01:55,080 that we get a float result. 34 00:01:55,080 --> 00:01:58,692 To convert it to a float that's as simple as adding a decimal on to the end. 35 00:01:58,692 --> 00:02:01,940 So we'll take this 4 and turn it into a 4.0. 36 00:02:01,940 --> 00:02:06,330 That way it doesn't matter even after the results of this addition 37 00:02:06,330 --> 00:02:11,480 here is a fixed number the results of this the vision will still be a float. 38 00:02:11,480 --> 00:02:13,588 And that way our number won't get truncated. 39 00:02:13,588 --> 00:02:15,937 So let's save this, run it again. 40 00:02:17,760 --> 00:02:21,475 Okay, and now we're getting the result that we expect, 8.5. 41 00:02:23,014 --> 00:02:24,320 Now for the extra credit. 42 00:02:25,610 --> 00:02:31,091 So we said that we could prompt the user to enter values for these four 43 00:02:31,091 --> 00:02:36,976 variables by calling gets and that we would get a string value from gets. 44 00:02:36,976 --> 00:02:40,893 So in order to convert that to a numeric value, 45 00:02:40,893 --> 00:02:44,840 we'll need to call to_f on the value in gets. 46 00:02:44,840 --> 00:02:47,458 So let's make that change up here real quick. 47 00:02:47,458 --> 00:02:53,580 We'll say, First we'll need to print a prompt for the users. 48 00:02:53,580 --> 00:03:00,265 So we'll say puts "Please enter four numbers". 49 00:03:01,965 --> 00:03:05,020 And now we'll say gets.to_f and 50 00:03:05,020 --> 00:03:09,967 that will convert the string that we get back from gets to 51 00:03:09,967 --> 00:03:15,349 a float number which it will then be stored in the a variable. 52 00:03:15,349 --> 00:03:18,103 And then we'll just do the same for these four remaining lines so 53 00:03:18,103 --> 00:03:19,282 I'll just copy and paste. 54 00:03:22,493 --> 00:03:28,510 Okay there we go we should now have user entry stored in those four variables and 55 00:03:28,510 --> 00:03:32,740 then will calculate an average of those user entries. 56 00:03:32,740 --> 00:03:37,007 So let's try running our program again please and her phone numbers and 57 00:03:37,007 --> 00:03:41,365 we'll just do the same ones we did previously 12, 7, 5, and 10. 58 00:03:41,365 --> 00:03:46,140 And we get a result of of 8.5. 59 00:03:46,140 --> 00:03:50,785 Now lets try averaging lets say, 2, 2, 60 00:03:50,785 --> 00:03:55,699 4 and 5, and we get an average of 3.25. 61 00:03:55,699 --> 00:03:57,859 So not only is our average working, 62 00:03:57,859 --> 00:04:01,178 we seem to be able to accept input from the user as well. 63 00:04:01,178 --> 00:04:04,238 I hope you gotten some good review practice, see the teachers notes for 64 00:04:04,238 --> 00:04:06,260 some other experiment you might trying. 65 00:04:06,260 --> 00:04:06,760 Have fun.