Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Similar documents
Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Lab # 02. Basic Elements of C++ _ Part1

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

A First Program - Greeting.cpp

Computer Programming : C++

Outline. First Quiz Results. Exercise Five Goals. Question Three. Questions One and Two. Exercise five if statements February 28, 2006

Creating a C++ Program

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

6.096 Introduction to C++ January (IAP) 2009

IT 1033: Fundamentals of Programming Data types & variables

REVIEW. The C++ Programming Language. CS 151 Review #2

2 nd Week Lecture Notes

Lecture 3 Tao Wang 1

The C++ Language. Arizona State University 1

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Integer Data Types. Data Type. Data Types. int, short int, long int

Programming. C++ Basics

Computer Science II Lecture 1 Introduction and Background

Objectives. In this chapter, you will:

Chapter 2: Introduction to C++

Data Types & Variables

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

BTE2313. Chapter 2: Introduction to C++ Programming

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Introduction to Programming EC-105. Lecture 2

CS242 COMPUTER PROGRAMMING

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

CSc Introduction to Computing

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

C++ Programming: From Problem Analysis to Program Design, Third Edition

These are reserved words of the C language. For example int, float, if, else, for, while etc.

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

CHAPTER 3 BASIC INSTRUCTION OF C++

Fundamentals of Programming CS-110. Lecture 2

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Exercise: Using Numbers

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

Computer System and programming in C

Datatypes, Variables, and Operations

Unit 3. Constants and Expressions

Outline. Review Choice Statements. Review Sequential Flow. Review Choice Before Loops. Review Choice After Loops

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Engineering Problem Solving with C++, Etter/Ingber

Maciej Sobieraj. Lecture 1

Number Representation & Conversion

Information Science 1

VARIABLES & ASSIGNMENTS

Chapter 1 INTRODUCTION

Introduction to the C++ Programming Language

LECTURE 02 INTRODUCTION TO C++

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 2: Overview of C++

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

Getting started with C++ (Part 2)

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

COMP322 - Introduction to C++ Lecture 01 - Introduction

1. In C++, reserved words are the same as predefined identifiers. a. True

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Chapter 3, Selection. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved.

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Input And Output of C++

Outline. Why do we write functions? Introduction to Functions. How do we write functions? Using Functions. Introduction to Functions March 21, 2006

BITG 1233: Introduction to C++

Topic 2: C++ Programming fundamentals

Data types Expressions Variables Assignment. COMP1400 Week 2

Groups of two-state devices are used to represent data in a computer. In general, we say the states are either: high/low, on/off, 1/0,...

Understanding main() function Input/Output Streams

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

CS2141 Software Development using C/C++ C++ Basics

Introduction to C# Applications

Selection Statements. Pseudocode


Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

BASIC ELEMENTS OF A COMPUTER PROGRAM

The type of all data used in a C (or C++) program must be specified

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Lecture 3. Input and Output. Review from last week. Variable - place to store data in memory. identified by a name should be meaningful Has a type-

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Scientific Computing

WARM UP LESSONS BARE BASICS

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Preview from Notesale.co.uk Page 6 of 52

Chapter 2. Outline. Simple C++ Programs

Variables. Data Types.

Course Outline Introduction to C-Programming

UNIT- 3 Introduction to C++

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

Lecture 2 Tao Wang 1

printf( Please enter another number: ); scanf( %d, &num2);

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Transcription:

Data Types Declarations and Initializations Larry Caretto Computer Science 16 Computing in Engineering and Science February 7, 25 Outline Review last week Meaning of data types Integer data types have no decimal point and integer division truncates Floating point data types: approximate representation of decimal numbers Character, string and boolean (logical) data types 2 Review of Last Week Basic elements of C++ programs Template #include, using namespace std; int main(), return EXIT_SUCCESS; C++ is case sensitive White space does not matter except in string constants End statements with a semicolon (;) Braces { and } frame logical code blocks Will use in lab this week Review of Last Week II Screen output using cout and << Keyboard input using cin and >> Variables refer to memory locations Use letters, numbers and _ Start with letter or _ 31 characters maximum Use meaningful names Variables are case sensitive (x2 is not X2) 3 4 Review Variables and Memory Address Value Program variables refer to computer memory (RAM) locations When we use a variable (say x) we get the value in the memory location the compiler associated with this variable x = 3; // assigns the value 3 to x cout << x; // writes value of x to the screen y = x; // assigns value of x to y x = x + 3; // replaces x by x + 3 5 3.14926 Variable name Cells show memory address, associated variable name and value stored (if any) What is effect of y2 = PI * rad * rad? What happens to cell 16? to cells 12 and 14? Cell 16 gets the new value of π() 2 Cells 12 and 14 are not changed 6 1

