chapter 2

31
Chapter 2 Data types, declarations, and displays

Upload: alesia

Post on 04-Jan-2016

31 views

Category:

Documents


1 download

DESCRIPTION

Chapter 2. Data types, declarations, and displays. Computer data processing. Data processing: the process of using a computer to convert raw data (unprocessed or unorganized) data into information (processed data). Data types. Numeric (dimensionless) integer type: 450, 25000, -99, etc. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 2

Chapter 2

Data types, declarations, and displays

Page 2: Chapter 2

Computer data processing

• Data processing: the process of using a computer to convert raw data (unprocessed or unorganized) data into information (processed data).

Page 3: Chapter 2

Data types

• Numeric (dimensionless)– integer type: 450, 25000, -99, etc.

– floating point type• Ordinary notation: 3.1415, 6.99, -12000., etc.

• Exponential notation: for very large or very small values such as 6.25e9, -3.5e-10, etc.

– Character type: a single character enclosed in a pair of single quote such as ‘a’, ‘Z’, ‘$’, etc. In C/C++ system, a character is internally treated/represented as an small integer, e.g., ‘a’ is actually 97 ‘A’ is 65.

Page 4: Chapter 2

Data types continued

– Character type: although C/C++ treats a single character as an integer, each character is encoded using a group of eight bits following a coding standard known as ASCII (American Standard for Information Interchange) of ANSI (American National Standard Institute). For example, when you hit ‘a’ key, a group of eight bits 01100001 goes into the memory (see p38).

– Boolean or logical type: recent C/C++ standard supports Boolean type. A Boolean data type allows only two different values of either true or false. Again, Boolean values are treated by C/C++ as two small integer values --- 1 for true and 0 for false.

Page 5: Chapter 2

Data types continued

– Character type: there are a small set of characters known as escape sequence (or escape characters) including ‘\n’ (a new line character), ‘\t’ (next tab stop character), etc. (see Tab 2-3 on p39). Although there are two characters enclosed in a pair of single quotes, the two characters are treated as a single character or more precisely, a small integer, ‘\n’ is 10 and ‘\t’ is 9. Note that escape character such as ‘\n’ and ‘\t’ play important role in output operation.

– String type: a series or a list of characters enclosed in a pair of double quotes such as “William Paterson Univ”, “Net Income”, etc.

Page 6: Chapter 2

How is main memory (RAM) organized and accessed

• RAM is byte-addressable (each byte has a unique address) with a one-dimension or linear memory space, i.e., one byte after another.

• How many bytes are needed to store an integer, a floating point number, a character, or a string?

Page 7: Chapter 2

Size of storage or memory needed to store a piece of data– Integer type

• short: 2 types • int: 2 types • long: 4 types

– Floating• float: 4 bytes • double: 8 bytes • long double: 10 bytes

It is noted that the larger the size of storage, the wider the range and more precise can a value be stored. Also noted is the fact that the sizes are dependent on the compiler! The numbers quoted are for Turbo C/C++ compiler.

Page 8: Chapter 2

Arithmetic operators

• + (addition), - (subtraction), * (multiplication), / (division), % (modulo division). Note that except for modulo division operator which operate on integers, all other four operator operate on either integer or floating point types of data.

• Property of an operator– arity: number of operands operated upon by the operator.– priority or precedence: the order of operations when two

or more operators appear in a statement or expression.– associativity: either left-to-right or right-to-left.

Page 9: Chapter 2

Arithmetic operators continued

• All operators are binary operators (with arity = 2) since each operates on two operand such as 4 * 5, 8 – 6, etc.

• the – operator can be a unary negation operator (one operand) as in –25, where – simply mean negative 25.

• The associativity is left-to-right meaning 5 - 3 is actually 5 minus 3, not 3 minus 5.

• *, /, and % are of the same priority and are higher than + and -, meaning 2 + 3 * 5 is equal to 17 instead of 25! Note that precedence can be overridden with parentheses.

Page 10: Chapter 2

Integer division

• 5 / 3 yields 1 (quotient)

• 99 / 100 yields 0 (quotient)

• 5 % 3 yields 2 (remainder)

• 99 % 100 yields 99 (remainder)

• etc.

Page 11: Chapter 2

Mixed mode operation and data type promotion

• In an arithmetic expression, there are two or more operands of different types (a mix of integer and floating point types)

Example:

2 + 3.5 (2 will be promoted to double type before computation will take place)

4.85 – 3.1415926L (4.85 will be promoted to long

double type before computation will proceed) etc.

Page 12: Chapter 2

Output using cout• Data flows like a stream (one character after another) to or out

of a C++ program.• cout is an ostream (output stream) object (it is helpful to view it

as an output stream); it is used together with the insertion operator <<, which inserts whatever follows it to the output stream.

Example:cout << “Hello World!” << endl;cout << (2 + 3) << endl;cout << “The first alphabet is “ << ‘a’ << endl;cout << “The total of 6 and 15 is “ << (6 + 15); etc…

Page 13: Chapter 2

The insertion operator << is “overloaded”

• Notice the insertion operation recognizes and inserts (or operates on) different types data item onto the output stream (which flows from the program to a printer or monitor); the insertion operator is said to be overloaded.

