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

C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.

Similar presentations


Presentation on theme: "C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin."— Presentation transcript:

1 C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference: Starting Out with C++, Tony Gaddis, 2 nd Ed.

2 C++ Programming, Namiq Sultan2 3.1The cin Object The cin object reads information types at the keyboard. cin is the standard input object Notice the >> and << operators appear to point in the direction information is flowing.

3 C++ Programming, Namiq Sultan3 Program 3-1 #include using namespace std; int main() { int length, width, area; cout <<"This program calculates the area of a 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 C++ Programming, Namiq Sultan4 Program Output This program calculates the area of a rectangle. What is the length of the rectangle? 10 [Enter] What is the width of the rectangle? 20 [Enter] The area of the rectangle is 200.

5 C++ Programming, Namiq Sultan5 Entering Multiple Values The cin object may be used to gather multiple values at once. cin >> length>> width;

6 C++ Programming, Namiq Sultan6 Reading Strings cin can read strings as well as numbers. Strings are stored in character arrays. char Company[12];

7 C++ Programming, Namiq Sultan7 Program 3-5 // 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 C++ Programming, Namiq Sultan8 Program Output What is your name? Charlie [Enter] Good morning Charlie

9 C++ Programming, Namiq Sultan9 Program 3-6 //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; }

10 C++ Programming, Namiq Sultan10 Program Output Enter your first and last names and I will reverse them. Johnny Jones [Enter] Jones, Johnny

11 C++ Programming, Namiq Sultan11 Notes on strings: If a character array is intended to hold strings, it must be at least one character larger than the largest string that will be stored in it. The cin object will let the user enter a string larger than the array can hold. If this happens, the string will overflow the array’s boundaries and destroy other information in memory. If you wish the user to enter a string that has spaces in it, you cannot use this input method.

12 C++ Programming, Namiq Sultan12 3.2 Mathematical Expressions C++ allows you to construct complex mathematical expressions using multiple operators and grouping symbols.

13 C++ Programming, Namiq Sultan13 Program 3-7 // 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() { float 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); return 0; }

14 C++ Programming, Namiq Sultan14 Program Output for Program 3-8 with Example Input This program shows the decimal value of a fraction. Enter the numerator: 3 [Enter] Enter the denominator: 6 [Enter] The decimal value is 0.1875

15 C++ Programming, Namiq Sultan15 Table 3-1 Precedence of Arithmetic Operators (Highest to Lowest) (unary negation) - * / % + -

16 C++ Programming, Namiq Sultan16 Table 3-2 Some Expressions

17 C++ Programming, Namiq Sultan17 Associativity If two operators sharing an operand have the same precedence, they work according to their associativity, either right to left or left to right

18 C++ Programming, Namiq Sultan18

19 C++ Programming, Namiq Sultan19 Converting Algebraic Expressions to Programming Statements

20 C++ Programming, Namiq Sultan20 No Exponents Please! C++ does not have an exponent operator. Use the pow() library function to raise a number to a power. Will need #include for pow() function. area = pow(4,2) // will store 4 2 in area

21 C++ Programming, Namiq Sultan21 Program 3-8 // 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 the 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); cout << "The area is " << area; return 0; }

22 C++ Programming, Namiq Sultan22 Program 3-8 Output With Example Input This program calculates the area of a circle. What is the radius of the circle? 10[Enter] The area is 314.159

23 C++ Programming, Namiq Sultan23 3.3 Type Conversion When an operator’s operands are of different data types, C++ will automatically convert them to the same data type.

24 C++ Programming, Namiq Sultan24 Type Conversion Rules: Rule 1: chars, shorts, and unsigned shorts are automatically promoted to int. Rule 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. Rule 3: When the final value of an expression is assigned to a variable, it will be converted to the data type of the variable.

25 C++ Programming, Namiq Sultan25 3.5 The Typecast Operator The typecast operator allows you to perform manual data type conversion. Val = int(number); //If number is a floating //point variable, it will be //truncated to an integer and //stored in the variable Val

26 C++ Programming, Namiq Sultan26 Program 3-11 #include using namespace std; int main() { int months, books; float perMonth; 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 = float(books) / months; cout << "That is " << perMonth << " books per month.\n"; return 0; }

27 C++ Programming, Namiq Sultan27 Program Output How many books do you plan to read? 30 [Enter] How many months will it take you to read them? 7 [Enter] That is 4.285714 books per month.

28 C++ Programming, Namiq Sultan28 Typecast Warnings In Program 3-11, the following statement would still have resulted in integer division: perMonth = float(books / months); Because the division is performed first and the result is cast to a float.

29 C++ Programming, Namiq Sultan29 Program 3-12 // This program uses a typecast operator to print // a character from a number. #include using namespace std; int main() { int number = 65; cout << number << endl; cout << char(number) << endl; return 0; }

30 C++ Programming, Namiq Sultan30 Program Output 65 A

31 C++ Programming, Namiq Sultan31 3.6 The Power of Constants Constants may be given names that symbolically represent them in a program.

32 C++ Programming, Namiq Sultan32 Program 3-13 // This program calculates the area of a circle. #include using namespace std; int main() { const float 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); cout << "The area is " << area; return 0; }

33 C++ Programming, Namiq Sultan33 The #define Directive The older C-style method of creating named constants is with the #define directive, although it is preferable to use the const modifier. #define PI 3.14159 is roughly the same as const float PI=3.14159;

34 C++ Programming, Namiq Sultan34 Program 3-14 #include #include // needed for pow function using namespace std; #define PI 3.14159 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 = PI * pow(radius, 2); cout << "The area is " << area; return 0; }

35 C++ Programming, Namiq Sultan35 3.7 Multiple Assignment and Combined Assignment Multiple assignment means to assign the same value to several variables with one statement. A = B = C = D = 12; Store1 = Store2 = Store3 = BegInv;

36 C++ Programming, Namiq Sultan36 Combined Assignment

37 C++ Programming, Namiq Sultan37 3.10 More Mathematical Library Functions

38 C++ Programming, Namiq Sultan38 Table 3-14

39 C++ Programming, Namiq Sultan39 Table 3-14 continued

40 C++ Programming, Namiq Sultan40 Table 3-14 continued

41 41 Program 3-32 // This program asks for the lengths of the 2 sides of a right triangle. // The length of the hypotenuse is then calculated and displayed. #include #include // For sqrt and pow using namespace std; int main() { float a, b, c; cout << "Enter the length of side A: "; cin >> a; cout << "Enter the length of side B: "; cin >> b; c = sqrt(pow(a, 2.0) + pow(b, 2.0)); cout << "The length of the hypotenuse is "; cout << c << endl; return 0; }

42 C++ Programming, Namiq Sultan42 Program Output Enter the length of side A: 5.0 [Enter] Enter the length of side B: 12.0 [Enter] The length of the hypotenuse is 13


Download ppt "C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin."

Similar presentations


Ads by Google