In this video, we'll go over c++ arithmetic operators.
These are exactly the ones you expect. The + symbol means addition.
the - symbol mean subtraction, the * is multiplication,
the / is division,
and the % symbol is the modulo operator or the remainder of division.
You may not be familiar with this operator, but that's okay, we'll cover it toward the end of the video.
The +, -, *, and / operators are overloaded.
What that means is that work with different types.
So we can add two integers, we can add two floats, we can add two doubles
by simply using the plus symbol.
The same is true for, subtraction, multiplication, division.
The modulo operator only works with integers.
And again, I'll show you an example of that in a little bit.
So let's try a few examples here.
In the code done here, you can see that I've declared
two integers: num1 and num2
and I've assigned 200 and 100 to them, respectively.
Again, these are integers. You could do this with doubles or floats or anything you'd like
but I'm just going to use integers here.
You can see that this first line, this first output statement, right here.
Let's walk through that really quickly so we're clear as to what's going on here.
Remember, we've got expressions here.
So num1 is an expression, num2 is an expression
and that addition expression
is evaluated to the sum of num1 and num2.
So in this this case, num1 is 200 so this will be replaced with 200.
Num2 is 100, and this will be replaced with 100.
And then num1 + num2 will be added together, that's why the plus is there
and this will give us 300.
Okay. Now these guys are inside
quotes so there are string literals and that would be printed out as is.
So what we expect here is 200 + 100 equals 300 and of course, we've got
the new line or the end line at the end.
All right. So let's run this
and see if we get what we expect.
And exactly that's what happens. 200 plus 100 equals 300.
Okay. So let me comment that line out and
what we could do is rather than do that calculation
right in line right here,
we could simply do the calculation and assign it to a variable.
In this case, let's create a variable called result.
And we'll initialize it to 0. And all were saying is
add num1 and num2 that'll give us 300 and assign that result
to the result variable. Okay. So we're going to add
200 + 100, that gives us 300, and we're storing 300 into result.
And then rather than do the calculation here, as we did, we're just going to print out result.
It's as easy as that. So when I run this now, I should have the same
exact output,
200 plus 100 equals 300.
So now let me comment out some of these, and we'll walk through these real quick.
And I'll do it right up to there.
So this next one here on line 29,
I'm simply subtracting num1
- num2, storing the difference in result
and then I'm out putting result.
So here, we would expect 200 - 100, we'd expect 100.
On line 32, we're multiplying the 2.
This is different from algebra.
Do you have to have that multiplication symbol, that asterisk.
You can't just say num1, num2 or something like that like you were doing algebra,
you just put the two things together. You can't use
a dot like you could in algebras as well. It's got to be
the multiplication operator, which is the asterisk.
So we're going to multiply num1 and num2 that will give us 20,000, which we store in result,
and we're displaying that.
And then the third one would be result is num1 divided by num2,
that's on line 35.
And we're going to store that quotient into result.
There we would expect 200/100, it gives us 2.
Okay. So let's run what we've got right here and
see what our results are.
So we've got 200 + 100, like we did before is 300.
200 - 100 is a 100. 200 * 100 is 20000.
And 200/100 is 2.
Okay. So this is exactly like you would expect. It's really, really simple.
Math works just like you expected to.
Now something you may not expect is this result right here on line 38.
I'm going to uncomment this.
And in this case, what I'm doing is
I'm dividing num2/num1.
Okay. Remember, num2 is 100 and num1 is 200.
So what's happening here is I'm dividing 100
/200. You would expect 0.5.
That's not what you get. We're doing integer division.
Okay. So there are no digits after the decimal point.
We're working with whole numbers only. So in this case,
what happens is 100
gets divided by 200.
And the results if this if these were doubles would be 0.5.
But we're working with integers. So this piece is just chopped right off
so we end up with a 0.
Okay. So let's run this.
And you can see here 100/200 is 0.
You've got to be really careful when you're doing division with whole numbers or integers
because this is the behavior.
If you need to 0.5, you want to be really, really precise,
then you need to use doubles, not integers.
And I'll show you an example using doubles in a second.
Now let's look at the modular operator or the remainder of operator.
In this case, right here,
I'm saying num1 mod num2.
Again, what's num1, it’s 200.
What's num2, 100.
So 200 divided by, let me write that out real clear
so it's not confusing it all. So we're going to say 200
divided by 100.
The result of this is 2 with a remainder of 0.
That's the modular operator.
It's only interested in the remainder. So in this case, it’ll print out a 0.
The next line, I’ll uncomment that out in a second. But the next line we're going to say
right here, num1
let me clear that draw that line a little nicer there.
Here we're saying num1 mod num2. So we're saying 10
mod 3.
Remember, this only works with integers.
So the results of the division would
be 3 with a remainder of 1,
number 3 x 3 is 9 and you get the 1 remainder.
That's the result of the modular operator. It's a pretty handy operator.
So let me clear this and I'll uncomment these few lines of code right here.
And we will run this.
And let's take a look at the result.
So here we go. 200 mod 100
is 0, right that's the remainder because we get a 2 with a remainder of 0.
And then this case 10 mod 3
is 1 because we get a 3 with a remainder of 1 when we divided by 3.
This could be a really, really handy operator to use.
And in the challenge exercise, you're going to have to use it to solve the problem.
A couple more things. Just like you can with
any programming language and doing any kind of math, we can add and
divide and subtract and we can do everything in line.
It's important to understand
that the precedents or the order in which this stuff
happens is the same precedents you probably learned in grade school.
Here in the US, we learned PEMDAS.
That was the acronym we used which meant parentheses
first then exponents then multiplication and division
and then addition and subtraction.
You may have learned something else if you're from a different country, but that's what we learned here.
So what this means is that
multiplication and division happened before addition and subtraction, right.
So in this case what’s going to happen is num1
is going to be multiplied by 10 and then
we're going to add num2 to that result,
right. If you want something else,
if you want to add the 10 in the num2 first,
then you need to use parentheses around there like that.
It's as simple as that and again it's something that we're all used to by now.
Let me clear this and then I'll do this last example with the division.
I'll comment these two lines out.
So in this case, I'm outputting 5/10.
Remember, this is using integer division.
So 5/10 is 0, not 0.5.
In this case,
5.0/10.0 is 0.5
because we're using doubles here.
In a couple of videos from now, we're going to do a video
where we're mixing types, we will be multiplying and dividing integers and doubles all together
on the same expression. In that case,
we can use casts to tell the compiler,hey, you know what,
I really want this to be a double instead of an integer, even though it looks like an integer there.
And so we'll get to that in a minute. And let me run this thing one more time.
And you can see the last two
results right down here 5/10 is 0
and 5.0/10.0 is 0.5.
Okay. So now let's solve a real problem. Let's convert
Euros to U.S. dollars
and I've created a
project in the section 8 workspace,
called Euros.
This project that I worked on right here is called arithmetic operator
so you can refer to that if you like.
But I've created a project called Euros and that's when we'll get to next.
Okay. So I've opened up the main CPP file in the Euros project.
Right now there's really not much in here.
I'm just printing a new line at the end to separate that little time message
that is displayed at the end
and there's no real code here. We're going to write the code from scratch.
So what we want to do is we want to convert
Euros to US dollar. So obviously we need some sort of conversion factor.
I just went on Google now and I saw that
you have 1.19
dollars per Euro. Okay. So 1 euro is 1.19 dollar.
So we need to use that value somewhere.
Now that's going to be a constant. So let's just find that as a constant.
We can do it right here. We could say const.
We want it to be a double right because it's not an integer, it’s 1.19
and we'll give it a name. We will call it US dollars per Euro,
per EUR.
And we will initialized that to 1.19.
And don't forget the semicolon.
So now we've declared that constant US dollars
per Euro to be 1.19.
It's not going to change even accidentally.
Now what do we need to do?
Well, we need to output a message to the user. We will say cout.
We will say welcome to the EUR
to USD converter
or something like that.
And we'll put an endline at the end.
We need to ask the user to enter
the value in Euros, right and that's the whole idea. So cout,
we will say enter the value
in Euros,
and we'll do something like that.
We won't put a new line there because we want the user to
type something and right next to it.
What do we do next? Well, we need to do a few things. We need – remember,
most of these programs are input, processing and then output.
We need input. We need to read in from cin,
the number of Euros that they want to convert, right? So we need to have something like
cin into
euros.
So we need to define that variable right here.
So let's do that right in here. We'll say double.
We will call it Euros. That's a good name,
and will initialize it to 0.0.
That way we know we've got a 0 value in there.
We know we're going to convert this to dollars, right. So we may as well
create our dollars variable as well. So we can say double,
and we can call it dollars.
And again 0.0.
Perfect. Now we're right in the Euros. We need to convert
the Euros to Dollars. How do we do that? We just use a real simple multiplication.
We can say that the number of Dollars is equal to
again the number of euros
x the const that we have,
dollars per Euro.
And now all we need is that final output statement. Let me scroll up just a little bit
and we will say something like cout
and we'll say the Euros, whatever they entered
is equivalent to or we will actually say Euros,
that'll be nice so that I can read it better is
equivalent to,
you put another insertion operator here,
we will type in dollars, whatever the conversion was
and then we will just type string dollar, so it's nice and readable.
That's it. We will put a new line at the end line
and that should be it.
Let's run this and see if we have any errors.
We will test it to make sure we don't have any errors
and we will debug it if we do have any errors.
All right. So I'm going to save, and I'm going to press ctrl F5 to run.
There is no compiler error so that's great.
So enter the value in Euros. Well, let's type in 1 euro, right.
If we type in 1 euro, we expect back 1.19 dollar.
That would be a real simple and good test case. 1,
we get 1 euro is equivalent to 1.19 dollars.
Good. Let's try another one.
Let’s run.
How about 123.5 Euros.
We get back 146.965 dollars. Don't worry about the
precision and the dollars and cents. We will learn how to
format output a little bit later. That's it.
That's the program. Really, really simple. You can see what we're doing here is we're
using the things that we've learned, right? We're using our cin. We're using our cout.
We've got a constant to find right here.
We've got some variables that we need here and we’re initializing them to 0.
And we're doing our calculation here using one of the arithmetic operators,
the multiplication symbol.
I encourage you to write one of these programs on your own.
Write, I don't know, something to something converter, Fahrenheit to Celsius,
kilometers to miles, yards to inches, meters to centimeters.
Write one of these programs just like that, that'll give you a good practice.