Page 14: Chapter 2

Formatted output

• For simple formatting, one can use escape sequence such as ‘\n’ (newline) and/or ‘\t’ (next tap stop).

• For more sophisticated formatting, one must use stream manipulators which are defined by the C/C++ system.

Page 15: Chapter 2

Commonly used stream manipulators

• setw( n ): set field width to n• setprecision( n ): set floating point precisin

to n places• setiosflag( flags ): set the format flags• dec: display a value in decimal notation• hex: display a value in hexadecimal

notation• oct: display a value in octal notation

Page 16: Chapter 2

Example: Version 1; printout not properly aligned

#include <iostream.h>

{

cout << 6 << endl << 18 << endl << 124

<< endl << “---\n” << (6 + 18 + 124) << endl;

return 0;

}

Page 17: Chapter 2

Example: Version 2; printout of integers is now properly aligned with setw(n)

#include <iostream.h>#include <iomanip.h>{ cout << setw(3) << 6 << endl

<< setw(3) << 18 << endl setw(3) << 124 << endl

<< “---\n” << (6 + 18 + 124) << endl; return 0;}// Note: setw(n) is non-persistent; integer right-justified in the

specified print field

Page 18: Chapter 2

Formatting floating point numbers

…cout << ‘|’ << setw(10) << setiosflags( ios::fixed )

<< setprecision(3) << 25.67 << ‘|’;…

The above output instruction displays:| 25.670|

Note that the setprecision manipulator defaults to 6 decimal places.

Page 19: Chapter 2

More examples on the effect of format manipulators are shown in table 2-8,

page 52

Page 20: Chapter 2

Format flags for use with setiosflags( ) on page53

• ios::showpoint• ios::showpos• ios::fixed• ios::scientific• ios::dec• ios::oct• ios::left• ios::right

Page 21: Chapter 2

A program that illustrates output conversions

#include <iostream.h>

#include <iomanip.h>

int main()

{

cout << “The decimal value of 15 is “ << 15 << endl

<< “The octal value of 15 is “

<< setiosflags(ios::oct) << 15 << endl

<< “The hexadecimal value of 15 is “

<< setiosflags(ios::hex) << 15 << endl;

return 0;

}

Page 22: Chapter 2

A simpler version#include <iostream.h>

#include <iomanip.h>

int main()

{

cout << “The decimal value of 15 is “ << 15 << endl

<< “The octal value of 15 is “

<< oct << 15 << endl

<< “The hexadecimal value of 15 is “

<< hex << 15 << endl;

return 0;

}

Page 23: Chapter 2

Variable and its declaration• A variable is a name (identifier) that identifies a

memory location in which a value (a piece of data) can be stored.

• It is helpful to visualize a memory location as a box, and the size of the box depends on data type, e.g., 1 byte for char type, 2 bytes for int type, and 8 bytes for double type.

• The variable name is associated with the actual physical address of the location in RAM.

• the variable name can be arbitrarily chosen by the programmer subjected to the following restrictions: (next slide)

Page 24: Chapter 2

Variable name• It is case-sensitive!• It must not be a keyword!• It must begin with a letter or an underscore• It cannot contain more than 31 characters• It must not have embedded space.• Upper and lower case alphabets, digits (0 through 9) and

some special characters such as underscore _ are allowed.• It is desirable to choose meaningful names, e.g., a

variable name tax would presumably be used to store tax value(s).

Page 25: Chapter 2

Declaration of variables

• A variable must be declared before it can be used. It is declared according to the following format:

data-type variable-name;

where data-type is a reserved word and variable is an identifier chosen by the programmer. Valid data types include char, int, double, etc.

Page 26: Chapter 2

Data types

– Integer: short, int, long– Floating-point: float, double, long double– Character: char– Boolean or logic: bool

Example:

char gender;

int age;

double interestRate, principal;

Page 27: Chapter 2

The assignment operator =

• Remember that a variable is a named memory location in which a value can be stored. So, once a variable is declared, a value can be assigned to or stored in it through the use of the assignment operator. The format is as follows:

variable = value;

The entire line above is known as an assignment statement

Page 28: Chapter 2

Example: compute the average of three test scores

#include <iostream.h>int main( ){ float grade1, grade2, total, average;

grade1 = 85.5;grade2 = 97.0;total = grade1 + grade2;average = total / 2.0;cout << “The average grade is “ << average << endl;return 0;

}

Page 29: Chapter 2

Example: character variable

#include <iostream.h>int main(){ char ch;

ch = ‘a’;cout << “The character stored in ch is << ch << endl;ch = ‘m’;cout << “The character now stored in ch is “ << ch;return 0;

}

Page 30: Chapter 2

Reference variable: additional name or alias of the same variable

• Once a variable has been defined, it may be given additional names. It is accomplished by using a reference declaration:

data-type &new-name = existing-name;

Example:float total;float &sum = total

Page 31: Chapter 2

Program involving reference variable

#include <iostream.h>int main(){ float total = 20.5; // declare and initialize

float &sum = total;cout << “Sum = “ << sum << endl;sum = 18.6;cout << “Total = “ << total << endl;return 0;

}