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

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

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

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

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

Week 0: Intro to Computers and Programming. 1.1 Why Program? 1.2 Computer Systems: Hardware and Software. Hardware Components

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

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.

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

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

Tutorial 3 Concepts for A1

LECTURE 02 INTRODUCTION TO C++

file://j:\macmillancomputerpublishing\chapters\in073.html 3/22/01

Professor: Alvin Chao

Searching, Sorting & Analysis

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

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

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

Language Identification for Texts Written in Transliteration

The C++ Language. Arizona State University 1

Directives & Memory Spaces. Dr. Farid Farahmand Updated: 2/18/2019

Professor: Alvin Chao

MCSE Training Guide: Windows Architecture and Memory

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

Special Edition Using Microsoft Excel Selecting and Naming Cells and Ranges

CSE120 Principles of Operating Systems. Architecture Support for OS

Register Allocation. Consider the following assignment statement: x = (a*b)+((c*d)+(e*f)); In posfix notation: ab*cd*ef*++x

As Michi Henning and Steve Vinoski showed 1, calling a remote

Navigating and searching theweb

Lecture outline Graphics and Interaction Scan Converting Polygons and Lines. Inside or outside a polygon? Scan conversion.

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

Chapter 2: Introduction to C++

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

Sample of a training manual for a software tool

Basic Concepts. Lexical Conventions

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

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

Solutions to the Final Exam

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

End To End Software Developer Training

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

CS 241 Computer Programming. Introduction. Teacher Assistant. Hadeel Al-Ateeq

understood as processors that match AST patterns of the source language and translate them into patterns in the target language.

Reference trajectory tracking for a multi-dof robot arm

Chapter 3: Introduction to the Flash Workspace

SQL3 Objects. Lecture #20 Autumn, Fall, 2001, LRX

Chapter 1 Introduction to Computers and C++ Programming

Chapter 1 Introduction to Computers and Programming

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

l A set is a collection of objects of the same l {6,9,11,-5} and {11,9,6,-5} are equivalent. l There is no first element, and no successor of 9.

A Design Method for Optimal Truss Structures with Certain Redundancy Based on Combinatorial Rigidity Theory

Nearest Neighbor Learning

CHAPTER 3 BASIC INSTRUCTION OF C++

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

Sensitivity Analysis of Hopfield Neural Network in Classifying Natural RGB Color Space

CSE120 Principles of Operating Systems. Prof Yuanyuan (YY) Zhou Lecture 4: Threads

MCSE TestPrep SQL Server 6.5 Design & Implementation - 3- Data Definition

Outerjoins, Constraints, Triggers

Extracting semistructured data from the Web: An XQuery Based Approach

Distance Weighted Discrimination and Second Order Cone Programming

BITG 1233: Introduction to C++

The Big Picture WELCOME TO ESIGNAL

This is a CLOSED-BOOK-CLOSED-NOTES exam consisting of five (5) questions. Write your answer in the answer booklet provided. 1. OO concepts (5 points)

Objectives. In this chapter, you will:

BEA WebLogic Server. Release Notes for WebLogic Tuxedo Connector 1.0

ECE 172 Digital Systems. Chapter 14 Itanium EPIC Processor Architecture. Herbert G. Mayer, PSU Status 5/10/2018

PL/SQL, Embedded SQL. Lecture #14 Autumn, Fall, 2001, LRX

Chapter 1: Why Program? Computers and Programming. Why Program?

Introduction. Arizona State University 1

RDF Objects 1. Alex Barnell Information Infrastructure Laboratory HP Laboratories Bristol HPL November 27 th, 2002*

A Memory Grouping Method for Sharing Memory BIST Logic

Substitute Model of Deep-groove Ball Bearings in Numeric Analysis of Complex Constructions Like Manipulators

Introduction to USB Development

ECE 172 Digital Systems. Chapter 5 Uniprocessor Data Cache. Herbert G. Mayer, PSU Status 6/10/2018

Infinity Connect Web App Customization Guide

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

