1 00:00:05,500 --> 00:00:09,600 In this video, we'll go over c++ arithmetic operators. 2 00:00:10,000 --> 00:00:13,800 These are exactly the ones you expect. The + symbol means addition. 3 00:00:14,160 --> 00:00:18,460 the - symbol mean subtraction, the * is multiplication, 4 00:00:18,460 --> 00:00:20,260 the / is division, 5 00:00:20,260 --> 00:00:24,560 and the % symbol is the modulo operator or the remainder of division. 6 00:00:25,060 --> 00:00:28,660 You may not be familiar with this operator, but that's okay, we'll cover it toward the end of the video. 7 00:00:29,320 --> 00:00:33,320 The +, -, *, and / operators are overloaded. 8 00:00:33,320 --> 00:00:35,920 What that means is that work with different types. 9 00:00:35,920 --> 00:00:39,720 So we can add two integers, we can add two floats, we can add two doubles 10 00:00:39,720 --> 00:00:41,520 by simply using the plus symbol. 11 00:00:41,850 --> 00:00:44,850 The same is true for, subtraction, multiplication, division. 12 00:00:45,250 --> 00:00:47,910 The modulo operator only works with integers. 13 00:00:47,910 --> 00:00:49,910 And again, I'll show you an example of that in a little bit. 14 00:00:50,310 --> 00:00:52,310 So let's try a few examples here. 15 00:00:52,810 --> 00:00:55,210 In the code done here, you can see that I've declared 16 00:00:55,210 --> 00:00:57,710 two integers: num1 and num2 17 00:00:57,710 --> 00:01:00,610 and I've assigned 200 and 100 to them, respectively. 18 00:01:00,610 --> 00:01:04,489 Again, these are integers. You could do this with doubles or floats or anything you'd like 19 00:01:04,489 --> 00:01:05,890 but I'm just going to use integers here. 20 00:01:06,390 --> 00:01:10,690 You can see that this first line, this first output statement, right here. 21 00:01:11,050 --> 00:01:14,410 Let's walk through that really quickly so we're clear as to what's going on here. 22 00:01:14,910 --> 00:01:17,410 Remember, we've got expressions here. 23 00:01:17,410 --> 00:01:20,010 So num1 is an expression, num2 is an expression 24 00:01:20,010 --> 00:01:22,310 and that addition expression 25 00:01:22,810 --> 00:01:25,810 is evaluated to the sum of num1 and num2. 26 00:01:25,810 --> 00:01:29,810 So in this this case, num1 is 200 so this will be replaced with 200. 27 00:01:31,310 --> 00:01:35,110 Num2 is 100, and this will be replaced with 100. 28 00:01:35,610 --> 00:01:39,610 And then num1 + num2 will be added together, that's why the plus is there 29 00:01:39,610 --> 00:01:41,110 and this will give us 300. 30 00:01:41,910 --> 00:01:44,510 Okay. Now these guys are inside 31 00:01:44,510 --> 00:01:47,630 quotes so there are string literals and that would be printed out as is. 32 00:01:48,130 --> 00:01:52,430 So what we expect here is 200 + 100 equals 300 and of course, we've got 33 00:01:52,790 --> 00:01:55,090 the new line or the end line at the end. 34 00:01:55,690 --> 00:01:57,190 All right. So let's run this 35 00:01:57,990 --> 00:02:00,290 and see if we get what we expect. 36 00:02:01,190 --> 00:02:05,290 And exactly that's what happens. 200 plus 100 equals 300. 37 00:02:05,290 --> 00:02:09,490 Okay. So let me comment that line out and 38 00:02:10,090 --> 00:02:13,990 what we could do is rather than do that calculation 39 00:02:13,990 --> 00:02:15,290 right in line right here, 40 00:02:15,790 --> 00:02:19,290 we could simply do the calculation and assign it to a variable. 41 00:02:19,490 --> 00:02:22,790 In this case, let's create a variable called result. 42 00:02:25,090 --> 00:02:28,390 And we'll initialize it to 0. And all were saying is 43 00:02:28,640 --> 00:02:32,840 add num1 and num2 that'll give us 300 and assign that result 44 00:02:33,040 --> 00:02:36,400 to the result variable. Okay. So we're going to add 45 00:02:36,600 --> 00:02:41,100 200 + 100, that gives us 300, and we're storing 300 into result. 46 00:02:41,500 --> 00:02:46,100 And then rather than do the calculation here, as we did, we're just going to print out result. 47 00:02:46,500 --> 00:02:49,940 It's as easy as that. So when I run this now, I should have the same 48 00:02:49,940 --> 00:02:51,140 exact output, 49 00:02:51,140 --> 00:02:53,140 200 plus 100 equals 300. 50 00:02:54,140 --> 00:02:58,440 So now let me comment out some of these, and we'll walk through these real quick. 51 00:02:59,140 --> 00:03:00,440 And I'll do it right up to there. 52 00:03:01,540 --> 00:03:04,040 So this next one here on line 29, 53 00:03:04,040 --> 00:03:06,540 I'm simply subtracting num1 54 00:03:06,540 --> 00:03:09,340 - num2, storing the difference in result 55 00:03:09,340 --> 00:03:10,940 and then I'm out putting result. 56 00:03:10,940 --> 00:03:14,540 So here, we would expect 200 - 100, we'd expect 100. 57 00:03:15,140 --> 00:03:17,740 On line 32, we're multiplying the 2. 58 00:03:18,040 --> 00:03:20,340 This is different from algebra. 59 00:03:20,340 --> 00:03:23,700 Do you have to have that multiplication symbol, that asterisk. 60 00:03:23,700 --> 00:03:28,000 You can't just say num1, num2 or something like that like you were doing algebra, 61 00:03:28,000 --> 00:03:30,360 you just put the two things together. You can't use 62 00:03:30,360 --> 00:03:33,360 a dot like you could in algebras as well. It's got to be 63 00:03:33,860 --> 00:03:36,660 the multiplication operator, which is the asterisk. 64 00:03:36,660 --> 00:03:40,960 So we're going to multiply num1 and num2 that will give us 20,000, which we store in result, 65 00:03:41,260 --> 00:03:42,620 and we're displaying that. 66 00:03:42,620 --> 00:03:48,210 And then the third one would be result is num1 divided by num2, 67 00:03:48,210 --> 00:03:49,910 that's on line 35. 68 00:03:49,910 --> 00:03:53,570 And we're going to store that quotient into result. 69 00:03:54,870 --> 00:03:57,570 There we would expect 200/100, it gives us 2. 70 00:03:57,570 --> 00:04:00,670 Okay. So let's run what we've got right here and 71 00:04:01,270 --> 00:04:02,630 see what our results are. 72 00:04:02,630 --> 00:04:05,930 So we've got 200 + 100, like we did before is 300. 73 00:04:06,230 --> 00:04:10,430 200 - 100 is a 100. 200 * 100 is 20000. 74 00:04:10,630 --> 00:04:12,630 And 200/100 is 2. 75 00:04:13,030 --> 00:04:16,930 Okay. So this is exactly like you would expect. It's really, really simple. 76 00:04:16,930 --> 00:04:19,290 Math works just like you expected to. 77 00:04:19,790 --> 00:04:23,490 Now something you may not expect is this result right here on line 38. 78 00:04:23,490 --> 00:04:24,990 I'm going to uncomment this. 79 00:04:26,690 --> 00:04:28,690 And in this case, what I'm doing is 80 00:04:28,690 --> 00:04:31,190 I'm dividing num2/num1. 81 00:04:31,690 --> 00:04:34,890 Okay. Remember, num2 is 100 and num1 is 200. 82 00:04:34,890 --> 00:04:38,690 So what's happening here is I'm dividing 100 83 00:04:38,690 --> 00:04:42,460 /200. You would expect 0.5. 84 00:04:42,460 --> 00:04:45,660 That's not what you get. We're doing integer division. 85 00:04:45,660 --> 00:04:49,260 Okay. So there are no digits after the decimal point. 86 00:04:49,260 --> 00:04:52,620 We're working with whole numbers only. So in this case, 87 00:04:52,620 --> 00:04:54,920 what happens is 100 88 00:04:55,720 --> 00:04:57,720 gets divided by 200. 89 00:04:59,120 --> 00:05:02,820 And the results if this if these were doubles would be 0.5. 90 00:05:03,220 --> 00:05:06,420 But we're working with integers. So this piece is just chopped right off 91 00:05:06,420 --> 00:05:07,720 so we end up with a 0. 92 00:05:08,120 --> 00:05:09,920 Okay. So let's run this. 93 00:05:13,120 --> 00:05:16,320 And you can see here 100/200 is 0. 94 00:05:16,320 --> 00:05:20,320 You've got to be really careful when you're doing division with whole numbers or integers 95 00:05:20,320 --> 00:05:21,980 because this is the behavior. 96 00:05:21,980 --> 00:05:25,230 If you need to 0.5, you want to be really, really precise, 97 00:05:25,230 --> 00:05:27,730 then you need to use doubles, not integers. 98 00:05:27,730 --> 00:05:29,930 And I'll show you an example using doubles in a second. 99 00:05:30,920 --> 00:05:34,920 Now let's look at the modular operator or the remainder of operator. 100 00:05:34,920 --> 00:05:37,920 In this case, right here, 101 00:05:38,420 --> 00:05:41,020 I'm saying num1 mod num2. 102 00:05:41,620 --> 00:05:44,390 Again, what's num1, it’s 200. 103 00:05:44,390 --> 00:05:46,190 What's num2, 100. 104 00:05:46,190 --> 00:05:50,190 So 200 divided by, let me write that out real clear 105 00:05:50,190 --> 00:05:53,490 so it's not confusing it all. So we're going to say 200 106 00:05:55,790 --> 00:05:58,150 divided by 100. 107 00:05:58,950 --> 00:06:02,940 The result of this is 2 with a remainder of 0. 108 00:06:03,340 --> 00:06:05,640 That's the modular operator. 109 00:06:05,640 --> 00:06:09,740 It's only interested in the remainder. So in this case, it’ll print out a 0. 110 00:06:10,240 --> 00:06:14,240 The next line, I’ll uncomment that out in a second. But the next line we're going to say 111 00:06:14,440 --> 00:06:17,240 right here, num1 112 00:06:17,940 --> 00:06:20,540 let me clear that draw that line a little nicer there. 113 00:06:20,540 --> 00:06:24,340 Here we're saying num1 mod num2. So we're saying 10 114 00:06:24,740 --> 00:06:25,740 mod 3. 115 00:06:25,740 --> 00:06:28,100 Remember, this only works with integers. 116 00:06:28,100 --> 00:06:30,400 So the results of the division would 117 00:06:30,400 --> 00:06:32,600 be 3 with a remainder of 1, 118 00:06:32,600 --> 00:06:35,500 number 3 x 3 is 9 and you get the 1 remainder. 119 00:06:35,500 --> 00:06:39,300 That's the result of the modular operator. It's a pretty handy operator. 120 00:06:39,700 --> 00:06:43,400 So let me clear this and I'll uncomment these few lines of code right here. 121 00:06:45,000 --> 00:06:46,000 And we will run this. 122 00:06:49,800 --> 00:06:51,600 And let's take a look at the result. 123 00:06:51,600 --> 00:06:54,100 So here we go. 200 mod 100 124 00:06:54,100 --> 00:06:58,200 is 0, right that's the remainder because we get a 2 with a remainder of 0. 125 00:06:58,500 --> 00:07:00,700 And then this case 10 mod 3 126 00:07:00,700 --> 00:07:05,200 is 1 because we get a 3 with a remainder of 1 when we divided by 3. 127 00:07:05,500 --> 00:07:08,200 This could be a really, really handy operator to use. 128 00:07:08,200 --> 00:07:12,300 And in the challenge exercise, you're going to have to use it to solve the problem. 129 00:07:12,900 --> 00:07:16,000 A couple more things. Just like you can with 130 00:07:16,000 --> 00:07:18,800 any programming language and doing any kind of math, we can add and 131 00:07:18,800 --> 00:07:22,100 divide and subtract and we can do everything in line. 132 00:07:22,700 --> 00:07:24,300 It's important to understand 133 00:07:24,300 --> 00:07:27,500 that the precedents or the order in which this stuff 134 00:07:27,500 --> 00:07:30,600 happens is the same precedents you probably learned in grade school. 135 00:07:31,100 --> 00:07:33,400 Here in the US, we learned PEMDAS. 136 00:07:35,100 --> 00:07:37,800 That was the acronym we used which meant parentheses 137 00:07:37,800 --> 00:07:41,000 first then exponents then multiplication and division 138 00:07:41,000 --> 00:07:42,900 and then addition and subtraction. 139 00:07:42,900 --> 00:07:47,260 You may have learned something else if you're from a different country, but that's what we learned here. 140 00:07:47,260 --> 00:07:49,060 So what this means is that 141 00:07:49,060 --> 00:07:52,960 multiplication and division happened before addition and subtraction, right. 142 00:07:52,960 --> 00:07:55,460 So in this case what’s going to happen is num1 143 00:07:55,760 --> 00:07:58,860 is going to be multiplied by 10 and then 144 00:07:58,860 --> 00:08:00,860 we're going to add num2 to that result, 145 00:08:01,660 --> 00:08:04,260 right. If you want something else, 146 00:08:04,260 --> 00:08:07,260 if you want to add the 10 in the num2 first, 147 00:08:07,260 --> 00:08:10,860 then you need to use parentheses around there like that. 148 00:08:11,760 --> 00:08:15,660 It's as simple as that and again it's something that we're all used to by now. 149 00:08:16,260 --> 00:08:20,960 Let me clear this and then I'll do this last example with the division. 150 00:08:22,560 --> 00:08:24,160 I'll comment these two lines out. 151 00:08:26,360 --> 00:08:30,460 So in this case, I'm outputting 5/10. 152 00:08:31,060 --> 00:08:33,360 Remember, this is using integer division. 153 00:08:33,360 --> 00:08:37,020 So 5/10 is 0, not 0.5. 154 00:08:37,919 --> 00:08:38,820 In this case, 155 00:08:38,820 --> 00:08:42,820 5.0/10.0 is 0.5 156 00:08:42,820 --> 00:08:44,420 because we're using doubles here. 157 00:08:45,220 --> 00:08:47,820 In a couple of videos from now, we're going to do a video 158 00:08:47,820 --> 00:08:51,720 where we're mixing types, we will be multiplying and dividing integers and doubles all together 159 00:08:51,720 --> 00:08:54,820 on the same expression. In that case, 160 00:08:54,820 --> 00:08:58,620 we can use casts to tell the compiler,hey, you know what, 161 00:08:58,620 --> 00:09:02,920 I really want this to be a double instead of an integer, even though it looks like an integer there. 162 00:09:03,220 --> 00:09:07,320 And so we'll get to that in a minute. And let me run this thing one more time. 163 00:09:08,920 --> 00:09:10,720 And you can see the last two 164 00:09:11,520 --> 00:09:14,820 results right down here 5/10 is 0 165 00:09:14,820 --> 00:09:18,420 and 5.0/10.0 is 0.5. 166 00:09:20,120 --> 00:09:24,220 Okay. So now let's solve a real problem. Let's convert 167 00:09:24,220 --> 00:09:25,920 Euros to U.S. dollars 168 00:09:26,280 --> 00:09:28,480 and I've created a 169 00:09:28,880 --> 00:09:31,180 project in the section 8 workspace, 170 00:09:31,180 --> 00:09:32,080 called Euros. 171 00:09:32,080 --> 00:09:35,580 This project that I worked on right here is called arithmetic operator 172 00:09:35,580 --> 00:09:37,180 so you can refer to that if you like. 173 00:09:37,180 --> 00:09:40,880 But I've created a project called Euros and that's when we'll get to next. 174 00:09:41,680 --> 00:09:46,280 Okay. So I've opened up the main CPP file in the Euros project. 175 00:09:46,780 --> 00:09:48,780 Right now there's really not much in here. 176 00:09:48,780 --> 00:09:52,480 I'm just printing a new line at the end to separate that little time message 177 00:09:52,480 --> 00:09:54,080 that is displayed at the end 178 00:09:54,330 --> 00:09:57,630 and there's no real code here. We're going to write the code from scratch. 179 00:09:57,630 --> 00:09:59,530 So what we want to do is we want to convert 180 00:09:59,530 --> 00:10:03,430 Euros to US dollar. So obviously we need some sort of conversion factor. 181 00:10:03,830 --> 00:10:06,430 I just went on Google now and I saw that 182 00:10:06,430 --> 00:10:08,730 you have 1.19 183 00:10:09,090 --> 00:10:14,080 dollars per Euro. Okay. So 1 euro is 1.19 dollar. 184 00:10:14,280 --> 00:10:16,940 So we need to use that value somewhere. 185 00:10:16,940 --> 00:10:20,240 Now that's going to be a constant. So let's just find that as a constant. 186 00:10:20,240 --> 00:10:22,840 We can do it right here. We could say const. 187 00:10:22,840 --> 00:10:26,640 We want it to be a double right because it's not an integer, it’s 1.19 188 00:10:26,640 --> 00:10:30,840 and we'll give it a name. We will call it US dollars per Euro, 189 00:10:33,040 --> 00:10:34,040 per EUR. 190 00:10:34,540 --> 00:10:37,640 And we will initialized that to 1.19. 191 00:10:38,640 --> 00:10:40,440 And don't forget the semicolon. 192 00:10:40,440 --> 00:10:43,340 So now we've declared that constant US dollars 193 00:10:43,340 --> 00:10:45,540 per Euro to be 1.19. 194 00:10:45,540 --> 00:10:47,840 It's not going to change even accidentally. 195 00:10:49,140 --> 00:10:50,440 Now what do we need to do? 196 00:10:50,440 --> 00:10:53,940 Well, we need to output a message to the user. We will say cout. 197 00:10:54,540 --> 00:10:58,640 We will say welcome to the EUR 198 00:10:59,440 --> 00:11:01,440 to USD converter 199 00:11:02,140 --> 00:11:03,440 or something like that. 200 00:11:05,640 --> 00:11:07,640 And we'll put an endline at the end. 201 00:11:08,640 --> 00:11:10,940 We need to ask the user to enter 202 00:11:10,940 --> 00:11:14,240 the value in Euros, right and that's the whole idea. So cout, 203 00:11:14,440 --> 00:11:17,840 we will say enter the value 204 00:11:20,840 --> 00:11:22,040 in Euros, 205 00:11:24,040 --> 00:11:25,640 and we'll do something like that. 206 00:11:26,190 --> 00:11:29,690 We won't put a new line there because we want the user to 207 00:11:29,690 --> 00:11:31,190 type something and right next to it. 208 00:11:31,990 --> 00:11:35,990 What do we do next? Well, we need to do a few things. We need – remember, 209 00:11:35,990 --> 00:11:39,980 most of these programs are input, processing and then output. 210 00:11:40,180 --> 00:11:43,180 We need input. We need to read in from cin, 211 00:11:43,480 --> 00:11:47,280 the number of Euros that they want to convert, right? So we need to have something like 212 00:11:47,680 --> 00:11:49,680 cin into 213 00:11:50,480 --> 00:11:51,380 euros. 214 00:11:52,480 --> 00:11:55,280 So we need to define that variable right here. 215 00:11:56,080 --> 00:11:59,380 So let's do that right in here. We'll say double. 216 00:12:00,980 --> 00:12:02,680 We will call it Euros. That's a good name, 217 00:12:03,680 --> 00:12:05,880 and will initialize it to 0.0. 218 00:12:05,880 --> 00:12:08,880 That way we know we've got a 0 value in there. 219 00:12:10,240 --> 00:12:12,910 We know we're going to convert this to dollars, right. So we may as well 220 00:12:12,910 --> 00:12:16,210 create our dollars variable as well. So we can say double, 221 00:12:18,410 --> 00:12:19,910 and we can call it dollars. 222 00:12:21,010 --> 00:12:23,210 And again 0.0. 223 00:12:25,710 --> 00:12:29,910 Perfect. Now we're right in the Euros. We need to convert 224 00:12:30,270 --> 00:12:34,470 the Euros to Dollars. How do we do that? We just use a real simple multiplication. 225 00:12:34,470 --> 00:12:37,970 We can say that the number of Dollars is equal to 226 00:12:38,470 --> 00:12:40,070 again the number of euros 227 00:12:41,070 --> 00:12:44,070 x the const that we have, 228 00:12:45,170 --> 00:12:46,170 dollars per Euro. 229 00:12:47,470 --> 00:12:51,670 And now all we need is that final output statement. Let me scroll up just a little bit 230 00:12:51,670 --> 00:12:53,870 and we will say something like cout 231 00:12:54,370 --> 00:12:57,970 and we'll say the Euros, whatever they entered 232 00:13:00,570 --> 00:13:03,870 is equivalent to or we will actually say Euros, 233 00:13:03,870 --> 00:13:06,370 that'll be nice so that I can read it better is 234 00:13:07,470 --> 00:13:09,170 equivalent to, 235 00:13:10,870 --> 00:13:13,070 you put another insertion operator here, 236 00:13:13,870 --> 00:13:17,170 we will type in dollars, whatever the conversion was 237 00:13:17,420 --> 00:13:20,420 and then we will just type string dollar, so it's nice and readable. 238 00:13:21,520 --> 00:13:24,520 That's it. We will put a new line at the end line 239 00:13:25,220 --> 00:13:27,020 and that should be it. 240 00:13:27,620 --> 00:13:30,020 Let's run this and see if we have any errors. 241 00:13:30,020 --> 00:13:32,720 We will test it to make sure we don't have any errors 242 00:13:32,720 --> 00:13:34,720 and we will debug it if we do have any errors. 243 00:13:34,720 --> 00:13:38,380 All right. So I'm going to save, and I'm going to press ctrl F5 to run. 244 00:13:39,180 --> 00:13:41,180 There is no compiler error so that's great. 245 00:13:41,380 --> 00:13:44,980 So enter the value in Euros. Well, let's type in 1 euro, right. 246 00:13:44,980 --> 00:13:48,750 If we type in 1 euro, we expect back 1.19 dollar. 247 00:13:48,750 --> 00:13:52,350 That would be a real simple and good test case. 1, 248 00:13:53,150 --> 00:13:56,950 we get 1 euro is equivalent to 1.19 dollars. 249 00:13:57,250 --> 00:13:59,450 Good. Let's try another one. 250 00:14:02,050 --> 00:14:02,950 Let’s run. 251 00:14:04,250 --> 00:14:09,150 How about 123.5 Euros. 252 00:14:09,950 --> 00:14:13,950 We get back 146.965 dollars. Don't worry about the 253 00:14:13,950 --> 00:14:16,950 precision and the dollars and cents. We will learn how to 254 00:14:16,950 --> 00:14:19,750 format output a little bit later. That's it. 255 00:14:19,750 --> 00:14:23,450 That's the program. Really, really simple. You can see what we're doing here is we're 256 00:14:23,450 --> 00:14:27,810 using the things that we've learned, right? We're using our cin. We're using our cout. 257 00:14:27,810 --> 00:14:30,410 We've got a constant to find right here. 258 00:14:30,410 --> 00:14:34,070 We've got some variables that we need here and we’re initializing them to 0. 259 00:14:34,670 --> 00:14:38,660 And we're doing our calculation here using one of the arithmetic operators, 260 00:14:38,660 --> 00:14:39,960 the multiplication symbol. 261 00:14:40,760 --> 00:14:45,020 I encourage you to write one of these programs on your own. 262 00:14:45,020 --> 00:14:48,620 Write, I don't know, something to something converter, Fahrenheit to Celsius, 263 00:14:48,620 --> 00:14:52,620 kilometers to miles, yards to inches, meters to centimeters. 264 00:14:52,620 --> 00:14:55,620 Write one of these programs just like that, that'll give you a good practice.