Chapter 2 Data types, declarations, and displays.

Slides:



Advertisements
Similar presentations
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Advertisements

Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
I/O and Program Control Statements Dick Steflik. Overloading C++ provides two types of overloading –function overloading the ability to use the same function.
Data types and variables
The Fundamentals of C++ Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
String Escape Sequences
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Operaciones y Variables
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Input & Output: Console
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
Chapter 2: Using Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
The Fundamentals of C++ Chapter 2: Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Lesson 3 – Primitive Data Types Objective: Students will investigate the definition and usage of objects and primitive data types in Java in order to practice.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
CSC 107 – Programming For Science. Announcements.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Data Types Declarations Expressions Data storage C++ Basics.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
ITEC113 Algorithms and Programming Techniques
Revision Lecture
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Chapter 2: Introduction to C++.
The Fundamentals of C++
Programming Fundamental-1
Presentation transcript:

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. –floating point type Ordinary notation: , 6.99, , 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 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 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 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.

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 is actually 5 minus 3, not 3 minus 5. *, /, and % are of the same priority and are higher than + and -, meaning * 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 will be promoted to double type before computation will take place) 4.85 – L (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 hexadecimal notation oct: display a value in octal notation

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

Example: Version 2; printout of integers is now properly aligned with setw(n) #include { cout << setw(3) << 6 << endl << setw(3) << 18 << endl setw(3) << 124 << endl << “---\n” << ( ) << endl; return 0; } // Note: setw(n) is non-persistent; integer right- justified in the specified print field

Formatting floating point numbers … cout << ‘|’ << setw(10) << setiosflags( ios::fixed ) << setprecision(3) << << ‘|’; … The above output instruction displays: | | Note that the setprecision manipulator defaults to 6 decimal places.

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

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

A program that illustrates output conversions #include 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; }

A simpler version #include 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; }

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)

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

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.

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;

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

Example: compute the average of three test scores #include 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; }

Example: character variable #include 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; }

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

Program involving reference variable #include 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; }