42
C++ Programming, Namiq Sultan 1 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.

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

Embed Size (px)

Citation preview

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

C++ Programming, Namiq Sultan 1

Chapter 3 Expressions and Interactivity

Namiq Sultan

University of Duhok

Department of Electrical and Computer Engineerin

Reference: Starting Out with C++, Tony Gaddis, 2nd Ed.

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

C++ Programming, Namiq Sultan 2

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

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

C++ Programming, Namiq Sultan 3

Program 3-1

#include <iostream>

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;

}

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

C++ Programming, Namiq Sultan 4

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.

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

C++ Programming, Namiq Sultan 5

Entering Multiple Values

• The cin object may be used to gather multiple values at once.

cin >> length>> width;

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

C++ Programming, Namiq Sultan 6

Reading Strings

• cin can read strings as well as numbers.

• Strings are stored in character arrays.

char Company[12];

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

C++ Programming, Namiq Sultan 7

Program 3-5

// This program demonstrates how cin can read a

// string into a character array

#include <iostream>

using namespace std;

int main()

{

char name[21];

cout << "What is your name? ";

cin >> name;

cout << "Good morning " << name << endl;

return 0;

}

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

C++ Programming, Namiq Sultan 8

Program Output

What is your name? Charlie [Enter]

Good morning Charlie

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

C++ Programming, Namiq Sultan 9

Program 3-6

// This program reads two strings into two character arrays.

#include <iostream>

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;

}

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

C++ Programming, Namiq Sultan 10

Program Output

Enter your first and last names and I will

reverse them.

Johnny Jones [Enter]

Jones, Johnny

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

C++ Programming, Namiq Sultan 11

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.

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

C++ Programming, Namiq Sultan 12

3.2 Mathematical Expressions

• C++ allows you to construct complex mathematical expressions using multiple operators and grouping symbols.

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

C++ Programming, Namiq Sultan 13

Program 3-7

// This program asks the user to enter the numerator// and denominator of a fraction and it displays the decimal value#include <iostream>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;}

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

C++ Programming, Namiq Sultan 14

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

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

C++ Programming, Namiq Sultan 15

Table 3-1 Precedence of Arithmetic Operators (Highest to Lowest)

(unary negation) -

* / %

+ -

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

C++ Programming, Namiq Sultan 16

Table 3-2 Some Expressions

Expression Value 5 + 2 * 4 13

10 / 2 - 3 2

8 + 12 * 2 - 4 28

4 + 17 % 2 - 1 4

6 - 3 * 2 + 7 - 1 6

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

C++ Programming, Namiq Sultan 17

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

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

C++ Programming, Namiq Sultan 18

Table 3-3 Associativity of Arithmetic Operators Operator Associativity (unary negation) - Right to left

* / % Left to right

+ - Left to right

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

C++ Programming, Namiq Sultan 19

Converting Algebraic Expressions to Programming Statements

Algebraic Expression

Operation C++ Equivalent

6B 6 times B 6 * B

(3)(12) 3 times 12 3 * 12

4xy 4 times x times y 4 * x * y

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

C++ Programming, Namiq Sultan 20

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 <math.h> for pow() function.

area = pow(4,2) // will store 42 in area

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

C++ Programming, Namiq Sultan 21

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

#include <math.h> // 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;

}

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

C++ Programming, Namiq Sultan 22

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

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

C++ Programming, Namiq Sultan 23

3.3 Type Conversion

• When an operator’s operands are of different data types, C++ will automatically convert them to the same data type.

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

C++ Programming, Namiq Sultan 24

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.

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

C++ Programming, Namiq Sultan 25

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

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

C++ Programming, Namiq Sultan 26

Program 3-11#include <iostream>

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;

}

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

C++ Programming, Namiq Sultan 27

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.

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

C++ Programming, Namiq Sultan 28

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.

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

C++ Programming, Namiq Sultan 29

Program 3-12

// This program uses a typecast operator to print

// a character from a number.

 

#include <iostream>

using namespace std;

int main()

{

int number = 65;

cout << number << endl;

cout << char(number) << endl;

return 0;

}

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

C++ Programming, Namiq Sultan 30

Program Output

65A

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

C++ Programming, Namiq Sultan 31

3.6 The Power of Constants

• Constants may be given names that symbolically represent them in a program.

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

C++ Programming, Namiq Sultan 32

Program 3-13

// This program calculates the area of a circle.#include <iostream>#include <math.h>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;

}

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

C++ Programming, Namiq Sultan 33

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;

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

C++ Programming, Namiq Sultan 34

Program 3-14

#include <iostream>

#include <math.h> // 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;

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

C++ Programming, Namiq Sultan 35

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;

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

C++ Programming, Namiq Sultan 36

Combined Assignment

Operator Example Usage Equivalent To += x += 5; x = x + 5;

-= y -= 2; y = y - 2;

*= z *= 10; z = z * 10;

/= a /= b; a = a / b;

%= c %= 3; c = c % 3;

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

C++ Programming, Namiq Sultan 37

3.10 More Mathematical Library Functions

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

C++ Programming, Namiq Sultan 38

Table 3-14

ab s Example Usage: y = abs(x);

Description Returns the absolute value of the argument. The argument and the return value are integers.

cos Example Usage: y = cos(x);

Description Returns the cosine of the argument. The argument should be an angle expressed in radians. The return type and the argument are doubles.

exp Example Usage: y = exp(x);

Description Computes the exponential function of the argument, which is x . The return type and the argument are doubles.

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

C++ Programming, Namiq Sultan 39

Table 3-14 continuedfmod Example Usage: y = fmod(x, z);

Description Returns, as a double, the remainder of the first argument divided by the second argument. Works like the modulus operator, but the arguments are doubles. (The modulus operator only works with integers.) Take care not to pass zero as the second argument. Doing so would cause division by zero.

log Example Usage: y = log(x);

Description Returns the natural logarithm of the argument. The return type and the argument are doubles.

log10 Example Usage: y = log10(x);

Description Returns the base-10 logarithm of the argument. The return type and the argument are doubles.

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

C++ Programming, Namiq Sultan 40

Table 3-14 continued

sin Example Usage: y = sin(x);

Description Returns the sine of the argument. The argument should be an angle expressed in radians. The return type and the argument are doubles.

sqrt Example Usage: y = sqrt(x); Description Returns the square root of the argument. The return type and

argument are doubles.

tan Example Usage: y = tan(x); Description Returns the tangent of the argument. The argument should

be an angle expressed in radians. The return type and the argument are doubles.

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

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

#include <math.h> // 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;

}

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

C++ Programming, Namiq Sultan 42

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