Chapter 1 Itroductio to Computers ad C++ Programmig Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Overview 1.1 Computer Systems 1.2 Programmig ad Problem Solvig 1.3 Itroductio to C++ 1.4 Testig ad Debuggig Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
1.1 Computer Systems Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Computer Systems A computer program is A set of istructios for a computer to follow Computer software is The collectio of programs used by a computer Icludes: Editors Traslators System Maagers Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Hardware Three mai classes of computers PCs (Persoal Computer) Relatively small used by oe perso at a time Workstatio Larger ad more powerful tha a PC Maiframe Still larger Requires support staff Shared by multiple users Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Networks A umber of computers coected to share resources Share priters ad other devices Share iformatio Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Computer Orgaizatio Five mai compoets Iput devices Allows commuicatio to the computer Output devices Allows commuicatio to the user Processor (CPU) Mai memory Memory locatios cotaiig the ruig program Secodary memory Permaet record of data ofte o a disk Display 1.1 Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Computer Memory Mai Memory Log list of memory locatios Each cotais zeros ad oes Ca chage durig program executio Biary Digit or Bit A digit that ca oly be zero or oe Byte Each memory locatio has eight bits Address Number that idetifies a memory locatio Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Larger Data Items Some data is too large for a sigle byte Most itegers ad real umbers are too large Address refers to the first byte Next few cosecutive bytes ca store the additioal bits for larger data Display 1.2 Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Data or Code? A may look like 01000001 65 may look like 01000001 A istructio may look like 01000001 How does the computer kow the meaig of 01000001? Iterpretatio depeds o the curret istructio Programmers rarely eed to be cocered with this problem. Reaso as if memory locatios cotai letters ad umbers rather tha zeroes ad oes Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Secodary Memory Mai memory stores istructios ad data while a program is ruig. Secodary memory Stores istructios ad data betwee sessios A file stores data or istructios i secodary memory Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Secodary Memory Media A computer might have ay of these types of secodary memory Hard disk Fast Fixed i the computer ad ot ormally removed Floppy disk Slow Easily shared with other computers Compact disk Slower tha hard disks Easily shared with other computers Ca be read oly or re-writable Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Memory Access Radom Access Usually called RAM Computer ca directly access ay memory locatio Sequetial Access Data is geerally foud by searchig through other items first More commo i secodary memory Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
The Processor Typically called the CPU Cetral Processig Uit Follows program istructios Typical capabilities of CPU iclude: add subtract multiply divide move data from locatio to locatio Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Computer Software The operatig system Allows us to commuicate with the computer Is a program Allocates the computer s resources Respods to user requests to ru other programs Commo operatig systems iclude UNIX Liux DOS Widows Macitosh VMS Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Computer Iput Computer iput cosists of A program Some data Display 1.3 Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
High-level Laguages Commo programmig laguages iclude C C++ Java Pascal Visual Basic FORTRAN Perl PHP Lisp Scheme Ada C# Pytho These high level laguages Resemble huma laguages Are desiged to be easy to read ad write Use more complicated istructios tha the CPU ca follow Must be traslated to zeros ad oes for the CPU to execute a program Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Low-level Laguages A assembly laguage commad such as ADD X Y Z might mea add the values foud at x ad y i memory, ad store the result i locatio z. Assembly laguage must be traslated to machie laguage (zeros ad oes) 0110 1001 1010 1011 The CPU ca follow machie laguage Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Compilers Traslate high-level laguage to machie laguage Source code The origial program i a high level laguage Object code The traslated versio i machie laguage Display 1.4 Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Likers Some programs we use are already compiled Their object code is available for us to use For example: Iput ad output routies A Liker combies The object code for the programs we write ad The object code for the pre-compiled routies ito The machie laguage program the CPU ca ru Display 1.5 Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
History Note First programmable computer Desiged by Charles Babbage Bega work i 1822 Not completed i Babbage s life time First programmer Ada Augusta, Coutess of Lovelace Colleague of Babbage Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Sectio 1.1 Coclusio Ca you List the five mai compoets of a computer? List the data for a program that adds two umbers? Describe the work of a compiler? Defie source code? Defie object code? Describe the purpose of the operatig system? Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
1.2 Programmig ad Problem- Solvig Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Algorithms Algorithm A sequece of precise istructios that leads to a solutio Program A algorithm expressed i a laguage the computer ca uderstad Display 1.6 Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Program Desig Programmig is a creative process No complete set of rules for creatig a program Program Desig Process Problem Solvig Phase Result is a algorithm that solves the problem Implemetatio Phase Result is the algorithm traslated ito a programmig laguage Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Problem Solvig Phase Be certai the task is completely specified What is the iput? What iformatio is i the output? How is the output orgaized? Develop the algorithm before implemetatio Experiece shows this saves time i gettig your program to ru. Test the algorithm for correctess Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Implemetatio Phase Traslate the algorithm ito a programmig laguage Easier as you gai experiece with the laguage Compile the source code Locates errors i usig the programmig laguage Ru the program o sample data Verify correctess of results Results may require modificatio of the algorithm ad program Display 1.7 Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Object Orieted Programmig Abbreviated OOP Used for may moder programs Program is viewed as iteractig objects Each object cotais algorithms to describe its behavior Program desig phase ivolves desigig objects ad their algorithms Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
OOP Characteristics Ecapsulatio Iformatio hidig Objects cotai their ow data ad algorithms Iheritace Writig reusable code Objects ca iherit characteristics from other objects Polymorphism A sigle ame ca have multiple meaigs depedig o its cotext Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Software Life Cycle Aalysis ad specificatio of the task (problem defiitio) Desig of the software (object ad algorithm desig) Implemetatio (codig) Maiteace ad evolutio of the system Obsolescece Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Sectio 1.2 Coclusio Ca you Describe the first step to take whe creatig a program? List the two mai phases of the program desig process? Explai the importace of the problem-solvig phase? List the steps i the software life cycle? Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
1.3 Itroductio to C++ Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Itroductio to C++ Where did C++ come from? Derived from the C laguage C was derived from the B laguage B was derived from the BCPL laguage Why the ++? ++ is a operator i C++ ad results i a cute pu Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
C++ History C developed by Deis Ritchie at AT&T Bell Labs i the 1970s. Used to maitai UNIX systems May commercial applicatios writte i c C++ developed by Bjare Stroustrup at AT&T Bell Labs i the 1980s. Overcame several shortcomigs of C Icorporated object orieted programmig C remais a subset of C++ Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
A Sample C++ Program A simple C++ program begis this way #iclude <iostream> usig amespace std; it mai() { Ad eds this way } retur 0; Display 1.8 Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Explaatio of code (1/5) Variable declaratio lie it umber_of_pods, peas_per_pod, total_peas; Idetifies ames of three variables to ame umbers it meas that the variables represet itegers Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Explaatio of code (2/5) Program statemet cout << Press retur after eterig a umber.\ ; cout (see-out) used for output to the moitor << iserts Press a umber.\ i the data boud for the moitor Thik of cout as a ame for the moitor << poits to where the data is to ed up \ causes a ew lie to be started o the moitor Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Explaatio of code (3/5) Program statemet ci >> umber_of_pods; ci (see-i) used for iput from the keyboard >> extracts data from the keyboard Thik of ci as a ame for the keyboard >> poits from the keyboard to a variable where the data is stored Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Explaatio of code (4/5) Program statemet total_peas = umber_of_pods * peas_per_pod; Performs a computatio * is used for multiplicatio = causes total_peas to get a ew value based o the calculatio show o the right of the equal sig Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Explaatio of code (5/5) Program statemet cout << umber_of_pods; Seds the value of variable umber_of_pods to the moitor Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Program Layout (1/3) Compiler accepts almost ay patter of lie breaks ad idetatio Programmers format programs so they are easy to read Place opeig brace { ad closig brace } o a lie by themselves Idet statemets Use oly oe statemet per lie Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Program Layout (2/3) Variables are declared before they are used Typically variables are declared at the begiig of the program Statemets (ot always lies) ed with a semi-colo Iclude Directives #iclude <iostream> Tells compiler where to fid iformatio about items used i the program iostream is a library cotaiig defiitios of ci ad cout Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Program Layout (3/3) usig amespace std; Tells the compiler to use ames i iostream i a stadard way To begi the mai fuctio of the program it mai() { To ed the mai fuctio retur 0; } Mai fuctio eds with a retur statemet Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Ruig a C++ Program C++ source code is writte with a text editor The compiler o your system coverts source code to object code. The liker combies all the object code ito a executable program. Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
C++11 C++11 (formerly kow as C++0x) is the most recet versio of the stadard of the C++ programmig laguage. Approved o August 12, 2011 by the Iteratioal Orgaizatio for Stadardizatio. C++11 laguage features are ot supported by older compilers Check the documetatio with your compiler to determie if special steps are eeded to compile C++11 programs e.g. with g++, use extra flags of std=c++11 Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Ru a Program Obtai code i Display 1.10 Compile the code Fix ay errors the compiler idicates ad re-compile the code Ru the program Display 1.10 Now you kow how to ru a program o your system Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Sectio 1.3 Coclusio Ca you Describe the output of this lie? cout << C++ is easy to uderstad. ; Explai what this lie does? ci >> peas_per_pod; Explai this? #iclude <iostream> Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
1.4 Testig ad Debuggig Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Testig ad Debuggig Bug A mistake i a program Debuggig Elimiatig mistakes i programs Term used whe a moth caused a failed relay o the Harvard Mark 1 computer. Grace Hopper ad other programmers taped the moth i logbook statig: First actual case of a bug beig foud. Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Program Errors Sytax errors Violatio of the grammar rules of the laguage Discovered by the compiler Error messages may ot always show correct locatio of errors Ru-time errors Error coditios detected by the computer at ru-time Logic errors Errors i the program s algorithm Most difficult to diagose Computer does ot recogize a error Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Sectio 1-4 Coclusio Ca you Describe the three kids of program errors? Tell what kid of errors the compiler catches? What kid of error is produced if you forget a puctuatio symbol such as a semi-colo? Tell what type of error is produced whe a program rus but produces icorrect results? Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Chapter 1 -- Ed Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Display 1.1 Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Display 1.2 Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Display 1.3 Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Display 1.4 Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Display 1.5 Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Display 1.6 Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Display 1.7 Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Display 1.8 Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Display 1.9 Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Display 1.10 Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.