Chapter 1 INTRODUCTION

A Fast Block Matching Algorithm Based on the Winner-Update Strategy

Revisions for VISRAD

CBSE SOLVED PAPER 2018 CLASS 11 INFORMATICS RRACTICE OSWAAL BOOKS LEARNING MADE SIMPLE. Strictly as per the Latest NCERT Edition

Dynamic Symbolic Execution of Distributed Concurrent Objects

VARIABLES & ASSIGNMENTS

Bottom-Up Parsing LR(1)

Concurrent programming: From theory to practice. Concurrent Algorithms 2016 Tudor David

Introduction to Programming

Chapter 2 Basic Elements of C++

Introduction to OpenMP

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

l A set is a collection of objects of the same l {6,9,11,-5} and {11,9,6,-5} are equivalent. l There is no first element, and no successor of 9.

A METHOD FOR GRIDLESS ROUTING OF PRINTED CIRCUIT BOARDS. A. C. Finch, K. J. Mackenzie, G. J. Balsdon, G. Symonds

Insert the power cord into the AC input socket of your projector, as shown in Figure 1. Connect the other end of the power cord to an AC outlet.

AN EVOLUTIONARY APPROACH TO OPTIMIZATION OF A LAYOUT CHART

Response Surface Model Updating for Nonlinear Structures

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

Application of Intelligence Based Genetic Algorithm for Job Sequencing Problem on Parallel Mixed-Model Assembly Line

ECEn 528 Prof. Archibald Lab: Dynamic Scheduling Part A: due Nov. 6, 2018 Part B: due Nov. 13, 2018

ECL Portal. Standardized SCADA solution for ECL Comfort 310. Data sheet. Description

Arithmetic Coding. Prof. Ja-Ling Wu. Department of Computer Science and Information Engineering National Taiwan University

Transcription:

Intro to Programming & C++ Unit 1 Sections 1.1-3 and 2.1-10, 2.12-13, 2.15-17 CS 1428 Fa 2017 Ji Seaman 1.1 Why Program? Computer programmabe machine designed to foow instructions Program instructions in computer memory to make the computer do something Programmer person who writes instructions (programs) to make computer perform a task SO, without programmers, no programs; without programs, a computer cannot do anything 1 2 1.2 Computer Systems: Hardware and Software Hardware Components Iustrated Hardware: the physica components that a computer is made of. Software: the programs that run on a computer 3 4

Hardware Components Software Centra Processing Unit (CPU) Arithmetic Logic Unit (math, comparisons, etc) Contro Unit (processes instructions) Main Memory (RAM): Fast, expensive, voatie Secondary Storage: Sow, cheap, ong-asting Input Devices: keyboard, mouse, camera Output Devices: screen, printer, speakers Programs that run on the hardware Operating Systems (System software): programs that manage the computer hardware and the programs that run on them. Unix, MS-DOS, Linux, Windows, Mac OS X Time machine, printer drivers, compiers Appication Programs (Apps): Sove specific probems and provide services to the user Word, Exce, itunes, Firefox, Angry Birds, Photoshop 5 6 1.3 Programs and Programming Languages A program is a set of instructions that the computer foows to perform a task An agorithm: A set of we-defined steps for performing a task or soving a probem. A step by step ordered procedure that soves a probem in a finite number of precise steps. An agorithm can be in any anguage (Engish, C++, machine code, etc). 7 Exampe (agorithm) 1.Dispay on screen: how many hours did you work? 2.Wait for user to enter number, store it in memory 3.Dispay on screen: what is your pay rate (per hour)? 4.Wait for user to enter rate, store it in memory 5.Mutipy hours by rate, store resut in memory 6.Dispay on screen: you have earned $xx.xx where xx.xx is resut of step 5. Note: Computer does not speak Engish, it ony understands its own machine anguage 8

