Upload
joel-floyd
View
222
Download
0
Embed Size (px)
Citation preview
1
CS 1430: Programming in C++
Turn in your Quiz1-1
My Home Page URL
http://people.uwplatt.edu/~yangq/
Go to UWP home page
Add “/~yangq” or
Search for Qi Yang
2
Installing VS 2012 on Your PC
• Email about Deamspark
(in Junk folder?)
• Help section on the web site
• Go to ITS Help Desk if still issues
3
4
Lab0
• Due time: 5 PM, September 3
• Grace time: 5 PM, September 8
5
Instructions
. . .
4. Coping files
. . .
8. Starting a New Project
(project folder is created)
9. Adding an Existing CPP file to your project
(copy file to the folder created at step 8)
. . .
Lab0
• Submit to the Grader
• Go to My Home Page then to the Grader
http://people.uwplatt.edu/~yangq/
6
7
Add Two Numbers
In Math
S = x + y
Or
x + y = S
8
Add Two Numbers In C++
Sum = num1 + num2;
// This is a comment
// Semicolon: Statement terminator
// Use meaningful names
Sum = num1 + num2;
9
Assignment Operator: =
num1 + num2 = Sum; // Valid in C++? // No // Assignment statement MUST // be from right to left
Sum = num1 + num2; // Correct! // Not equal
10
Are the two statements the same?
Sum = Num1 + Num2;
Sum = num1 + num2;
// NO!
// C++ is case sensitive!
11
Arithmetic Operators
• Addition: +
to compute Sum• Subtraction: -
to compute Difference• Multiplication: *
to compute Product• Division: /
to compute Quotient
You MUST use operators to compute in C++!
12
Arithmetic Operators • What is ** in C++?
result = x ** y;
// Not good!
• What is ^ in C++?
result = x ^ y;
// Not good!
S = x (y + z); //Is it good in C++?
//Missing operator!
S = x * (y + z); // Good!
13
Programming Style
S = x * (y + z); // Good!
S = x*(y+z); // What’s the difference?
// Is it good in C++?
// Yes
// Is it good in CS1430 at UWP?
// No!
// Style!
S=x * (y + z); // Not Good!
S = x * (y + z); // Good!
14
Semantics, Syntax, and Styletotal = total + 2;
• Semantics Increase total by 2 Assignment (right to left) Not equal• Syntax Statement terminator Case sensitive • Style one space before and after any operator meaningful name
15
Precedence Rules
• From high to low:
• ( )
• *, /
• +, -
16
Examples
x = 8 - 4 * 2; // result:x = (8 - 4) * 2; // result: y = 8 / 4 * 2; // result: y = 8 / (4 * 2); // result:
17
More Examples
z = 8 / 4 ^ 2;
// No!
z = 8 / 4 ** 4;
// NO!
z = 8 / 4 * 4
// Result: ?
X = 5(3 – 7);
//Result: ?
z = 5 / (2 * 5);
// Result: 0.5 or 0?
18
Integer Division vs. Float Division
In Math 5 is almost always the same as 5.0
In C++
5 is almost never the same as 5.0
Why?
Store value in computer as bits, bytes.
5 is stored as an integer
5.0 is stored as a float number
19
Integer Division
We can only get integers!
Long Division!
7 / 5
Quotient: 1
Remainder: 2
5 / 7
Quotient: 0
Remainder: 5
20
Float Division
We get float result!
Long Division!
7.0 / 5.0
Quotient: 1.4
7 / 5.0 (Same)
7.0 / 5 (Same)
5.0 / 7
Quotient: 0.714285…
21
Quotient (Division) Operator: /
Expression
7 / 5
5 / 7
14 / 5
5 / 14
143 / 10
10 / 143
Result
1
0
2
0
14
0
22
Remainder (Modular) Operator: %
Expression
7 % 5
5 % 7
14 % 5
5 % 14
143 % 10
10 % 143
Result
2
5
4
5
3
10
23
Exercises
Expression Result 12 % 20 * 3
(12 % 20) * 3
12 * 3
20 % 12 * 3
12 / 20 * 3
20 / 12 * 3
20 / 12 % 3
20 % 12 % 3
24
Why Integer Division?
Get $143 from ATM
Number of $50 bills: 2
143 / 50
Amount left after $50 bills: 43
143 % 50
Number of $10 bills: 4
43 / 10
Amount left after $10 bills: 3
43 % 10
Number of $1 bills: 3
25
Summary
• Statement Terminator
• Assignment Operator
total = total + num1; // from right to left
• Arithmetic Operators and Precedence Rules
()
/, %, *
-, +
• Semantics, Syntax, and Style
26
Quiz 1-2
1 point
Due beginning of class Wednesday
Visual Studio Demo
27