3.14926 What is effect of _data = x1? The value of in cell 11 is stored in cell 17 What happens to the value of in cell 17? It is lost forever What happens to the value of in cell 11? Nothing 7 3.14926 What is effect of var = var +? What happens to cell 1? The value of is replaced by + =.7 Are any other cells affected? No 8 3.14926 What is effect of var = var + rad on cell 14? on cell 1? The value of in cell 14 does not change The value of in cell 1 is replaced by the result + =.7 9 3.14926 Can you do y2 = nam? This is a trick question. You have no basis for answering it yet. It depends on the type for y2. If y2 can hold strings the operation is okay If y2 cannot hold strings this is a syntax error 1 Data Types Computer memory stores binary information a string of ones and zeros How are these ones and zeros interpreted? Depends on data type Analogy How do you interpret the following string of characters? chair Depends on language 11 Integer Data Types There are several of these including int, the only one we will use in this course Integer data types have no decimal part When we divide two integers the decimal part is truncated (lost) Not rounded, but truncated What is 12/7? 1 What is 4/5? 12 2

Integer Data Types II Range of values for different types Based on number of bits in computer member allocated for type short -32,768 to 32,767 int -2,147,483,648 to 2,147,483,647 long -2,147,483,648 to 2,147,483,647 unsigned short to 65,535 unsigned int to 4,294,967,295 unsigned long to 4,294,967,295 Integer Data Types III Range depends on compiler vendor Ranges shown on previous page are common and are used in Visual C++ Only requirement for standard is that digits for long digits for int digits for short Range comes from binary storage 16 bits stores a number from to 2 16 1 This is actual range for unsigned short Short range is 2 to 2 1 14 Integer Data Types IV Floating Point Data Types What is result of following code? short result, maxshort = 32767; result = maxshort + 1; cout << Result = << result; Result is -32768, the smallest short integer What is result of code where you subtract 1 from minshort = -32768? Result, 34767, maximum short integer Numbers with decimal points Stored as characteristic plus mantissa Numbers not stored exactly Smallest type, float, occupies four bytes Range for float is -3.42823466x1 38 to -1.175494351x1-38,, and 1.175494351x1-38 to 3.42823466x1 38 16 Approximate Representation Floating Point Data Types II How do you represent 1/3 as decimal?.3,.33,.333,.3333333333333 or? Representation should be accurate enough for calculations but will never be exact Similar problem on computers Fractional numbers like 1/1 =.1 are represented as binary fractions 1/1 is.111111111111 2 1 *.1 will not be exactly one 17 Type float has about seven significant figures of accuracy When does 1 + ε = 1? When ε is so small that adding it to 1 does not change the significant figures available For float, ε = 1.19x1-7 Other floating point types are double and long double Both same in Visual C++ 18 3

Floating Point Data Types III Type double has about 16 significant figures of accuracy Range is -1.797693486238x1 38 to -2.2257385857214x1-38,, and 2.2257385857214x1-38 to 1.797693486238x1 38 For double, ε = 2.2x1-16 Generally use type double for modern engineering/science/mathematics codes 19 Other Data Types char data type represents single characters (constant example: a ) string data type represents a string of characters (constant example: string ) Requires #include<string> declaration bool data type used in logical operations Only two possible values for type bool variables: true and false 2 Constant Types Declaring Variables Program constants have types integer 37 is type int decimal 123.4 or 1.234e2 is type double Character (char) c, string This is a string constant The only place where spaces matter logical (boolean) true false Can force constants to other types by adding a letter (e.g. 37L is a long int) 21 All variables must be declared as belonging to a specific data type the first time they appear in the code Failure to do so is a syntax error Can initialize a variable with declaration (recommended practice) or subsequent to declaration Can declare several variables in a single statement 22 Examples Summary int x; // length int y; // width int z; // height double x, y; char YES = Y, NO = N ; bool converged = false; int maxiterations = 1; int x = 2, // length y = 4, // width z = 7; // height double radius = 1; char c = 9 ; string name = ; double maxallowederror = 1e-; 23 Variables represent memory locations Must declare variables before use Data types for variables Integer types (e.g., int) for counting Floating point types (e.g., double) for calculations Do not represent numbers exactly Other types are char (individual characters), string, and bool (true and false) 24 4

True-False Review Quiz You do not have to declare a variable before using it in a program False Declaring a variable is the same as assigning it an initial value False You can declare a variable and assign it a value at the same time True Subtracting one from the minimum short integer gives the maximum short True Assignments Reading pages in text Today: 45-61, 1-16 Thursday: 64-72, 93-98 Next Tuesday, February 14: 117-7 117-7 Recommended homework for this week: pages 75, problems 7 and 8; page 78, problem 27 Exercise one may be submitted late Exercise two due Thursday (9 Feb) 25 26 5