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

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

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

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

3.1 The cin Object. Expressions & I/O. Console Input. Example program using cin. Unit 2. Sections 2.14, , 5.1, CS 1428 Spring 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.

3.1. Chapter 3: The cin Object. Expressions and Interactivity

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

Functions. 6.1 Modular Programming. 6.2 Defining and Calling Functions. Gaddis: 6.1-5,7-10,13,15-16 and 7.7

Straight-line code (or IPO: Input-Process-Output) If/else & switch. Relational Expressions. Decisions. Sections 4.1-6, , 4.

Tutorial 3 Concepts for A1

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

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

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

Engineering Problem Solving with C++, Etter/Ingber

LECTURE 02 INTRODUCTION TO C++

Arrays. Array Data Type. Array - Memory Layout. Array Terminology. Gaddis: 7.1-4,6

Searching, Sorting & Analysis

3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity

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

Arrays. Array Data Type. Array - Memory Layout. Array Terminology. Gaddis: 7.1-3,5

3.1. Chapter 3: The cin Object in Program 3-1. Displaying a Prompt 8/23/2014. The cin Object

Chapter 3: Expressions and Interactivity

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

Chapter 2. Outline. Simple C++ Programs

Objectives. In this chapter, you will:

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

Chapter 3: Expressions and Interactivity. Copyright 2012 Pearson Education, Inc. Thursday, October 9, 14

PIC 10A. Lecture 3: More About Variables, Arithmetic, Casting, Assignment

Chapter 3. Numeric Types, Expressions, and Output

The C++ Language. Arizona State University 1

UNIT- 3 Introduction to C++

CS103L FALL 2017 UNIT 1: TYPES, VARIABLES,EXPRESSIONS,C++ BASICS

BITG 1233: Introduction to C++

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Expressions, Input, Output and Data Type Conversions

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

A First Program - Greeting.cpp

Structures. Data Types Structures. Data Types (C/C++) Gaddis: A Data Type consists of: Unit 7. example: Integer. CS 1428 Spring 2018

CSCE 110 PROGRAMMING FUNDAMENTALS

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

Chapter 2: Introduction to C++

Introduction to C++ (Extensions to C)

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

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

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

I/O Streams and Standard I/O Devices (cont d.)

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Chapter 3 - Notes Input/Output

Lecture 3 Tao Wang 1

CHAPTER 3 Expressions, Functions, Output

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-

VARIABLES & ASSIGNMENTS

CHAPTER 3 BASIC INSTRUCTION OF C++

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

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM

Chapter Two: Fundamental Data Types

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

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Week 3: File I/O and Formatting 3.7 Formatting Output

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

2 nd Week Lecture Notes

CS 1428 Review. CS 2308 :: Spring 2016 Molly O Neil

Introduction to Programming

Window s Visual Studio Output

Professor: Alvin Chao

Input and Expressions Chapter 3 Slides #4

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

Basic Concepts. Lexical Conventions

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

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

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

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

l Tree: set of nodes and directed edges l Parent: source node of directed edge l Child: terminal node of directed edge

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

Getting started with C++ (Part 2)

(created by professor Marina Tanasyuk) FUNCTIONS

9 Using Equation Networks

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

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

Ch 6. Functions. Example: function calls function

ANSI C Programming Simple Programs

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Programming Language. Functions. Eng. Anis Nazer First Semester

Introduction to C++ Dr M.S. Colclough, research fellows, pgtas

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

Week 4. Pointers and Addresses. Dereferencing and initializing. Pointers as Function Parameters. Pointers & Structs. Gaddis: Chapters 9, 11

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

Programming. C++ Basics

Operations. Making Things Happen

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Program Organization and Comments

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

Summary of basic C++-commands

Chapter 2: Overview of C++

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

Chapter 2. C++ Basics

Transcription:

Week 1 Operators, Data Types & I/O Gaddis: Chapters 1, 2, 3 CS 5301 Fa 2018 Ji Seaman Programming A program is a set of instructions that the computer foows to perform a task It must be transated from a programming anguage (C++) to machine code in order to run on the machine. 1 2 Structure of a C++ Program Heo word: In genera: //This program outputs a message to the screen #incude <iostream> using namespace std; int main() { cout << Heo word! << end; } //This is a comment #incude <incudefie>... using namespace std; int main() { statements... } Variabes, Data Types Variabe: portion of memory that stores a vaue Identifier: name of a program eement Fundamenta data types short int ong ong Variabe Decaration statement datatype identifier; Variabe Initiaization statement: datatype identifier = constant; boo char hours; int count = 0; 3 4

