Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS 1430: Programming in C++ Turn in your Quiz1-1 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 Lab0 • Due time: 5 PM, September 3 • Grace time: 5 PM, September 8 4 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) ... 5 Lab0 • Submit to the Grader • Go to My Home Page then to the Grader http://people.uwplatt.edu/~yangq/ 6 Add Two Numbers In Math S=x+y Or x+y=S 7 Add Two Numbers In C++ Sum = num1 + num2; // This is a comment // Semicolon: Statement terminator // Use meaningful names Sum = num1 + num2; 8 Assignment Operator: = num1 + num2 = Sum; // Valid in C++? // No // Assignment statement MUST // be from right to left Sum = num1 + num2; // Correct! // Not equal 9 Are the two statements the same? Sum = Num1 + Num2; Sum = num1 + num2; // NO! // C++ is case sensitive! 10 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++! 11 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! 12 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! 13 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 14 Precedence Rules • • • • From high to low: () *, / +, - 15 Examples x = 8 - 4 * 2; // result: x = (8 - 4) * 2; // result: y = 8 / 4 * 2; // result: y = 8 / (4 * 2); // result: 16 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? 17 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 18 Integer Division We can only get integers! Long Division! 7/5 Quotient: 1 Remainder: 2 5/7 Quotient: 0 Remainder: 5 19 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… 20 Quotient (Division) Operator: / Expression 7/5 5/7 14 / 5 5 / 14 143 / 10 10 / 143 Result 1 0 2 0 14 0 21 Remainder (Modular) Operator: % Expression 7%5 5%7 14 % 5 5 % 14 143 % 10 10 % 143 Result 2 5 4 5 3 10 22 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 23 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 24 Summary • Statement Terminator • Assignment Operator total = total + num1; // from right to left • Arithmetic Operators and Precedence Rules () /, %, * -, + • Semantics, Syntax, and Style 25 Quiz 1-2 1 point Due beginning of class Wednesday 26 Visual Studio Demo 27