Numbers. John Perry. Spring 2017

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

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

CS242 COMPUTER PROGRAMMING

CSI33 Data Structures

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

BITG 1233: Introduction to C++

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Introduction to Programming EC-105. Lecture 2

A First Program - Greeting.cpp

Chapter 2 Basic Elements of C++

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

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

Programming. C++ Basics

Introduction to Programming

Fundamentals of Programming CS-110. Lecture 2

LECTURE 3 C++ Basics Part 2

UNIT- 3 Introduction to C++

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

Chapter 3 - Functions

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

Computer Programming : C++

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

Chapter 1 Introduction to Computers and C++ Programming

Introduction to Programming

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

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

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

Chapter 2: Basic Elements of C++

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

DEPARTMENT OF MATHS, MJ COLLEGE

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

Basics of Java Programming

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

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

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

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

CHAPTER 3 BASIC INSTRUCTION OF C++

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

Operations. Making Things Happen

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

Add Subtract Multiply Divide

Chapter 2: Overview of C++

Introduction to C++ Systems Programming

Chapter 2. C++ Basics

CSCE 110 PROGRAMMING FUNDAMENTALS

Functions and Recursion

Chapter 15 - C++ As A "Better C"

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Chapter 2: Introduction to C++

Chapter 2: Using Data

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

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

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

Lecture 2 Tao Wang 1

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

6.096 Introduction to C++ January (IAP) 2009

Creating a C++ Program

Bits, Words, and Integers

A SHORT COURSE ON C++

C++ Programming Lecture 11 Functions Part I

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

Tutorial-2a: First steps with C++ programming

Data types, variables, constants

Fundamentals of Programming

Chapter 2. Outline. Simple C++ Programs

Objectives. In this chapter, you will:

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Basic Types, Variables, Literals, Constants

Topic 2: C++ Programming fundamentals

The C++ Language. Arizona State University 1

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

Informatik I (D-ITET) Übungsstunde 5, Hossein Shafagh

Relational Operators and if. Class 10

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

BASIC ELEMENTS OF A COMPUTER PROGRAM

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit.

LECTURE 02 INTRODUCTION TO C++

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

Scientific Computing

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Introduction to C ++

This watermark does not appear in the registered version - Slide 1

Chapter 2 C++ Fundamentals

Computing and Statistical Data Analysis Lecture 3

Engineering Problem Solving with C++, Etter/Ingber

Types, Values, Variables & Assignment. EECS 211 Winter 2018

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

Exercise: Using Numbers

Zheng-Liang Lu Java Programming 45 / 79

2 nd Week Lecture Notes

EEE145 Computer Programming

CHAPTER 3 Expressions, Functions, Output

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

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

Transcription:

Basic MAT 685: C++ Numbers University of Southern Msississippi Spring 2017

Outline Basic 1 2 Basic 3 4

Outline Basic 1 2 Basic 3 4

Variable? Basic Name representing data in computer s memory first character? upper-/lower- case letter underscore avoid upper-case, underscore next characters? upper-/lower-case letter underscore digit cannot be a keyword (word w/special meaning to C++) Examples center_x, center_y, _my_data

Type? Specifies data s characteristics: what kind Basic

Type? Basic Specifies data s characteristics: what kind Machine boolean numerical character pointer

Type? Basic Specifies data s characteristics: what kind Machine boolean numerical character pointer Structured array enumeration structured record union class

Type systems Basic weak type system variable s type ill-defined, changeable introduce variables without specifying type type can change flexible, interactive BASIC, Python, Sage

Type systems Basic weak type system variable s type ill-defined, changeable introduce variables without specifying type type can change flexible, interactive BASIC, Python, Sage strong type system variable s type carefully checked well-defined before use type cannot change typically fast C++, Eiffel, Fortran can abuse via cast or conversion

Outline Basic 1 2 Basic 3 4

Outline Basic 1 2 Basic 3 4

Depends on bit length Basic l: bit length short l 16 int l short 16 long l int 32 long long l long 64

Depends on bit length Basic l: bit length short l 16 int l short 16 long l int 32 long long l long 64 T smallest range is [ 2 l, 2 l 1 ) ( signed ) unsigned T change range to [ 0, 2 l+1 1 )

Example 1 Basic int a; unsigned long b; Questions What values can a contain? What values can b contain?

Example 1 Basic int a; unsigned long b; Questions What values can a contain? 2 16 a 2 16 What values can b contain?