Integer types Integers are whoe numbers such as 12, 7, and -99 Data Type Range short -32,768 to 32,767 int -2,147,483,648 to 2,147,483,647 ong -2,147,483,648 to 2,147,483,647 char type stores characters such as A, @, and 9 The ascii code vaue (an integer) of the character is stored in memory. 5 Foating-point types (and boo) Foating point types store rea numbers such as 12.45 and -3.8 They are stored using scientific notation. Data Type ong boo type stores vaues that are true or fase fase is 0, true is 1. Range ±3.4E-38 to ±3.4E38 ±1.7E-308 to ±1.7E308 ±1.7E-308 to ±1.7E308 6 Constants Literas (specific vaue of a given type) Assignment statement, expressions To change the vaue of a variabe: 1 75-2 12.45-3.8 6.25e-5 true fase A 2 Named Constants: variabe whose vaue cannot be changed const datatype identifier = constant; const TAX_RATE = 0.0675; 7 The efthand side must be a variabe The righthand side is an expression of the right type What is an expression? variabe = expression; count = 10; an expression has a type and evauates to a vaue itera named constant variabe arithmetic expression etc. 8

Arithmetic Operations arithmetic operators: + addition - subtraction * mutipication / division % moduo (remainder) Integer division: x + 10 7 * 2 8-5 * 10 (3 * 10) / 2 14 3 = 4 r. 2 (because 4*3+2 = 14) 14/3 => 4 in C++ 14%3 => 2 in C++ 14.0/3.0 => 4.6666667 in C++ 9 Operator precedence In an expression with mutipe operators, which one happens first? Use this order for different operators: + - (unary) * / % + - (binary) < > <= >= ==!= && We wi study reationa and ogica operators next week. Use this order for mutipe occurrences of the same operator - (unary negation) associates right to eft *, /, %, +, - associate eft to right 10 Output (cout and <<) Basic Input/Output sends data to the screen (consoe) cout << expression; cout << expr1 << expr2; cout << heo ; cout << Count is: << count << end; Input (cin and >>) receives data typed in from the keyboard (stops at space) cin >> variabe; cin >> var1 >> var2; right hand side must be a variabe! cout << Enter the height and width: ; cin >> height >> width; cout << The height is << height << end; 11 Formatting output Goa: contro how output dispays for numeric data these require #incude<iomanip> setw(x): print next vaue in a fied at east x spaces wide (right justified, padded with spaces). cout << setw(6) << 1234 << setw(6) << 5 << end; cout << setw(6) << 5 << setw(6) << 1234 << end; cout << fixed << setprecision(x); print a ing point vaues using x digits after the decima (put it at the beginning of the program): cout << fixed << setprecision(2); cout << 3.14159 << end; x = 20; cout << x << end; 3.14 20.00 1234 5 5 1234 12

The string cass string iteras: represent sequences of chars, inside of quotes: To define string variabes: string firstname, astname; Operations incude: - = for assignment -.size() function for ength cout << Heo ; string name; name = George ; cout << name.size() << ; cout << name[2] << end; - [n] to access one character in the nth position. 13 Impicit - assignment: - binary operations: Expicit Type conversions int x; d = 3.1415; x = d; cout << x << end; int x = 10; d = 2.3; cout << x + d << end; int x, y;... avg = static_cast<>(x)/y; or avg = x/()y; //c-stye notation the type of expression on the right wi be converted to type of variabe on eft, possiby osing information. the operand with the ower ranking type is converted to the type of the other. Order of types: ong ong int char 14 Math Library functions Comments These require cmath header fie These take argument, return a Commony used functions: pow y = pow(x,d); returns x raised to the power d abs y = abs(x); returns absoute vaue of x sort y = sqrt(x); returns square root of x cei y = cei(x); returns the smaest integer >= x sin y = sin(x); returns the sine of x (in radians) etc. Singe-Line Comments // this text is ignored, to end of ine Muti-Line Comments /* Anything occurring between a sash star and a star sash is ignored. Even when spanning mutipe ines. */ Use comments to expain your code to a human reader who knows C++. 15

Programming Stye The visua organization of the source code Purpose: improve the readabiity of the source code Incudes the use of spaces, tabs, and bank ines Incudes naming of variabes, constants. Incudes where to use comments. Common eements to improve readabiity: Braces { } aigned verticay Indentation of statements within a set of braces Lines shorter than 80 characters. 17