1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.

Slides:



Advertisements
Similar presentations
1 Demo Reading Assignments Important terms & concepts Fundamental Data Types Identifier Naming Arithmetic Operations Sample Programs CSE Lecture.
Advertisements

CS 1400 Chapter 2 sections 1, 2, 4 – 6, 8,
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Types and Variables. Computer Programming 2 C++ in one page!
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Data types and variables
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
Basic Elements of C++ Chapter 2.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
Input & Output: Console
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (4)
C Tokens Identifiers Keywords Constants Operators Special symbols.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
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.
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
C++ Programming: Basic Elements of C++.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects.
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.
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.
Programming Fundamentals
CS Jan 2007 Chapter 2 sections 1, 2, 4 – 6, 8,
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Programming in C++
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
Introduction to C++. 2 What Is a Program Made Of?
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
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
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Chapter 2: Introduction to C++
BASIC ELEMENTS OF A COMPUTER PROGRAM
Documentation Need to have documentation in all programs
Chapter 2: Introduction to C++
Chapter 2: Introduction to C++
2.1 Parts of a C++ Program.
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
What Actions Do We Have Part 1
Variables and Constants
Presentation transcript:

1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I

2 9/8/08CS150 Introduction to Computer Science 1 Today  Last we covered o main function o cout object o How data that is used by a program can be declared and stored  Today we will o Investigate the various types of data that C++ can handle

3 9/8/08CS150 Introduction to Computer Science 1 Declaration Statements  Variable declarations double area; double circ; double perimeter, volume;  Constant declaration const double PI = 3.14; const double RADIUS = 5.4;

4 9/8/08CS150 Introduction to Computer Science 1 Identifiers  Programmer-defined names that represent some element of a program  C++ limits on variable names: 1. Identifiers must begin with a letter or an underscore 2. Identifiers must consist of letters, numbers and underscore, nothing else 3. Identifiers cannot be a keyword page 42

5 9/8/08CS150 Introduction to Computer Science 1 Identifiers  Identifiers are case sensitive int totalCost; int TotalCost;  Use meaningful variable names width w

6 9/8/08CS150 Introduction to Computer Science 1 Identifiers  Q 4.1 Which of the following declarations are invalid and why? a. char Letter1; b. char 1letter; c. double inches, kms; d. double inches*num; e. int joe’s; f. Int cent_per_inch; g. double two-dimensional; h. char hello; i. int return; j. size int;

7 9/8/08CS150 Introduction to Computer Science 1 Data types  A data type defines: o how the computer interprets data in memory  What? What does memory really look like? o what is a byte? a bit?

8 9/8/08CS150 Introduction to Computer Science 1 Integers  The main integer data type is int  int s are finite (why?)  An int without a sign (+ or - ) is assumed to be positive  2,353 is not an int, 2353 is an int  Operations?

9 9/8/08CS150 Introduction to Computer Science 1 Integer Data Types  There are six integer data types, each with a different range and a different size o what does unsigned mean?  Range of data types is listed on page 44  see program 2-17 on page 58 for the above output

10 9/8/08CS150 Introduction to Computer Science 1 Variable Ranges TypeSizeValues short int 2 bytes–32,768 to 32,767 int 4 bytes–2,147,483,648 to 2,147,483,647 unsigned int 4 bytes0 to 4,294,967,295 long int 4 bytes–2,147,483,648 to 2,147,483,647  What is the range of an unsigned short ?  What data type should you use for a person’s age?  What data type should you use for the temperature on the moon?  What data type should you use for the size of a music file (mp3)? Why?

11 9/8/08CS150 Introduction to Computer Science 1 char  The char data type is used to store single characters (letters, digits, special characters) o ASCII  Character literals are enclosed in single quotes  Examples of character literals are: ‘A’, ‘a’, ‘*’, ‘2’, ‘$’

12 9/8/08CS150 Introduction to Computer Science 1 ASCII Character Set  page 1097  Decimal ValueCharacter 32 33! 65A 66B 67C 97a 98b 99c

13 9/8/08CS150 Introduction to Computer Science 1 Example // page 48, program 2-11 #include using namespace std; int main() { char letter; letter = 65; cout << letter << endl; letter = 66; cout << letter << endl; return 0; }

14 9/8/08CS150 Introduction to Computer Science 1 Program 4.2 // page 49, program 2-12 #include using namespace std; int main() { char letter; letter = 'A'; cout << letter << endl; letter = 'B'; cout << letter << endl; return 0; }

15 9/8/08CS150 Introduction to Computer Science 1 string Class  string is used to store a list of characters  To indicate the end of a string, a null terminator is used o why?  Need to include the preprocessor directive o #include o why?

16 9/8/08CS150 Introduction to Computer Science 1 Questions  Q 4.2 How are the character ‘A’ and the string constant “A” stored in memory?  Q 4.3 Is the escape character \n a character or a string?  Q 4.4 How do we declare a char variable and assign it a value?

17 9/8/08CS150 Introduction to Computer Science 1 string Questions  Q 4.5 How do we declare a variable of type string?  Q 4.6 How do we assign a value to the variable?  Q 4.7 How do we output a string constant and a string variable? What is output?

18 9/8/08CS150 Introduction to Computer Science 1 Floating-Point Data Types  float, double, long double o positive and negative o no unsigned float!  Scientific Notation  Examples: o 1.0, -2.3, -0.3, 12E5, -1E-2, 1.4e+8  2, is not a double  is a double

19 9/8/08CS150 Introduction to Computer Science 1 Variable Sizes  On my machine the sizes are

20 9/8/08CS150 Introduction to Computer Science 1 Variable Size Program // page 58, program 2-17 #include using namespace std; int main() { cout << "The size of an int is:\t\t" << sizeof(int) << " bytes.\n"; cout << "The size of a short int is:\t" << sizeof(short) << " bytes.\n"; cout << "The size of a long int is:\t" << sizeof(long) << " bytes.\n"; cout << "The size of a char is:\t\t" << sizeof(char) << " bytes.\n"; cout << "The size of a float is:\t\t" << sizeof(float) << " bytes.\n"; cout << "The size of a double is:\t" << sizeof(double) << " bytes.\n"; return 0; }

21 9/8/08CS150 Introduction to Computer Science 1 Variable Ranges TypeSizeValues int 4 bytes–2,147,483,648 to 2,147,483,647 short int 2 bytes–32,768 to 32,767 long int 4 bytes–2,147,483,648 to 2,147,483,647 unsigned int 4 bytes0 to 4,294,967,295 char 1 byte256 character values float 4 bytes±3.4e–38 to ± 3.4e38 double 8 bytes±1.7e–308 to ± 1.7e308

22 9/8/08CS150 Introduction to Computer Science 1 How to Choose a Numeric Data Type  Ask yourself the following questions o What are the largest and smallest numbers that may be stored? o How much memory does the variable use? o Is the variable signed (positive and negative)? o How many decimal places of precision does the variable need?

23 9/8/08CS150 Introduction to Computer Science 1 Problem  What variables will you need for the following program?  page 71, #4.  Write a program the computes the tax and tip on a restaurant bill. The user will enter the original bill and the tax rate. Assume a 15% tip. Display the tax amount, tip amount, and total bill on the screen.

24 9/8/08CS150 Introduction to Computer Science 1 Examples  Remember, the format for declaring variables is: o data-type identifier;  You can declare variables of the different data types as follows o int num1; o double num2; o char letter ;

25 9/8/08CS150 Introduction to Computer Science 1 Summary  In today’s lecture we covered o Identifiers o Data types o How data that is used by a program can be declared and stored  We have covered sections 2.7 – 2.12 of your textbook