Presentation is loading. Please wait.

Presentation is loading. Please wait.

Agenda Review Compiling Review Data Types Integer Division Composition C++ Mathematical Functions User Input Reading: 2.7-3.4, 8.11 Homework #3.

Similar presentations


Presentation on theme: "Agenda Review Compiling Review Data Types Integer Division Composition C++ Mathematical Functions User Input Reading: 2.7-3.4, 8.11 Homework #3."— Presentation transcript:

1 Agenda Review Compiling Review Data Types Integer Division Composition C++ Mathematical Functions User Input Reading: 2.7-3.4, 8.11 Homework #3

2 Review: Compiling C++ What is a.cpp file? C++ Source Code What is source code? Human readable instructions for the computer to follow. What is a.o file? A binary object, or executable, of the program you wrote source code for. What is a compiler? A compiler translates source code into machine code (binary object, executable) that the computer can understand and execute. How do you compile a C++ program? Use the g++ command: g++ SourceCode.cpp -o Executable.o

3 Review: Data Types What is the difference between declaring and assigning a variable? How do you declare more than one variable at a time (without assigning a value)?

4 Review: Data Types What is a... Character? Integer? Double? Float? String? Boolean?

5 Review: Data Types What is a... Character? A single letter, number, or symbol Integer? A whole number, positive or negative Double? A number with a decimal (large) Float? A number with a decimal (smaller than a double) String? A series of characters Boolean? True or False, 1 or 0

6 Review: Data Types How do you declare and assign a: Character? char someCharacter = 'A'; Integer? int someInteger = 15; Double? double someDouble = 1.25; Boolean? bool thisIsTrue = true; String? string someString = “This is my string”;

7 Integer Division Division can be tricky due to different data types. If you divide two integers, the result is an integer. 2/3 = 0 3/2 = 1 3/3 = 1 double quotient = 2/3; quotient will be 0.00 double quotient = 2.0/3.0; quotient will be 0.66 Code Example: IntegerDivision.cpp

8 What is wrong with this? int numHomeworks = 3; int maxHwGrade = 25; int hw1 = 18; int hw2 = 23; int hw3 = 15; int avg = (hw1 + hw2 + hw3) / (numHomeworks * maxHwGrade) * 100; Code Example: HomeworkGradeDiv.cpp

9 Grade is 0? This is what happens when you use integer division: (hw1 + hw2 + hw3) / (numHomeworks * maxHwGrade) * 100 (18 + 23 + 15) / (3 * 25) * 100 (18 + 23 + 15) / (75) * 100 (56) / (75) * 100 0 * 100 0 If we change the dividend or divisor to a double: (hw1 + hw2 + hw3) / (numHomeworks * maxHwGrade) * 100 (18 + 23 + 15) / (3.0 * 25) * 100 (18 + 23 + 15) / (75.0) * 100 (56) / (75.0) * 100 0.7466 * 100 74.66 Then if we stick 74.66 into our integer avg, it will cut off the decimals and give us 74. Code Example: HomeworkGradeDivFixed.cpp

10 Order of operations C++ uses the same order of operations you would expect from any mathematical equation. Division and Multiplcation first, left to right Followed by Addition and Subtraction, left to right You can also use parenthesis, just as you do in math.

11 Composition In a program, you can replace any value or expression with another expression. Let's say you have declared and assigned the following variables: int a = 10; int b = 5; int sum = a + b; int multiplier = 2; Now, you can calculate the total (some arbitrary equation) in any of these ways: int total = (10 + 5) * 2; int total = (a + b) * multiplier; int total = sum * multiplier;

12 Functions Recall our discussion on functions. If you have a square root function called sqrt() that accepts one parameter (an integer) and returns an integer, you write the definition like this: int sqrt (int num); Function definitions are what you look at to remind yourself what data types and parameters the function requires and what data type it returns. If you look up C++ functions online, you will see function definitions. To actually use the function, you type: int root = sqrt(94786);