Example 1 Basic int a; unsigned long b; Questions What values can a contain? 2 16 a 2 16 What values can b contain? 0 a 2 33 1

Example 2 Basic #include <iostream> using std::cout; using std::endl; int main() { long x, y; x = 3; y = 4; } cout << x << " + " << y << " = "; cout << x + y << endl; return 0;

Type matters! Basic 2 16 = 65536 > 1000 = 10 3 if you multiply two small integers, you can get a larger one product must fit in type of destination! Overflow Mathematical operation w/larger result than allowed by type

Example of overflow Basic #include <iostream> using std::cout; using std::endl; int main() { short thousand = 1000; short million = thousand * thousand; } cout << "According to this computer, "; cout << thousand << " squared is\n"; cout << "\t" << million << endl;

Result on my home computer Basic $./a.out According to this computer, 1000 squared is 16960

Outline Basic 1 2 Basic 3 4

Floating-point numbers Basic no exact represention of real approximation by floating point a 10 b slower, inexact, but well-specified no overflow, but division by small numbers problematic Example 1e+06 = 1 10 6

Real type names Basic float machine-dependent double no less precise than float long double no less precise than double

Example of non-overflow Basic #include <iostream> using std::cout; using std::endl; int main() { float thousand = 1000; float million = thousand * thousand; } cout << "According to this computer, "; cout << thousand << " squared is\n"; cout << "\t" << million << endl;

Result on my home computer Basic $./a.out According to this computer, 1000 squared is 1e+06

Complex type names Basic #include <complex> using std::complex; complex <T> varname; where T is another numerical type

Complex type names Basic #include <complex> using std::complex; complex <T> varname; complex<double> yer usual complex type complex<long> Gaussian integers where T is another numerical type

Complex type names Basic #include <complex> using std::complex; complex <T> varname; where T is another numerical type complex<double> yer usual complex type complex<long> Gaussian integers (templated type, discussed later)

Too long? typedef it Basic Typing complex<t> repeatedly is tiresome! typedef T N; Defines N as a shortcut for T

Too long? typedef it Basic Typing complex<t> repeatedly is tiresome! typedef T N; Defines N as a shortcut for T Place outside program block, preferably immediately after #include s.

Basic Program 2.7 (pp. 23 24, slightly modified) Example (p. 1/2) Listing 1: complex_demo.cpp #include <complex> using std::complex; #include <iostream> using std::cout; using std::endl; typedef complex<double> CC; int main() { CC x(3,4); // define x = 3+4i CC z; // define z to be complex z = CC(2,7); // assign z = 2+7i CC i(0,1); // define i = sqrt(-1)

Basic Program 2.7 (pp. 23 24, slightly modified) Example (p. 2/2) cout << "z = " << z << endl; cout << "x = " << x << endl; cout << "z + x = " << z + x << endl; cout << "z*x = " << z*x << endl; cout << "z/x = " << z/x << endl; z = 5. - 4.*i; } cout << "Now z = " << z << endl; cout << "The real part of z is " << z.real() << "\nand the imaginary part is " << z.imag() << endl; return 0;

Example, run on my computer Basic $./a.out z = (2,7) x = (3,4) z + x = (5,11) z*x = (-22,29) z/x = (1.36,0.52) Now z = (5,-4) The real part of z is 5 and the imaginary part is -4

Outline Basic 1 2 Basic 3 4

bool and char Basic bool value can be true or false old style: 1 (true) or 0 (false) output displayed in old style

bool and char Basic bool value can be true or false old style: 1 (true) or 0 (false) output displayed in old style char character enclosed in single quotes 256 possibilities, defined by ASCII standard many the usual ones: a, Y, 1, _ includes escape codes: '\n', '\t', others

bool and char Basic bool value can be true or false old style: 1 (true) or 0 (false) output displayed in old style char character enclosed in single quotes 256 possibilities, defined by ASCII standard many the usual ones: a, Y, 1, _ includes escape codes: '\n', '\t', others string sequence of char enclosed in double quotes

Example Basic #include <iostream> using std::cout; using std::endl; #include <string> using std::string; int main() { bool truth = 1; bool same_truth = true; const string message = "Is the truth the same truth? "; cout << message << (truth == same_truth) << endl; }

Result on my home computer Basic $./a.out Is the truth the same truth? 1

Points to ponder Basic const <type> <variable_name> const tells the compiler that a variable will not change (truth == same_truth) == discussed below; parentheses needed for order of displays 1, not true or even t or T

Outline Basic 1 2 Basic 3 4

