WPU CS 2300 - Data types declarations and displays

Unformatted text preview:

Chapter 2Computer data processingData typesData types continuedSlide 5How is main memory (RAM) organized and accessedSize of storage or memory needed to store a piece of dataArithmetic operatorsArithmetic operators continuedInteger divisionMixed mode operation and data type promotionOutput using coutThe insertion operator << is “overloaded”Formatted outputCommonly used stream manipulatorsExample: Version 1; printout not properly alignedExample: Version 2; printout of integers is now properly aligned with setw(n)Formatting floating point numbersMore examples on the effect of format manipulators are shown in table 2-8, page 52Format flags for use with setiosflags( ) on page53A program that illustrates output conversionsA simpler versionVariable and its declarationVariable nameDeclaration of variablesSlide 26The assignment operator =Example: compute the average of three test scoresExample: character variableReference variable: additional name or alias of the same variableProgram involving reference variableChapter 2 Data types, declarations, and displaysComputer 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.–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.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.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.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?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 bytesIt 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.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.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.Integer division•5 / 3 yields 1 (quotient)•99 / 100 yields 0 (quotient)•5 % 3 yields 2 (remainder)•99 % 100 yields 99 (remainder)•etc.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.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…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.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.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


View Full Document

WPU CS 2300 - Data types declarations and displays

Download Data types declarations and displays
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Data types declarations and displays and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Data types declarations and displays 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?