13 Functions What about a function called multiplyThree that accepts 3 parameters, all integers, and returns the product of all 3? Definition: int multiplyThree (int num1, int num2, int num3); Use the function: int product = multiplyThree(94786, 3, 12);

14 math.h library Some extra math functions are included in the math.h header. To use them, we put #include at the top of our source code. log log10 sin cos acos You can look up more functions online: http://www.cplusplus.com/reference/cmath/ Learn to read formal documentation – it will help you. Code Example: MathFunctions.cpp, Math.cpp

15 ctype library The ctype library has functions for determining if a character meets certain type conditions: isalnum isalpha isdigit islower isupper You can look up more functions online: http://www.cplusplus.com/reference/cctype/ Code Example: CTypeFunctions.cpp

16 Practice Write a program that... 1) Calculates the hypotenuse of a right triangle with sides of length 9 and 5 (pythagorean theorem) 2) Converts a Celsius temperature to Fahrenheit (F = 9/5 C + 32) 3) Calculates the volume of a sphere (V = 4/3 pi r 3 ) 4) Converts kilometers to miles (miles = kilometers * 0.621371)

17 What is User Input? - Keyboard input - Mouse movement - Finger movement (touch screen) - Voice (speech recognition, think Siri) - Joystick / Game controller *For this class, we use only keyboard

18 Backspace ^? ^H what?? Before we start writing programs with user input, we need to fix something. In UNIX, depending on your terminal settings, pressing the backspace key may result in funny characters. It's easy enough to change these settings: nano ~/.login.user Type in: stty erase ^H stty erase ^\? Save the file. Close your terminal and open a new one.

19 cin The iostream header file provides cout and cin (among others). We know cout handles output to the screen. cout << “Hello!”; cin handles user input from the keyboard string firstName; cout << “What is your first name?”; cin >> firstName; cout << “Hello, “ << firstName << “!”;

20 cin / cout operators Note that cout uses the << operator cin uses the >> operator I like to think of them as pointing to where the input/output is going to. Either the screen (cout > myVar). Examples: CinNumbersExample.cpp, CinGreeting.cpp

21 Input Validation What if a user types in an alphabetical character when I ask for a number? The value that ends up in your variable will be junk. The program will not error, but you will not get your desired result. For now, we won't worry about this. We will assume that the user always types in the correct value.

22 User Input: Strings This program will not work as expected: int main () { cout << “What is your full name?”; string fullName; cin >> fullName; cout << “Hello, “ << fullName << “!” << endl; return 0; } Example: CinString.cpp

23 User Input: Strings cin stops recording user input as soon as a space or new line (enter) is encountered. This program will only print the first name to the screen. Instead, we can use a function called getline() from the header file string. getline(cin, fullName); Example: GetLineString.cpp

24 User Input: Strings So why don't we use getline() all the time? getline() will only save the input as a string. cin will determine, based on the variable you are putting the input into, what data type to use. If you want a user to input a number, use cin. If you want a user to input a string, use getline().

25 Practice 1) Add user input to the programs we did earlier 2) Write a program that asks the user 3 questions (of your choice). Then, print those questions with their answers back to the screen. Example: What is your name? Jenn What is your favorite color? Green What is your favorite season? Fall Jenn's favorite color is Green and favorite season is Fall 2) Write a program that calculates the surface area of a sphere. The user provides the radius. 3) Ask the user to type in a keyboard character. Print to the screen the results of the functions that will tell them if it is a letter, if it is a number, and if it is lowercase.

26 Practice 4) Ask the user for hours and minutes. Print out the total number of minutes. 5) Do the reverse of #4 6) Ask the user for the number of hours they worked and what their hourly wage is. Tell them how much money they have made. Later, when we learn conditionals, we can consider overtime.

27 HW #3 Posted online


Download ppt "Agenda Review Compiling Review Data Types Integer Division Composition C++ Mathematical Functions User Input Reading: 2.7-3.4, 8.11 Homework #3."

Similar presentations


Ads by Google