Outline Basic 1 2 Basic 3 4

Basic operation usage notes addition a + b watch for overflow subtraction a - b watch for overflow multiplication a * b watch for overflow division a / b integers? quotient only modular division a % b remainder can be negative

Basic #include <iostream> using std::cin; using std::cout; using std::endl; int main() { int a, b; Example cout << "Enter the first number --> "; cin >> a; cout << "Enter the second number --> "; cin >> b; cout << a << " % " << b << " = "; cout << a % b << endl; } return 0;

Result on my home computer Basic $./a.out Enter the first number --> 5 Enter the second number --> -3 5 % -3 = 2 $./a.out Enter the first number --> -5 Enter the second number --> 3-5 % 3 = -2 $./a.out Enter the first number --> -5 Enter the second number --> -3-5 % -3 = -2

Operate and assign Basic operation usage notes increment by 1 ++a or a++ pre- or postincrement decrement by 1 a or a-- pre- or postdecrement increment by b a += b result in a; watch for overflow decrement by b a -= b result in a; watch for overflow dilate by b a *= b result in a; watch for overflow contract by b a /= b result in a; integers? quotient only modular division a %= b result in a; remainder can be negative

Basic Pre- vs. Post- in/decrement? ++a increments a before using it a++ increments a after using it

Basic Pre- vs. Post- in/decrement? ++a increments a before using it a++ increments a after using it #include <iostream> using std::cout; using std::endl; int main() { int a; a = 10; cout << ++a << endl; a = 10; cout << a++ << endl; return 0; }

Basic Pre- vs. Post- in/decrement? ++a increments a before using it a++ increments a after using it #include <iostream> using std::cout; using std::endl; int main() { int a; a = 10; cout << ++a << endl; a = 10; cout << a++ << endl; return 0; } $./a.out 11 10

Basic Exponentiation? Not a basic operator. Use library functions: function usage notes e b exp(b) best to use double for result a b pow(a,b) best to use double for result

Basic Exponentiation? Not a basic operator. Use library functions: function usage notes e b exp(b) best to use double for result a b pow(a,b) best to use double for result #include <iostream> using std::cout; using std::endl; #include <cmath> using std::pow; int main() { double e = exp(1.); double pi = M_PI; } cout << "e to the pi is " << exp(pi) << endl; cout << "pi to the e is " << pow(pi, e) << endl;

Basic Exponentiation? Not a basic operator. Use library functions: function usage notes e b exp(b) best to use double for result a b pow(a,b) best to use double for result #include <iostream> using std::cout; using std::endl; #include <cmath> using std::pow; int main() { double e = exp(1.); double pi = M_PI; } cout << "e to the pi is " << exp(pi) << endl; cout << "pi to the e is " << pow(pi, e) << endl; $./a.out e to the pi is 23.1407 pi to the e is 22.4592

Numerical comparisons Basic Return true or false depending on values of a and b comparison usage notes equal? a == b two equals signs; forgetting can be catastrophic! different? a!= b what we call a b smaller? a <= b what we call a b strictly smaller? a < b strictly larger? a > b larger? a >= b what we call a b

Outline Basic 1 2 Basic 3 4

Old style (book) Basic Return true or false depending on values of a and b comparison usage notes equal? a == b two equals signs different? a!= b logical negation?!a what we call a or a logical and? a && b true iff both true logical or? a b true iff at least one true logical xor? a ^ b true iff exactly one true

New style (clearer) Basic Return true or false depending on values of a and b comparison usage notes equal? a == b two equals signs different? a!= b logical negation? not a what we call a or a logical and? a and b true iff both true logical or? a or b true iff at least one true logical xor? a xor b true iff exactly one true

Basic #include <iostream> using std::cout; using std::endl; int main() { bool yes = true; bool no = false; Example cout << "yes = " << yes << "; no = " << no << endl; cout << "not yes? " << not yes << endl; cout << "not no? " << not no << endl; cout << "yes and no? " << (yes and no) << endl; cout << "yes or no? " << (yes or no) << endl; cout << "yes xor no? " << (yes xor no) << endl; return 0; }

Result on my home computer Basic $./a.out yes = 1; no = 0 not yes? 0 not no? 1 yes and no? 0 yes or no? 1 yes xor no? 1

Outline Basic 1 2 Basic 3 4

Basic C++ strongly typed basic : numerical, boolean, character, pointer numerical allow for exact or approximate arithmetic many basic available some common require math library

Homework Basic pp. 28 29 #2.1 2.8