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

Similar documents
Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

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

Number Systems, Scalar Types, and Input and Output

Fundamentals of Programming CS-110. Lecture 2

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

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

LECTURE 02 INTRODUCTION TO C++

CHAPTER 3 BASIC INSTRUCTION OF C++

Chapter 2: Introduction to C++

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

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Exercise: Using Numbers

Introduction to Programming EC-105. Lecture 2

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

Types, Variables, and Constants

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Getting started with C++ (Part 2)

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

A First Program - Greeting.cpp

2 nd Week Lecture Notes

CS1500 Algorithms and Data Structures for Engineering, FALL Virgil Pavlu, Jose Annunziato,

BITG 1233: Introduction to C++

Unit 3. Constants and Expressions

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of 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++

Input And Output of C++

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

Chapter 2: Overview of C++

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

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

The C++ Language. Arizona State University 1

Maciej Sobieraj. Lecture 1

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

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

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

Basic data types. Building blocks of computation

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

The C++ Language. Output. Input and Output. Another type supplied by C++ Very complex, made up of several simple types.

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

Computer Programming : C++

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

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

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

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

CSc Introduc/on to Compu/ng. Lecture 17 Edgardo Molina Fall 2011 City College of New York

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

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

Objectives. In this chapter, you will:

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

Basic Definition INTEGER DATA. Unsigned Binary and Binary-Coded Decimal. BCD: Binary-Coded Decimal

Increment and the While. Class 15

do { statements } while (condition);

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

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

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

causing a set of statements (the body) to be executed repeatedly. C++ provides three control structures to support iteration (or looping).

Your First C++ Program. September 1, 2010

X Language Definition

Lecture 2 Tao Wang 1

Lab 6. Review of Variables, Formatting & Loops By: Dr. John Abraham, Professor, UTPA

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Exam 1 Practice CSE 232 Summer 2018 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

Building on the foundation. Now that we know a little about cout cin math operators boolean operators making decisions using if statements

Creating a C++ Program

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

CSCI 6610: Review. Chapter 7: Numbers Chapter 8: Characters Chapter 11 Pointers

Introduction and basic C++ programming

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Integer Representation Floating point Representation Other data types

CS107, Lecture 2 Bits and Bytes; Integer Representations

WARM UP LESSONS BARE BASICS

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

Chapter 2 - Control Structures

Operations. Making Things Happen

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

To become familiar with array manipulation, searching, and sorting.

BASIC ELEMENTS OF A COMPUTER PROGRAM

Programming. C++ Basics

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

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

Chapter 1 INTRODUCTION

Input and Output. Data Processing Course, I. Hrivnacova, IPN Orsay

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

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

Introduction to Programming using C++

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

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

CS 261 Fall Mike Lam, Professor Integer Encodings

CHAPTER 3 Expressions, Functions, Output

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

FILE IO AND DATA REPRSENTATION. Problem Solving with Computers-I

CSc Introduction to Computing

Java Notes. 10th ICSE. Saravanan Ganesh

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

Transcription:

THE INTEGER DATA TYPES

STORAGE OF INTEGER TYPES IN MEMORY All data types are stored in binary in memory. The type that you give a value indicates to the machine what encoding to use to store the data in binary. Example: bool bval = true; stored as 1 int ival = 1 stored as 00000000000000000000000000000001

THE BOOL TYPE For example, the bool type has only two possible values: 0 and 1. Since they are already binary, they are stored as a 0 or a 1. C++ has literal constants to represent each: false = 0 true = 1

THE INT TYPE The int type is stored in memory with 4 bytes of memory One byte = 8 bits A bit is a binary digit -- i.e. 0 or 1 So an integer is made up of 32 bits We will work with the char type for examples, since they only have 8 bits.

BINARY NUMBERS A positive number is represented as a binary number (or base-2 number). You can convert binary numbers to base-10, or vice versa. Example 1: Change 1234 10 to a binary number. Example 2: Change 10011001 2 to a base 10 number

POSITIVE AND NEGATIVE NUMBERS An int can be a positive or negative number, so there must be a method to represent both. The method that is used is called Two s Complement. Converting a positive base-10 number to two s complement is just converting it to binary. To convert a negative base-10 number to two s complement: 1. you convert the positive number 2. you flip all the bits (0s to 1s, 1s to 0s) 3. you add 1

EXAMPLE 3 Convert -5342 10 to a binary number stored in 16 bits.

TWO S COMPLEMENT TO BASE 10 If your number starts with a 0, it is positive, and the conversion is standard. If the number starts with a 1, it is negative: flip all bits add 1 Example 4: Convert 10101010 2 to base-10.

