1 / 31

Chapter 2

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.

alesia
Download Presentation

Chapter 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2 Data types, declarations, and displays

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

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

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

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

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

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

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

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

  10. Integer division • 5 / 3 yields 1 (quotient) • 99 / 100 yields 0 (quotient) • 5 % 3 yields 2 (remainder) • 99 % 100 yields 99 (remainder) • etc.

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

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

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

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

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

  16. Example: Version 1; printout not properly aligned #include <iostream.h> { cout << 6 << endl << 18 << endl << 124 << endl << “---\n” << (6 + 18 + 124) << endl; return 0; }

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

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

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

  20. Format flags for use with setiosflags( ) on page53 • ios::showpoint • ios::showpos • ios::fixed • ios::scientific • ios::dec • ios::oct • ios::left • ios::right

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

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

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

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

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

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

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

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

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

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

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

More Related