Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Expressions and Interactivity: Namiq Sultan

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 36

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.

C++ Programming, Namiq Sultan 1


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.

C++ Programming, Namiq Sultan 2


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;
}

C++ Programming, Namiq Sultan 3


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.

C++ Programming, Namiq Sultan 4


Entering Multiple Values

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


once.

cin >> length>> width;

C++ Programming, Namiq Sultan 5


3.2 Mathematical Expressions

• C++ allows you to construct complex mathematical


expressions using multiple operators and grouping
symbols.

C++ Programming, Namiq Sultan 6


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;
}
C++ Programming, Namiq Sultan 7
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.5

C++ Programming, Namiq Sultan 8


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

(unary negation) -
* / %
+ -

C++ Programming, Namiq Sultan 9


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

C++ Programming, Namiq Sultan 10


Associativity
• When we combine operators to form expressions, the order in
which the operators are to be applied is determined by its
associativity. For example,

a+b+c means (a+b)+c + is left to right associativity
a=b=c means a=(b=c) = is right to left associativity

• All arithmetic operators +, -, *, /, and % are left to right


associativity

C++ Programming, Namiq Sultan 11


Converting Algebraic Expressions to Programming Statements

Algebraic Operation C++ Equivalent


Expression
6B 6 times B 6*B

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

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

C++ Programming, Namiq Sultan 12


Integer Division

• When both operands of a division statement are integers, the


statement will perform integer division. This means the result of
the division will be an integer as well. If there is a remainder, it
will be discarded. For example, in the following statement, parts is
assigned the value 5:
parts = 17 / 3;
• If you want to make regular division, express one of the numbers
as a floating-point number.
parts = 17.0 / 3;
• The result of the division is 5.66667.

C++ Programming, Namiq Sultan 13


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

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

C++ Programming, Namiq Sultan 14


Program 3-8
// This program calculates the area of a circle.
#include <iostream>
#include <cmath>
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;
}
C++ Programming, Namiq Sultan 15
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

C++ Programming, Namiq Sultan 16


3.3 Type Conversion

• When an operator’s operands are of different data types,


C++ will automatically convert them to the same data
type.

C++ Programming, Namiq Sultan 17


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.

C++ Programming, Namiq Sultan 18


Type Conversion Example
char c;
int i;
float f;
double d;
result = ( ch / i ) + ( f * d ) - ( f + i )
int double float

int double float

double
C++ Programming, Namiq Sultan 19
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

C++ Programming, Namiq Sultan 20


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;
}

C++ Programming, Namiq Sultan 21


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.

C++ Programming, Namiq Sultan 22


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.

C++ Programming, Namiq Sultan 23


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;
}

C++ Programming, Namiq Sultan 24


Program Output

65
A

C++ Programming, Namiq Sultan 25


3.6 The Power of Constants

• Constants may be given names that symbolically represent


them in a program.

C++ Programming, Namiq Sultan 26


Program 3-13
// This program calculates the area of a circle.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
const float pi = 3.14159;
double area, radius;
cout << "What is the radius of the circle? ";
cin >> radius;
area = pi * pow(radius,2);
cout << "The area is " << area;
return 0;
}
C++ Programming, Namiq Sultan 27
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;

C++ Programming, Namiq Sultan 28


Program 3-14
#include <iostream>
#include <cmath> // needed for pow function
using namespace std;
#define PI 3.14159
int main()
{
double area, radius;
cout << "What is the radius of the circle? ";
cin >> radius;
area = PI * pow(radius, 2);
cout << "The area is " << area;
return 0;
}
 

C++ Programming, Namiq Sultan 29


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;

C++ Programming, Namiq Sultan 30


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;

C++ Programming, Namiq Sultan 31


3.10 More Mathematical Library Functions

C++ Programming, Namiq Sultan 32


Table 3-14
abs 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.

C++ Programming, Namiq Sultan 33


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.

C++ Programming, Namiq Sultan 34


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
#include <iostream>
#include <cmath> // For sqrt and pow
using namespace std;
int main()
{ float a, b, c;
  cout << "Enter the length of side A and side B: ";
cin >> a;
cin >> b;
c = sqrt(pow(a, 2.0) + pow(b, 2.0));
cout << "The length of the hypotenuse is " << c << endl;
return 0;
}
35
Program Output
Enter the length of side A and B: 5 12 [Enter]
The length of the hypotenuse is 13

C++ Programming, Namiq Sultan 36

You might also like