Exercise: Using Numbers

Similar documents
Main Memory Organization

COMP2611: Computer Organization. Data Representation

Lecture 2 Tao Wang 1

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

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

CSc Introduction to Computing

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

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

CS242 COMPUTER PROGRAMMING

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

Basic data types. Building blocks of computation

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

Chapter 2 Basic Elements of C++

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

Chapter 2: Introduction to C++

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

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

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

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

Reserved Words and Identifiers

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

Objectives. In this chapter, you will:

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

LECTURE 02 INTRODUCTION TO C++

CHAPTER 3 Expressions, Functions, Output

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

Computer System and programming in C

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Unit 3. Constants and Expressions

Relational Operators and if. Class 10

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

Variables and Constants

BTE2313. Chapter 2: Introduction to C++ Programming

Review of Important Topics in CS1600. Functions Arrays C-strings

Exercise: Inventing Language

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

VARIABLES AND TYPES CITS1001

CHAPTER 3 BASIC INSTRUCTION OF C++

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

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

OWEN COMPUTER SCIENCE LESSONS L2RVW4 Pg 1. Lesson 2 Review #4. Matching. Match each description to the correct data type.

CSI33 Data Structures

Topic 2: Introduction to Programming

IT 1033: Fundamentals of Programming Data types & variables

DEPARTMENT OF MATHS, MJ COLLEGE

Chapter 6 Primitive types

Fundamental Data Types

ARG! Language Reference Manual

UNIT- 3 Introduction to C++

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

PROGRAMMING FUNDAMENTALS

Input File Syntax The parser expects the input file to be divided into objects. Each object must start with the declaration:

LECTURE 3 C++ Basics Part 2

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

Full file at


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

CS112 Lecture: Primitive Types, Operators, Strings

Getting started with Java

1. C++ Overview. C++ Program Structure. Data Types. Assignment Statements. Input/Output Operations. Arithmetic Expressions.

Full file at

Creating a C++ Program

World Inside a Computer is Binary

Data Types & Variables

Chapter 1 INTRODUCTION

Bits, Words, and Integers

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

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

B.V. Patel Institute of BMC & IT, UTU 2014

Introduction to Programming EC-105. Lecture 2

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

Visual C# Instructor s Manual Table of Contents

Lesson 2 Variables and I/O

Bits. Binary Digits. 0 or 1

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

More about Binary 9/6/2016

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

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

6.096 Introduction to C++ January (IAP) 2009

Fundamental of Programming (C)

BASIC ELEMENTS OF A COMPUTER PROGRAM

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

The Java Language Rules And Tools 3

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

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Programming in C++ 6. Floating point data types

Chapter 7. Basic Types

Data Types Literals, Variables & Constants

3. Java - Language Constructs I

A First Program - Greeting.cpp

Fundamentals of Programming

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

1.3b Type Conversion

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Transcription:

Exercise: Using Numbers Problem: You are a spy going into an evil party to find the super-secret code phrase (made up of letters and spaces), which you will immediately send via text message to your team to prevent, oh, let s say a volcano from destroying the Earth. Unfortunately, you re a poor spy and your phone can only type digits (0-9). It can t even type spaces! Quickly! Before you are sent in, figure out what you can tell your team that will allow you to get the code phrase out to them.

Main Memory Organization Bit Smallest piece of memory Stands for binary digit Has values 0 (off) or 1 (on) Byte Is 8 consecutive bits Word Usually 4 consecutive bytes Has an address 8 bits 0 1 1 0 0 1 1 1 1 byte 1-2

Data Storage All data is stored in binary bits in main memory The meaning of a set of bits depends on the encoding Encoding determines the length of a meaningful chunk We use bytes (8-bits) as our most common unit Encoding also determines how those bits should be read 8-bit Binary 8-bit Integer 8-bit Character (ASCII) 0100 0001 65 A 0100 0010 66 B 0100 0011 67 C

Data Types and Conversion The data type assigned to a literal or variable tells the computer how it is encoded That is, how to interpret it You can also tell the computer to treat a literal or variable with a different encoding This is called type casting or type coercion static_cast<datatypename>( expression )

Simple Data Types Three categories of simple data Integral: integers (numbers without a decimal) Floating-point: decimal numbers Enumeration type: user-defined data type 5

Simple Data Types (continued) Integral data types are further classified into nine categories: 6

Examples: -6728 0 78 +763 int Data Type Positive integers do not need a + sign No commas are used within an integer Commas are used for separating items in a list 7

char Data Type The smallest integral data type Used for characters: letters, digits, and special symbols Each character is enclosed in single quotes 'A', 'a', '0', '*', '+', '$', '&',' ' A blank space is a character 8

bool type bool Data Type Two values: true and false Manipulate logical (Boolean) expressions true and false are called logical values bool, true, and false are reserved words 9

Simple Data Types Different compilers may allow different ranges of values 10

Overflow and Underflow Occurs when assigning a value that is too large (overflow) or too small (underflow) to be held in a variable Variable contains value that is wrapped around the set of possible values 3-11

Overflow Example // Create a short int initialized to // the largest value it can hold short int num = 32767; cout << num; // Displays 32767 num = num + 1; cout << num; // Displays -32768 3-12

Handling Overflow and Underflow Different systems handle the problem differently. They may display a warning / error message display a dialog box and ask what to do stop the program continue execution with the incorrect value 3-13

Floating-Point Data Types C++ uses scientific notation to represent real numbers (floating-point notation) 14

Floating-Point Data Types (continued) float: represents any real number Range: -3.4E+38 to 3.4E+38 (four bytes) double: represents any real number Range: -1.7E+308 to 1.7E+308 (eight bytes) On most newer compilers, data types double and long double are same 15

Floating-Point Data Types (continued) Maximum number of significant digits (decimal places) for float values is 6 or 7 Maximum number of significant digits for double is 15 Precision: maximum number of significant digits Float values are called single precision Double values are called double precision 16