1 00:00:00,000 --> 00:00:02,360 >> DAVID J. MALAN: As you probably know, temperatures are measured using 2 00:00:02,360 --> 00:00:04,360 different scales in different parts of the world. 3 00:00:04,360 --> 00:00:09,370 For instance, 212 degrees Fahrenheit is 100 degrees Celsius and 32 degrees 4 00:00:09,370 --> 00:00:11,810 Fahrenheit is 0 degrees Celsius. 5 00:00:11,810 --> 00:00:14,230 Wouldn't it be nice if we had a computer program that allowed us to 6 00:00:14,230 --> 00:00:16,950 convert from, say, Fahrenheit to Celsius? 7 00:00:16,950 --> 00:00:18,510 >> Well, we can write that program. 8 00:00:18,510 --> 00:00:24,350 Let's first start with include cs50.h, so that we can use a function called 9 00:00:24,350 --> 00:00:28,610 get float, which, like get int, gets a number from the user, but this time a 10 00:00:28,610 --> 00:00:31,440 floating point number-- one with a decimal point. 11 00:00:31,440 --> 00:00:34,840 >> Let's also include standard io.h so that we have access to 12 00:00:34,840 --> 00:00:36,230 functions like print f. 13 00:00:36,230 --> 00:00:40,000 And let's also declare main in the usual way. 14 00:00:40,000 --> 00:00:42,675 >> Let's next prompt the user for a temperature in Fahrenheit. 15 00:00:42,675 --> 00:00:46,050 16 00:00:46,050 --> 00:00:49,220 Let's now actually get that temperature from the user, first by 17 00:00:49,220 --> 00:00:52,910 declaring a variable called f of type float. 18 00:00:52,910 --> 00:00:56,750 A float, again, meaning a variable that stores a floating point value, 19 00:00:56,750 --> 00:00:58,200 one with a decimal point. 20 00:00:58,200 --> 00:01:01,780 >> Let's assign it the return value of get float. 21 00:01:01,780 --> 00:01:06,080 And then let's do a bit of arithmetic on it, first declaring another float 22 00:01:06,080 --> 00:01:11,690 called C for Celsius and store in C the result of some common arithmetic. 23 00:01:11,690 --> 00:01:19,060 5.0 divided by 9.0 times F minus 32.0. 24 00:01:19,060 --> 00:01:23,440 >> Let's now print the result of this computation. 25 00:01:23,440 --> 00:01:24,730 percent f-- 26 00:01:24,730 --> 00:01:27,890 signifying a placeholder for floating point value-- 27 00:01:27,890 --> 00:01:31,680 comma C to print out the temperature in Celsius. 28 00:01:31,680 --> 00:01:33,000 Let's save my final. 29 00:01:33,000 --> 00:01:38,220 Compile with F to C. Run the program with dot slash F to C. 30 00:01:38,220 --> 00:01:39,940 >> And let's try those common temperatures. 31 00:01:39,940 --> 00:01:45,820 212 degrees in Fahrenheit is 100 degrees Celsius. 32 00:01:45,820 --> 00:01:50,090 32 degrees in Fahrenheit is 0 degrees in Celsius. 33 00:01:50,090 --> 00:01:54,240 >> Now lets refine this program a little bit by not printing quite as many 0's 34 00:01:54,240 --> 00:01:55,820 after that decimal place. 35 00:01:55,820 --> 00:01:57,940 To do this, I'm going to go back to line 11. 36 00:01:57,940 --> 00:02:03,430 And rather than just specify percent f, I'm going to instead specify, say, 37 00:02:03,430 --> 00:02:08,800 0.1 f, informing print f that I only want to print a floating point value 38 00:02:08,800 --> 00:02:11,750 to one value after the decimal place. 39 00:02:11,750 --> 00:02:13,630 >> Let's resave my program. 40 00:02:13,630 --> 00:02:19,680 Recompile it with make F to C. Then rerun it with dot slash F to C. And 41 00:02:19,680 --> 00:02:24,910 let's retry with, say, 212, which gives me 100.0. 42 00:02:24,910 --> 00:02:28,360 >> Now it's worth noting that I did something very deliberately in line 9. 43 00:02:28,360 --> 00:02:35,830 Notice how I wrote 5 is 5.0, 9 as 9.0, and even 32 as 32.0. 44 00:02:35,830 --> 00:02:39,000 Well, the first two of those values were very deliberately chosen to be 45 00:02:39,000 --> 00:02:42,200 floating point values, not just because of consistency with the rest 46 00:02:42,200 --> 00:02:42,940 of my program-- 47 00:02:42,940 --> 00:02:45,110 which clearly involves floating point values-- 48 00:02:45,110 --> 00:02:50,210 but because it turns out that C, If you divide an int by another int, the 49 00:02:50,210 --> 00:02:54,350 resulting answer you're going to get is itself an int, even if that means 50 00:02:54,350 --> 00:02:57,450 having to throw away everything after the decimal point. 51 00:02:57,450 --> 00:03:04,990 >> In other words, if I change this 5.0 to 5 or this 9.0 to 9 and then resave 52 00:03:04,990 --> 00:03:10,550 my program, recompile with make F to C, and then re-run it with dot slash F 53 00:03:10,550 --> 00:03:15,310 to C and type in an input of like 212, notice that the answer I'm going to 54 00:03:15,310 --> 00:03:17,860 get this time is actually quite wrong. 55 00:03:17,860 --> 00:03:23,570 0.0 is not the correct degree in Celsius as 212 Fahrenheit. 56 00:03:23,570 --> 00:03:24,500 >> Well, what's going on? 57 00:03:24,500 --> 00:03:29,410 Well, in line 9, because 5 is now an integer and because 9 is now an 58 00:03:29,410 --> 00:03:34,810 integer, the result mathematically should be 0.5555 and so on. 59 00:03:34,810 --> 00:03:39,120 But because the result, according to C's rules, has to be an int, that 60 00:03:39,120 --> 00:03:44,020 0.5555 gets thrown away, leaving us with just 0. 61 00:03:44,020 --> 00:03:48,600 >> So in the end, I end up multiplying quite accidentally 0 times f minus 62 00:03:48,600 --> 00:03:52,830 32.0, which is no matter what always going to give me 0. 63 00:03:52,830 --> 00:03:56,930 So do keep in mind, any time using floating point values in proximity of 64 00:03:56,930 --> 00:03:59,860 ints, you might not necessarily get the answer so you expect. 65 00:03:59,860 --> 00:04:04,220 And so take care to use, as I did in the first case, floating point values 66 00:04:04,220 --> 00:04:06,530 throughout to avoid any such issues. 67 00:04:06,530 --> 00:04:08,267