270 likes | 282 Views
This webpage provides instructions and examples for Lab0 in the CS.1430: Programming in C++ course. It covers topics such as coping files, starting a new project, adding existing CPP files, and using arithmetic operators.
E N D
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
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
Lab0 • Due time: 5 PM, September 3 • Grace time: 5 PM, September 8
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/
Add Two Numbers In Math S = x + y Or x + y = S
Add Two Numbers In C++ Sum = num1 + num2; // This is a comment // Semicolon: Statement terminator // Use meaningful names Sum = num1 + num2;
Assignment Operator: = num1 + num2 = Sum; // Valid in C++? // No // Assignment statement MUST // be from right to left Sum = num1 + num2; // Correct! // Not equal
Are the two statements the same? Sum = Num1 + Num2; Sum = num1 + num2; // NO! // C++ is case sensitive!
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++!
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!
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!
Semantics, Syntax, and Style total = 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
Precedence Rules • From high to low: • ( ) • *, / • +, -
Examples x = 8 - 4 * 2; // result: x = (8 - 4) * 2; // result: y = 8 / 4 * 2; // result: y = 8 / (4 * 2); // result:
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?
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
Integer Division We can only get integers! Long Division! 7 / 5 Quotient: 1 Remainder: 2 5 / 7 Quotient: 0 Remainder: 5
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…
Quotient (Division) Operator: / Result 1 0 2 0 14 0 Expression 7 / 5 5 / 7 14 / 5 5 / 14 143 / 10 10 / 143
Remainder (Modular) Operator: % Result 2 5 4 5 3 10 Expression 7 % 5 5 % 7 14 % 5 5 % 14 143 % 10 10 % 143
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
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
Summary • Statement Terminator • Assignment Operator total = total + num1; // from right to left • Arithmetic Operators and Precedence Rules () /, %, * -, + • Semantics, Syntax, and Style
Quiz 1-2 1 point Due beginning of class Wednesday