Download presentation
Presentation is loading. Please wait.
1
More C++ Basics October 4, 2017
2
Division Recall from last time that division is more nuanced than the other arithmetic operations. The / command refers to integer division: how many times does one number go into another, ignoring the remainder? 10 / 3 == 3 5 / 2 == 2 20 / 3 == 6 You do not need to include spaces between the numbers—this is done to make the code easier to read.
3
Division To include the decimal part with the quotient, you MUST include a decimal part with at least one of the original numbers. 5 / 2 == 2 5.0 / 2 == 2.5 5 / 2.0 == 2.5 5.0 / 2.0 == 2.5
4
Division Pitfalls Consider the following:
double a = 5, b = 2; cout << a / b; Be careful!!! The CS50 IDE will output 2.5, but some compilers (e.g., NetBeans) will output 2. Best to use the following declaration and initialization: double a = 5.0, b = 2.0;
5
Division Pitfalls Consider the following code:
double c = 20; double f; f = (9 / 5) * c ; What value is assigned to f? How can this error be corrected?
6
Decimal Numbers Sometimes it is helpful to use a certain number of digits after the decimal place—for instance, to always have two when dealing with dollar amounts. The following three lines of code will set all variables of type double to display with exactly two decimal places: cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2);
7
Characters A variable of type char (character) is a single symbol such as a letter, digit, or punctuation mark. A variable of type char is always contained inside of single quotes: char letter = 'A'; 'A' and "A" are not equivalent! "A" is a string, not a character. A blank space is considered a character: ' '.
8
Strings A string is a series of one or more text characters.
string day = "Monday"; In order to use the string class, you must include the string library: #include <string>
9
Strings The + symbol can be used to concatenate strings (i.e., put two or more strings together). Consider: string day, day1, day2; day1 = "Monday"; day2 = "Tuesday"; day = day1 + day2; This results in the concatenated string MondayTuesday. Spaces must be explicitly added: day = day1 + " " + day2;
10
If-Else Statements Consider the following scenario: An employee receives his/her regular hourly salary for the first 40 hours worked in a week. If the employee works beyond 40 hours, (s)he receives 1.5 times the regular rate per hour. If the employee works 40 or more hours, we can model this using the following formula: (rate * 40) + (1.5 * rate * (hours – 40)) Note that if the employee works fewer than 40 hours, (s)he will receive a negative paycheck! In this case, we need the formula rate * hours.
11
If-Else Statements We need the program to do the following:
Determine whether hours > 40. If hours > 40, use the following formula: gross_pay = (rate * 40) + (1.5 * rate * (hours – 40)); If not, use the following formula: gross_pay = rate * hours; We can accomplish this using an if-else statement.
12
If-Else Statements if(hours > 40) { gross_pay = (rate * 40) + (1.5 * rate * (hours – 40)); } else { gross_pay = rate * hours;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.