In this example, we’ll write a program to calculate the area of the triangle. In case we don’t remember, we should look that up from some reference source in this case the internet. The area of a triangle is half the base times the height. And we find our formula here 1/2 base times height. We can create an I-P-O chart. Our inputs would be base and height. Our output would be area. And the process would be: area equals 1/2 the base times the height. Let’s look at how this would look in C++. We need to declare all our variables that showed up in our IPO. So we’re going to go ahead and double base, height, and area. Okay. Remember double is for floating-point numbers. So if we expect a decimal point, we would want to use double. If we didn’t, we could use int or integer. Okay, in the next step, our input. We need to input base. Remember, we need a user-friendly output, so we use a cout (see out) statement. To put a literal string on the screen to prompt the user, “Enter the base of the triangle.” And then we cin (see in) our getting input from the keyboard… base. So that’s our first input. Our next input is for height, so again we have a pair of cout and cin statements. So user-friendly prompt and the actual input statement to get input from the keyboard into height. We have our assignment statement here area equals 1/2 base times height. Now we have a little problem here in C++. Our variable names don’t match so if we tried to compile this right now, we’re going to have a syntax error. So we actually have to use the same variable names. So it would have to be base and height. And we also need an operator. In math class if we see b and h together, that implies multiplication. But in C++, we have to explicitly say multiply. So even here we need a multiplication symbol. Okay, so basically 1 divided by 2 times base times height would be the equivalent of our formula that we looked up. Okay, and then our output statement the area of a triangle is area. Okay and end with an end line (endl). Now we still have one problem here, that I’m going to show you in our statement. Here. Mainly dealing with this 1/2, but let’s test our program. We’ll compile it and run it. I’m going to use some simple inputs. So I’ll pick… Okay, so we’ll pick some easy numbers. 2 for the base and also 2 for the height And as you can see, we have a problem here. It’s telling us the area of the triangle is 0. Ok now Let’s go back to this Even though 1 & 2… 1 divided by 2 normally we would think of this like point 5 decimal but You have to be aware of something in C++ because the datatype for 1 and the data type for 2, even though they’re constants are integers You get an integer result. So 1 divided by 2 is 0.5 but the result being an integer you chop off the decimal part so it turns out to be 0. Okay, I know that’s a little bit different.. maybe. But that’s how integer division works, so to change that, we’re going to change one of the two numbers into a non integer or floating-point number just by simply changing… adding the point 0 in the end. So divided by 2 point 0 (2.0) this becomes a floating point number a real number and by the rules of promotion when we have an integer divided by a floating point, we convert everything to floating point. So if we run this again… Okay. We should get.. 2, 2, and 2 So 2 times 2 is 4, divided by 2. Okay good we get the right answer. The area of the triangle is 2. So just something to be aware of with integer division. You have to be careful of that in the future. And a simple fix is just to put the point 0. We don’t change the value. We just change the data type of that constant.