Programming Languages Transation Process Machine Language: Instructions are encoded as a sequence of 1's and 0 s Machine specific Low Leve Languages: Assemby Language Letters and digits (codes) Direct correspondence to Machine Language High Leve Languages (ike C++): Words, symbos, numbers Easier for humans to read and use Must be transated to Machine Code 9 10 2.1 The Parts of a C++ Program Parts of a C++ Program // sampe C++ program #incude <iostream> using namespace std; int main() { cout << "Heo, word!"; return 0; } Comment: // ignored by compier notes to human reader Preprocessor Directive: #incude <iostream> compier inserts contents of fie iostream here required because cout is defined in iostream using namespace std; aows us to write cout instead of std::cout 11 12

Parts of a C++ Program int main () start of function (group of statements) named main the starting point of the program {} contains the body of the function cout << Heo, word! ; statement to dispay message on screen return 0; quit and send vaue 0 to OS (means success!) 2.2 The cout Object cout: short for consoe output a stream object: represents the contents of the screen <<: the stream insertion operator use it to send data to cout (to be output to the screen) cout << This is an exampe. ; when this instruction is executed, the consoe (screen) ooks ike this: This is an exampe. Note: the do not show up in the output 13 14 The end manipuator more exampes end: short for end ine send it to cout when you want to start a new ine of output. cout << Heo << end << there! ; or you can use the newine character: \n cout << Heo \nthere! ; Either way the output to the screen is: cout << Heo << there! ; Heo there! cout << Heo ; cout << there! ; Heo there! Heo there! 15 cout << The best seing book on Amazon\n is \ The Hep\ ; The best seing book on Amazon is The Hep 16

2.3 The #incude Directive 2.4 Variabes and Literas Inserts the contents of another fie into the program. #incude <iostream> For exampe, cout is not part of the core C++ anguage, it is defined in the iostream fie. Any program that uses the cout object must contain the extensive setup information found in iostream. The code in iostream is C++ code. 17 Variabe: named ocation in main memory A variabe definition has a name and a datatype <datatype> <identifier>; The data type indicates the kind of data it can contain. The identifier is a name of your choosing. A variabe must be defined before it can be used!! Exampe variabe definitions: int somenumber; char firstletter; 18 Literas 2.5 Identifiers A itera represents a constant vaue used in a program statement. Numbers: 0, 34, 3.14159, -1.8e12, etc. Strings (sequence of keyboard symbos): Heo, This is a string 100 years, 100, Y, etc. NOTE: These are different: 5 5 An identifier is a name for some program eement (ike a variabe). Rues: May not be a keyword (see Tabe 2.4 in the book) First character must be a etter or underscore Foowing characters must be etters, numbers or underscores. Identifiers are case-sensitive: myvariabe is not the same as MyVariabe 19 20

2.12 Variabe Assignments and Initiaization An assignment statement uses the = operator to store a vaue in an aready defined variabe. somenumber = 12; When this statement is executed, the computer stores the vaue 12 in memory, in the ocation named somenumber. The variabe receiving the vaue must be on the eft side of the = (the foowing does NOT work): 12 = somenumber; //This is an ERROR Exampe program using a variabe #incude <iostream> using namespace std; int main() { int number; } number = 100; cout << The vaue of the number is << number << end; return 0; 21 output screen: The vaue of the number is 100 22 Variabe Initiaization Data Types To initiaize a variabe means to assign it a vaue when it is defined: Variabes are cassified according to their data type. int ength = 12; You can define and initiaize mutipe variabes at once (and change them ater) : int ength = 12, width = 5, area; area = 35; ength = 10; area =40; The data type determines the kind of information that may be stored in the variabe. A data type is a set of vaues. Generay two main (types of) data types: Numeric Character-based 23 24