RANGE OF VALUES The range of values that an integer can represent depends on the number of bits it is using. bool: 1 bit char: 8 bits short: 16 bits long or int: 32 bits long long: 64 bits double: 64 bits

RANGE OF VALUES Since each bit can take 2 different values, and integer type can store 2 * 2 * 2 *... * 2 = 2 n different values Half of them are negative Half are positive (including 0)

EXAMPLE 5 What is the range of values that you can represent with a bool short int long long

UNSIGNED TYPES If you put the word unsigned before a type, then the type will only represent positive values. This will give you twice as many positive values, and could be useful for something like a counter. Ex: unsigned int counter = 0;

EXAMPLE 6 What is the range of integers that can be stored in the following types? unsigned char; 0 to 256 unsigned int; 0 to about 4 billion

INTEGER OVERFLOW What happens if you end up with a value that doesn t fit in the type? Example 7: char value = 120; char value2 = value + value;

INTEGER OVERFLOW: DEFINITION (WIKIPEDIA) an integer overflow occurs when an arithmetic operation attempts to create a numeric value that is larger than can be represented within the available storage space

SOME USEFUL CONSTANTS Some useful constants are found in <climits> and can be referred to in your programs: INT_MAX INT_MIN CHAR_MAX CHAR_MIN SHRT_MAX SHRT_MIN

CONSIDER THE FOLLOWING CODE #include <iostream> #include <climits> using namespace std; What is this code doing? Printing powers of 2 When will it stop? never int main() { int val = 1; cout << val << endl; while ( val * 2 <= INT_MAX ) { val *= 2; cout << val << endl; cin.get(); this makes the program pause and wait for the user to enter something } }

CORRECT THE EXIT CONDITIONS OF THE LOOP #include <iostream> #include <climits> using namespace std; int main() { int val = 1; cout << val << endl; while ( val <= INT_MAX / 2) { val *= 2; cout << val << endl; cin.get(); } }

EXAMPLE 8: WHEN ARE THESE CONDITIONS TRUE? HOW COULD THEY BE FIXED? int val; function will wrap around just work as a math equation: if ( val + 8 > INT_MAX )... (val > INT_MAX 8) while( 2*val 3 <= INT_MAX )... (val <= INT_MAX/2 + 3/2 ) if ( val * val > INT_MAX )... (val > sqrt(int_max)) while ( val * -5 >= INT_MIN )... (val > INT_MIN/-5)

MORE USEFUL INTEGER TIDBITS A useful manipulator for booleans is boolalpha bool b = true; cout << b << endl; cout << boolalpha << b << endl; cout << noboolalpha << b << endl; Remember to #include <iomanip> Output: 1 true 1

ESCAPE SEQUENCES (USING THE \ CHARACTER) A few chars that you might want to use: \n newline (like endl, but will work faster) \t tab (in general, better to use setw) \ quotation marks cout << I am bored the string ends before b here cout << I am \ bored\ better \ single quotes

SOME CODE TO PRINT THE LETTERS OF THE ALPHABET for ( char letter = A ; letter < Z ; letter++ ) { cout << letter << endl; } Since char is an integer type, you can use integer operations like +

HOW CHARS ARE ENCODED The char type uses the ASCII encoding. It is 8 bits, so there are a total of 256 different characters that it can represent. This is good enough for the English alphabet, but for internationalization, they created Unicode (16 bits) In C++ you would use wchar for this.

CAN ONLY USE WITH INTEGER TYPES: % double a = 5; cout << a % 2 << endl; compiler error

CAN ONLY USE WITH INTEGER TYPES: SWITCH STATEMENTS int card_val; cin >> card_val; if ( card_val == 1 ) { cout << A << endl; } else if ( card_val == 11 card_val == 12 card_val == 13 ) { cout << Face Card << endl; } else { cout << card_val << endl; } The next slide will have the equivalent switch statement

EXAMPLE: THE SWITCH STATEMENT int card_val; cin >> card_val; switch( card_val ) { } case 1: // executed for card_val == 1 cout << A << endl; break; case 11: // keep going until the break is reached case 12: // keep going until the break is reached case 13: // keep going until the break is reached cout << Face Card << endl; // printed for 11, 12, 13 break; default: // executed for any other card cout << card_val << endl; break;

A COMMON USE char user_input; cout << Enter Y or y to... << endl; cin >> user_input; switch ( user_input ) { case Y : case y : // do something here break; case N : case n : // do something else break; default: // do default action you can just treat it like n // or make them reenter } We should always include a default action