PF - 5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 39

CS1002 – Programming

Fundamentals
5
2 Data Types

 Data type: Set of values together with a set of operations


 C++ data types fall into three categories:
3 Simple Data Types

Three categories of simple data


 Integral: integers (numbers without a decimal)
 Floating-point: decimal numbers
 Enumeration type: user-defined data type
4 Simple Data Types (cont'd.)

Integral data types are further classified into nine categories:


 char, short, int, long, bool
 unsigned char, unsigned short, unsigned int, unsigned long

A signed integer is a 32-bit datum that encodes an integer in the range


[-2147483648 to 2147483647].

An unsigned integer is a 32-bit datum that encodes a nonnegative


integer in the range [0 to 4294967295].
5 Simple Data Types (cont'd.)

 Different compilers may allow different ranges of values

Data Type Values Storage (in Bytes)


int -2147483648 to 2147483647 4
bool true and false 1
char -128 to 127 1
6 int Data Type
Examples:
-6728
0
78
+763
 Positive integers do not need a + sign
 No commas are used within an integer [76,385]
 Commas are used for separating items in a list
7 bool Data Type

 bool type

 Two values: true and false

 Manipulate logical (Boolean) expressions


 true and false
 Logical values
 bool, true, and false
 Reserved words
8 char Data Type
 The smallest integral data type
 Used for characters: letters, digits, and special symbols
 Each character is enclosed in single quotes
 'A', 'a', '0', '*', '+', '$', '&'
 A blank space is a character
 Written ' ', with a space left between the single quotes
 ‘abc’ and ‘!=‘ are not char
9 Floating-Point Data Types

 You may be familiar with scientific notation. For example:


 43872918 = 4.3872918 * 107 {10 to the power of seven}
 .0000265 = 2.65 * 10-5 {10 to the power of minus five}
 47.9832 = 4.79832 * 101 {10 to the power of one}
10 Floating-Point Data Types
 C++ uses scientific notation to represent real numbers (floating-point
notation)

Real Number C++ Floating-Point


Notation

75.924 7.592400E1
0.18 1.800000E-1
0.0000453 4.530000E-5
-1.482 -1.482000E0
7800.0 7.800000E3
11 Floating-Point Data Types (cont'd.)

 float: represents any real number


 Range: -3.4E+38 to 3.4E+38 (four bytes)
 double: represents any real number
 Range: -1.7E+308 to 1.7E+308 (eight bytes)
12 Floating-Point Data Types (cont'd.)
 Maximum number of significant digits (decimal places) for float
values is 6 or 7
 Maximum number of significant digits for double is 15
 Precision: Maximum number of significant digits
 float values are called single precision
 double values are called double precision
13 Data type and variables
 When we declare a variable, we not only specify the variable we
also specify what type of data a variable can store. A syntax rule
to declare a variable

datatype identifier;

 For example, consider the following examples:

int counter;
double interestRate;
char grade;
14 Datatypes and their ranges
Type Width(bits) Common Range
char 8 -128 to 127
unsigned char 8 0 to 255
int 32 –2,147,483,648 to
2,147,483,647
unsigned int 32 0 to 4,294,967,295
short int 16 -32768 to 32767
unsigned short int 16 0 to 65535
long int 64 -2,147,483,648 to
2,147,483,647
unsigned long int 64 0 to 4,294,967,295
float 32 3.4E-38 to 3.4E+38
double 64 1.7E-308 to 1.7E+308
long double 80 3.4E-4932 to 3.4E+4932
15 string Type

 Programmer-defined type supplied in ANSI/ISO Standard C++ library


 Sequence of zero or more characters
 Enclosed in double quotation marks
 Null: a string with no characters
 Each character has relative position in string
 Position of first character is 0
 Length of a string is number of characters in it
 Example: length of "William Jacob" is 13
16 Using the string Data Type in a Program

 To use the string type, you need to access its definition from the header file
string
 Include the following preprocessor directive:
#include <string>
17 Using string datatype
String Position of a Character in the String Length of String
----------------------------------------------------------------------------------
“Test String” Position of ‘T’ is 0 11
Position of ‘i’ is 8
Position of ’ ’ (the space) is 4
Position of ‘S’ is 5
Position of ‘g’ is 10

“String” Position of ‘S’ is 0 6


Position of ‘t’ is 1
Position of ‘r’ is 2
Position of ‘i’ is 3
Position of ‘n’ is 4
Position of ‘g’ is 5

When determining the length of a string, you must also count any spaces in the string. For
example, the length of the following string is 22
“It is a beautiful day.”
18 Arithmetic Operators and Operator
Precedence
C++ arithmetic operators:
 + addition
 - subtraction
 * multiplication
 / division
 % modulus operator
 +, -, *, and / can be used with integral and floating-point data types
 Operators can be unary or binary
19 Example
Arithmetic Results Description
Expression

5/2 2 In the division 5 / 2, the quotient is 2 and the remainder is


1. Therefore, 5 / 2 with the integral operands evaluates
to the quotient, which is 2.
14 / 7 2 In the division 14 / 7, the quotient is 2 and remainder is 0.
Therefore, 14 / 7 with integral operands evaluates to 2

34 % 5 4 In the division 34 / 5, the quotient is 6 and the remainder


is 4. Therefore, 34 %5 with the integral operands
evaluates to the remainder, which is 4.
4%6 4 In the division 4 / 6, the quotient is 0 and the remainder is
4. Therefore, 4 % 6 with the integral operands evaluates
to the remainder, which is 4.
20 Example (Do it yourself)

5.0 + 3.0
3.0 + 9.4
16.3 - 5.2
4.2 * 2.5
5.0 / 2.0
34.5 / 6.0
34.5 / 6.5
21 Example
#include <iostream>
using namespace std;
int main()
{
cout << "5.0 + 3.0 = " << 5.0 + 3.0 << endl;
cout << "3.0 + 9.4 = " << 3.0 + 9.4 << endl;
cout << "16.3 - 5.2 = " << 16.3 - 5.2 << endl;
cout << "4.2 * 2.5 = " << 4.2 * 2.5 << endl;
cout << "5.0 / 2.0 = " << 5.0 / 2.0 << endl;
cout << "34.5 / 6.0 = " << 34.5 / 6.0 << endl;
cout << "34.5 / 6.5 = " << 34.5 / 6.5 << endl;

return 0;
}
22 C++ Math Operator Rules
 * : Multiplication
 / : Division
 Integer division truncates remainder
 7 / 5 evaluates to 1
 % : Modulus operator returns remainder
 7 % 5 evaluates to 2
 + : Addition
 - : Subtraction

Operator Operation (s) Order of evaluation (precedence)


() Parentheses Evaluated first. If the parentheses are nested,
the expression in the innermost pair is evaluated
first. If there are several pairs of parentheses
“on the same level” (i.e., not nested), they are
evaluated left to right.
*, / or % Multiplication, Evaluated second. If there are several, they are
Division, Modulus evaluated left to right.
+ or - Addition, Evaluated last. If there are several, they are
Subtraction evaluated left to right
23 Order of Precedence
 All operations inside of () are evaluated first
 *, /, and % are at the same level of precedence and are evaluated next
 + and – have the same level of precedence and are evaluated last
 When operators are on the same level
 Performed from left to right (associativity)
 3 * 7 - 6 + 2 * 5 / 4 + 6 means
(((3 * 7) – 6) + ((2 * 5) / 4 )) + 6
24 Example
25 Example
26 Expressions
 If all operands are integers
 Expression is called an integral expression
 Yields an integral result
 Example: 2 + 3 * 5

 If all operands are floating-point


 Expression is called a floating-point expression
 Yields a floating-point result
 Example: 12.8 * 17.5 - 34.50
27 Examples
 Consider the following C++ integral expressions:
2+3*5
3+x-y/7
 x + 2 * (y - z) + 18
 In these expressions, x, y, and z represent variables of the
integer type; that is, they can hold integer values
28 Examples

 Consider the following C++ floating-point expressions:


 12.8 * 17.5 - 34.50
 x * 10.5 + y - 16.2
 Here, x and y represent variables of the floating-point
type; that is, they can hold floating-point values
29 Mixed Expressions
 Mixed expression:
 Has operands of different data types
 Contains integers and floating-point
 Examples of mixed expressions:
 2 + 3.5
 6 / 4 + 3.9
 5.4 * 2 – 13.6 + 18 / 2
30 Mixed Expressions (cont'd.)
Evaluation rules:
Rule # 1
a. If operator has same types of operands
 Evaluated according to the type of the operands
b. If operator has both types of operands
 Integer is changed to floating-point with decimal part “0”
 Operator is evaluated
 Result is floating-point
Rule # 2
 Entire expression is evaluated according to precedence rules
31 Examples
32 Examples Solution
33 Exercise
 Solve this expression on notebook
 10 * 5 + 100/10 – 5 + 7 % 2
10 * 5 + 100 / 10 – 5 + 7 % 2
50 + 100 / 10 – 5 + 7 % 2
50 + 10 - 5 + 7 % 2
60 - 5 + 7 % 2
55 + 7 % 2
55 + 1
56
34 Exercise Solution
 Solve this expression on notebook
 10 * 5 + 100/10 – 5 + 7 % 2
10 * 5 + 100 / 10 – 5 + 7 % 2
50 + 100 / 10 – 5 + 7 % 2
50 + 10 - 5 + 7 % 2
60 - 5 + 7 % 2
55 + 7 % 2
55 + 1
56
35 Example (Do yourself)
 3 / 2 + 5.5
 15.6 / 2 + 5
 4 + 5 / 2.0
 4 * 3 + 7 / 5 – 25.5
36 Example
37 Example (Integers)

2+5=7
13 + 89 = 102
34 - 20 = 14
45 - 90 = -45
2 * 7 = 14
5/2=2
14 / 7 = 2
34 % 5 = 4
4%6=4
38 Example (Float)

5.0 + 3.5 = 8.5


3.0 + 9.4 = 12.4
16.3 - 5.2 = 11.1
4.2 * 2.5 = 10.5
5.0 / 2.0 = 2.5
34.5 / 6.0 = 5.75
34.5 / 6.5 = 5.30769
39

Thank you

You might also like