C++ Data Types int, short, ong whoe numbers (integers) foat, doube rea numbers (with fractiona amounts, decima points) boo ogica vaues: true and fase char a singe character (keyboard symbo) string any text, a sequence of characters 25 2.6 Integer Data Types Whoe numbers such as 12, 7, and -99 Typica ranges (may vary on different systems): Data Type: Range of vaues: short -32,768 to 32,767 unsigned short 0 to 65,535 int -2,147,483,648 to 2,147,483,647 unsigned int 0 to 4,294,967,295 ong -2,147,483,648 to 2,147,483,647 unsigned ong 0 to 4,294,967,295 Exampe variabe definitions: short dayofweek; unsigned ong distance; int xcoordinate; 26 2.9 Foating-Point Data Types Rea numbers such as 12.45, and -3.8 Typica ranges (may vary on different systems): Exampe program using foatingpoint data types // This program uses foating point data types. #incude <iostream> using namespace std; Data Type: foat doube ong doube Range of vaues: +/- 3.4e +/- 38 (~7 digits of precision) +/- 1.7e +/- 308 (~15 digits of precision) +/- 1.7e +/- 308 (~15 digits of precision) Foating-point iteras can be represented in Fixed point (decima) notation: 31.4159 0.0000625 E (scientific) notation: 3.14159E1 6.25e-5 27 int main() { foat distance; doube mass; } distance = 1.495979E11; mass = 1.989E30; cout << "The Sun is " << distance << " meters away.\n"; cout << "The Sun\'s mass is " << mass << " kiograms.\n"; return 0; output screen: The Sun is 1.49598e+11 meters away. The Sun's mass is 1.989e+30 kiograms. 28

2.10 The boo Data Type 2.7 The char Data Type The vaues true and fase. Litera vaues: true, fase (fase is equivaent to 0, true is equivaent to 1) int main() { boo boovaue; boovaue = true; cout << boovaue << end; boovaue = fase; cout << boovaue << end; return 0; } output screen: 1 0 29 A the keyboard and printabe symbos. Litera vaues: A 5? b characters are indicated using singe quotes Numeric vaue of character from the ASCII character set is stored in memory: CODE: char etter; etter = C'; cout << etter << end; MEMORY: etter 67 Appendix B shows the ASCII code vaues OUTPUT: C 30 Specia characters 2.8 The C++ string cass Newine: \n' Doube quote: \ '. These can occur in strings: Heo\nthere she said \ boo\ very quiety See textbook for more It s a backsash (\), not a sash (/) Sequences of characters Requires the string header fie: To define string variabes in programs: string firstname, astname; To assign iteras to variabes: firstname = "George"; astname = "Washington"; To dispay via cout cout << firstname << " " << astname; #incude <string> 31 OUTPUT: George Washington 32

2.13 Scope 2.15 Comments The scope of a variabe is the part of the program in which the variabe can be accessed. A variabe cannot be used before it is defined. // This program can't find its variabe. #incude <iostream> using namespace std; int main() { cout << vaue; // ERROR! vaue not defined yet! Used to document parts of the program Intended for humans reading the source code of the program: Indicate the purpose of the program Describe the use of variabes Expain compex sections of code Are ignored by the compier } int vaue = 100; return 0; 33 34 Singe and Muti-Line Comments Singe-Line comments begin with // through to the end of ine: int ength = 12; // ength in inches int width = 15; // width in inches int area; // cacuated area // cacuate rectange area area = ength * width; Muti-Line comments begin with /*, end with */ /* this is a muti-ine comment */ int area; /* cacuated area */ 35 2.16 Named Constants Named constant : variabe whose vaue cannot be changed during program execution Used for representing constant vaues with descriptive names: const doube TAX_RATE = 0.0675; const int NUM_STATES = 50; Note: initiaization required. Often named in uppercase etters (see stye guideines) 36

2.17 Programming Stye The visua organization of the source code Incudes the use of spaces, tabs, and bank ines Incudes naming of variabes, constants. Incudes where to use comments. Purpose: improve the readabiity of the source code Programming Stye Common eements to improve readabiity: Braces { } aigned verticay Indentation of statements within a set of braces Bank ines between decaration and other statements Long statements intentionay broken up over mutipe ines. See the Stye Guideines on the cass website. You must foow these in your programming assignments. 37 38