agenda review compiling review data types integer division composition c++ mathematical functions...

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

Upload: katrina-bradley

Post on 25-Dec-2015

223 views

Category:

Documents


0 download

TRANSCRIPT

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

Agenda

Review Compiling

Review Data Types

Integer Division

Composition

C++ Mathematical Functions

User Input

Reading: 2.7-3.4, 8.11

Homework #3

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

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

Page 3: Agenda Review Compiling Review Data Types Integer Division Composition C++ Mathematical Functions User Input Reading: 2.7-3.4, 8.11 Homework #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)?

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

Review: Data Types

What is a...

Character?

Integer?

Double?

Float?

String?

Boolean?

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

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

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

Review: Data TypesHow 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”;

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

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

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

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

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

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) * 1000 * 1000

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) * 1000.7466 * 10074.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

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

Order of operations

C++ uses the same order of operations you would expect from any mathematical equation.

Division and Multiplcation first, left to rightFollowed by Addition and Subtraction, left to right

You can also use parenthesis, just as you do in math.

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

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;

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

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);

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

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);

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

math.h library

Some extra math functions are included in the math.h header. To use them, we put #include <math.h> at the top of our source code.

log log10sin cosacos

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

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

ctype library

The ctype library has functions for determining if a character meets certain type conditions:

isalnum isalphaisdigit islowerisupper

You can look up more functions online:http://www.cplusplus.com/reference/cctype/

Code Example: CTypeFunctions.cpp

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

PracticeWrite 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 r3)

4) Converts kilometers to miles (miles = kilometers * 0.621371)

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

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

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

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 ^Hstty erase ^\?

Save the file. Close your terminal and open a new one.

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

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 << “!”;

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

cin / cout operators

Note that cout uses the << operatorcin uses the >> operator

I like to think of them as pointing to where the input/output is going to. Either the screen (cout << something), or the variable (cin >> myVar).

Examples: CinNumbersExample.cpp, CinGreeting.cpp

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

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.

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

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

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

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

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

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().

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

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.

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

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.

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

HW #3

Posted online