> length; cout << "What is the width of the rectangle? "; cin >> width; area = length * width; cout << "The area of the rectangle is " << area << ".\n"; return 0; }"> > length; cout << "What is the width of the rectangle? "; cin >> width; area = length * width; cout << "The area of the rectangle is " << area << ".\n"; return 0; }">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.

Similar presentations


Presentation on theme: "Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220."— Presentation transcript:

1 Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220

2 The cin Object The cout object’s counterpart for input is cin Standard console input object Prompt the user for input >> stream extraction operator (remember <<, the stream insertion operator Gather input is a two step process use cout object to display a prompt use cin object to read a value from the keyboard

3 // This program asks the user to enter the length and width of // a rectangle. It calculates the rectangle's area and displays // the value on the screen. #include using namespace std; int main() { int length, width, area; cout << "This program calculates the area of a "; cout << "rectangle.\n"; cout << "What is the length of the rectangle? "; cin >> length; cout << "What is the width of the rectangle? "; cin >> width; area = length * width; cout << "The area of the rectangle is " << area << ".\n"; return 0; }

4 Entering multiple values // This program asks the user to enter the length and width of // a rectangle. It calculates the rectangle's area and displays // the value on the screen. #include using namespace std; int main() { int length, width, area; cout << "This program calculates the area of a "; cout << "rectangle.\n"; cout << "Enter the length and width of the rectangle "; cout << "separated by a space.\n"; cin >> length >> width; area = length * width; cout << "The area of the rectangle is " << area << endl; return 0; }

5 Entering different data types // This program demonstrates how cin can read multiple values // of different data types. #include using namespace std; int main() { int whole; double fractional; char letter; cout << "Enter an integer, a double, and a character: "; cin >> whole >> fractional >> letter; cout << "Whole: " << whole << endl; cout << "Fractional: " << fractional << endl; cout << "Letter: " << letter << endl; return 0; }

6 Reading Strings cin can read more than one character and store it in memory as a character array, or C-string char company[12] Note: A larger string entered will overflow the array’s boundaries and destroy data in memory! Char Type Name Number indicates the size of the array

7 // This program demonstrates how cin can read a string into // a character array. #include using namespace std; int main() { char name[21]; cout << "What is your name? "; cin >> name; cout << "Good morning " << name << endl; return 0; }

8 // This program reads two strings into two character arrays. #include using namespace std; int main() { char first[16], last[16]; cout << "Enter your first and last names and I will\n"; cout << "reverse them.\n"; cin >> first >> last; cout << last << ", " << first << endl; return 0; }

9 Mathematical Expressions C++ allows you to construct complex mathematical expressions using multiple operators and grouping symbols Expression: statement that has a value Example: sum = 21 + 3; Num = 4; Result = x; Result = 15 / 3; Result = a + b + c;

10 // This program asks the user to enter the numerator // and denominator of a fraction and it displays the // decimal value. #include using namespace std; int main() { double numerator, denominator; cout << "This program shows the decimal value of "; cout << "a fraction.\n"; cout << "Enter the numerator: "; cin >> numerator; cout << "Enter the denominator: "; cin >> denominator; cout << "The decimal value is "; cout << (numerator / denominator) << endl; return 0; }

11 Operator Precedence PMDAMS! 1. Parentheses 2. Multiplication 3. Division 4. Addition 5. Modulus 6. Subtraction Examples 5 + 2 * 4 = ? 10 / 2 -3 = ? 8 + 12 * 2 – 4 = ? 4 + 17 % 2 – 1 = ? 6 – 3 * 2 + 7 – 1 = ?

12 Associativity and Grouping Either left to right or right to left If two operators sharing an operand have the same precedence, they work according to their associativity * / % + - (left to right) Unary negation – (right to left) You can use () to group operations and force their precedence Result = (a + b) / 4;

13 Converting Algebraic Expressions to Programming Statements Examples 6B → 6 * B (3)(12) → 3 * 12 4xy → 4 * x * y; Insert parentheses where necessary → x = (a + b) / c; Using exponents → y = pow(x,2); This statement calls pow, with arguments x and 2 and the result of the power is returned

14 // This program calculates the area of a circle. // The formula for the area of a circle is Pi times // the radius squared. Pi is 3.14159. #include #include // needed for pow function using namespace std; int main() { double area, radius; cout << "This program calculates the area of a circle.\n"; cout << "What is the radius of the circle? "; cin >> radius; area = 3.14159 * pow(radius, 2.0); cout << "The area is " << area << endl; return 0; }

15 When you mix apples and oranges: Type Conversion When an operator’s operands are different data types, C++ will automatically convert to the same data type, affecting the results Depends on Ranking and Rules

16 Rules 1. chars, shorts, and unsigned shorts are automatically converted to int 2. when an operator works with two values of different data types, the lower ranking value is promoted to the type of the higher-ranking value 3. when the final value of an expression is assigned to a variable, it will be converted to the data type of the variable

17 Overflow and Underflow When a variable is assigned a value that is too large or too small in range for that variable’s data type, the variable overflows or underflows

18 // This program demonstrates integer overflow and underflow. #include using namespace std; int main() { // testVar is initialized with the maximum value for a short. short testVar = 32767; // Display testVar. cout << testVar << endl; // Add 1 to testVar to make it overflow. testVar = testVar + 1; cout << testVar << endl; // Subtract 1 from testVar to make it underflow. testVar = testVar - 1; cout << testVar << endl; return 0; }

19 // This program can be used to see how your system handles // floating point overflow and underflow. #include using namespace std; int main() { float test; test = 2.0e38 * 1000; // Should overflow test. cout << test << endl; test = 2.0e-38 / 2.0e38; // Should underflow test. cout << test << endl; return 0; }

20 Type Casting Type casting allows for manual data type conversion General format static_cast (Value) Example Double number = 3.7; int val; val = static_cast (number);

21 // This program uses a type cast to avoid integer division. #include using namespace std; int main() { int books; // Number of books to read int months; // Number of months spent reading double perMonth; // Average number of books per month cout << "How many books do you plan to read? "; cin >> books; cout << "How many months will it take you to read them? "; cin >> months; perMonth = static_cast (books) / months; cout << "That is " << perMonth << " books per month.\n"; return 0; }

22 // This program uses a type cast expression to print a character // from a number. #include using namespace std; int main() { int number = 65; // Display the value of the number variable. cout << number << endl; // Display the value of number converted to // the char data type. cout (number) << endl; return 0; }

23 Named Constants Literals may be given names that symbolically represent them in a program Example: PI const double PI = 3.1459;

24 // This program calculates the area of a circle. // The formula for the area of a circle is PI times // the radius squared. PI is 3.14159. #include #include // needed for pow function using namespace std; int main() { const double PI = 3.14159; double area, radius; cout << "This program calculates the area of a circle.\n"; cout << "What is the radius of the circle? "; cin >> radius; area = PI * pow(radius, 2.0); cout << "The area is " << area << endl; return 0; }

25 Multiple Assignment and Combined Assignment

26 Formatting Output

27 Formatted Input

28 Introduction to File Input and Output

29 More Math Library Functions

30 Problem Solving: Case Study


Download ppt "Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220."

Similar presentations


Ads by Google