35
1 Programming Fundamentals Programming Fundamentals How to Program in C++ How to Program in C++

1 Programming Fundamentals How to Program in C++ How to Program in C++

Embed Size (px)

Citation preview

11

Programming FundamentalsProgramming Fundamentals

How to Program in C++How to Program in C++

22

What is Computer What is Computer SystemSystem ??

•Computer Computer System System is a is a systemsystem capable of capable of –Performing computationsPerforming computations –Making logical decisionsMaking logical decisions –Works billions of times faster than human Works billions of times faster than human

beingsbeings

•Components of a computer systemComponents of a computer system::–HardwareHardware–SoftwareSoftware

33

HardwareHardware

•The hardware The hardware components are the components are the

electronic and electronic and mechanical partsmechanical parts . .

•hardwarehardware devices devices•Keyboard, mouse, etcKeyboard, mouse, etc..

•Screen (monitor)Screen (monitor)

•DisksDisks

•MemoryMemory

•Processing UnitsProcessing Units

44

•InpuInput devicest devices–Obtains data from Obtains data from outside of the outside of the

computercomputer . .–Usually a keyboard, mouse, disk or Usually a keyboard, mouse, disk or

scannerscanner

•Output Output devicesdevices–Makes info available outside the Makes info available outside the

computercomputer..–ScScreens, paper printouts, speakersreens, paper printouts, speakers

Major Hardware Major Hardware ComponentsComponents

55

Major Hardware Major Hardware ComponentsComponents

•ProcessorProcessor–Central Processing UnitCentral Processing Unit or CPU or CPU

–The brain of a computer systemThe brain of a computer system..–It does the fundamental computing within It does the fundamental computing within

the systemthe system..•RResponds to and processes the basic esponds to and processes the basic

instructionsinstructions that drive a computer that drive a computer..

–DDirectly or indirectly controls all the other irectly or indirectly controls all the other componentscomponents . .

66

•MemoryMemory –holdholdss data and programs that CPU is using, and save data and programs that CPU is using, and save

results of programsresults of programs..–Rapid access, Rapid access, low capacitylow capacity, short-term, , short-term, temporarytemporary

“warehouse“warehouse””..–Often called Often called main memorymain memory, , primary memoryprimary memory, or , or

RRandom andom AAccess ccess MMemoryemory ( (RAMRAM)) –““44 gigabytes (4GB) of RAMgigabytes (4GB) of RAM””

•One megabyte of memory is enough to hold One megabyte of memory is enough to hold approximately one million characters of a word approximately one million characters of a word

processing documentprocessing document . .

•I Kb = 1024 bytesI Kb = 1024 bytes

•1024102422 bytes = 1 megabyte & 1024bytes = 1 megabyte & 102433 bytes = 1 gigabyte bytes = 1 gigabyte

Major Hardware Major Hardware ComponentsComponents

77

•Secondary storageSecondary storage –Stores programs or data not currently Stores programs or data not currently

being used by other units on being used by other units on secondary secondary storage devicesstorage devices (like (like hhard disk and ard disk and

floppy disksfloppy disks)) –Long-term, high-capacity “warehouseLong-term, high-capacity “warehouse””–Takes longer to access than primary Takes longer to access than primary

memorymemory

Major Hardware Major Hardware ComponentsComponents

88

QuestionQuestion

•Imagine that you are using a word Imagine that you are using a word processor program to write a letterprocessor program to write a letter . .

–Where (in the computer system) is the Where (in the computer system) is the program you are runningprogram you are running ? ?

–Where are the characters you have Where are the characters you have typedtyped before you hit “ before you hit “savesave” button” button??

After you hit “After you hit “savesave” button” button??

99

AnswerAnswer

•Where (in the computer system) is the Where (in the computer system) is the program you are runningprogram you are running ? ?

–In main memory. (A permanent copy will In main memory. (A permanent copy will also be in secondary also be in secondary storagestorage--the hard disk)--the hard disk)..

•Where are the characters you have Where are the characters you have typedtyped ? ?

–In main memory. (When you "save" your In main memory. (When you "save" your document, they will be copied to a file on document, they will be copied to a file on the hard disk or the place you specified.)the hard disk or the place you specified.)

1010

What is Computer What is Computer ProgramProgram ? ? •Computer Programs are called Computer Programs are called

softwaresoftware–sets of instructions that process datasets of instructions that process data–guide computer through orderly sets of guide computer through orderly sets of

actions specified by actions specified by computer computer programmersprogrammers

–Programmers write instructions that Programmers write instructions that comprise software in various comprise software in various programming languagesprogramming languages

1111

•Problem solvingProblem solving

Problem

programmer

1212

Types of ProgramsTypes of Programs

•There are two categories of programsThere are two categories of programs..–ApplicationApplication programs are programs that programs are programs that

people use to get their work donepeople use to get their work done . .•Word Processor, Web Browsers, etcWord Processor, Web Browsers, etc..

–SystemSystem programs keep all the hardware programs keep all the hardware and software running together smoothlyand software running together smoothly . .

•Operating System Operating System is a kind of software is a kind of software system which is system which is developed to make using developed to make using

computers more convenient.computers more convenient. Windows, Unix, Windows, Unix, Linux, MacintoshLinux, Macintosh..

1313

Machine Languages, Assembly Machine Languages, Assembly Languages, and High-level Languages, and High-level LanguagesLanguagesThree types of programming languagesThree types of programming languages

.1.1Machine languagesMachine languages •Strings of numbers giving machine specific Strings of numbers giving machine specific

instructionsinstructions

•Natural language of the computerNatural language of the computer–0-10-1 corresponding to high or low voltagecorresponding to high or low voltage

•Instructions and data are encoded into binary Instructions and data are encoded into binary numbersnumbers

•ExampleExample::++13000427741300042774

++14005934191400593419

++12002740271200274027

1414

Machine Languages, Assembly Machine Languages, Assembly Languages, and High-level Languages, and High-level LanguagesLanguages

Three types of programming Three types of programming languageslanguages

.1.1Assembly languagesAssembly languages•English-like abbreviations representing English-like abbreviations representing

elementary computer operations elementary computer operations (translated via assemblers)(translated via assemblers)

•ExampleExample::LOAD BASEPAYLOAD BASEPAY

ADD OVERPAYADD OVERPAY

STORE GROSSPAYSTORE GROSSPAY

1515

Machine Languages, Assembly Machine Languages, Assembly Languages, and High-level Languages, and High-level LanguagesLanguages

Three types of programming Three types of programming languages (continued)languages (continued)

.3.3High-level languagesHigh-level languages•Codes similar to everyday EnglishCodes similar to everyday English

•Use mathematical notations (translated via Use mathematical notations (translated via compilers)compilers)

•ExampleExample::grossPay = basePay + overTimePaygrossPay = basePay + overTimePay

1616

Types of Programming Types of Programming LanguagesLanguages

•Three general types of programming Three general types of programming languageslanguages

–Machine languagesMachine languages •machine machine dependentdependent

–Assembly languagesAssembly languages•machine machine dependentdependent

–High-level languagesHigh-level languages•most are most are portableportable

•Specific languages includeSpecific languages include C, C++, Visual Basic C, C++, Visual Basic and Javaand Java

1717

Steps of Software Steps of Software DevelopmentDevelopment

•Problem definitionProblem definition

•Analysing the problemAnalysing the problem

•Development of algorithm and flow Development of algorithm and flow chartchart

•ProgrammingProgramming

•Testing the programTesting the program

•ImplementingImplementing

1818

History of C and CHistory of C and C++++•CC

–Evolved by Ritchie from two previous programming Evolved by Ritchie from two previous programming languages, BCPL and Blanguages, BCPL and B

–Used to develop UNIXUsed to develop UNIX–Used to write modern operating systemsUsed to write modern operating systems–Hardware independent (portable)Hardware independent (portable)–By late 1970's C had evolved to "Traditional CBy late 1970's C had evolved to "Traditional C""

•StandardizationStandardization–Many slight variations of C existed, and were incompatibleMany slight variations of C existed, and were incompatible–Committee formed to create a "unambiguous, machine-Committee formed to create a "unambiguous, machine-

independent" definitionindependent" definition–Standard created in 1989, updated in 1999Standard created in 1989, updated in 1999

1919

The C Standard LibraryThe C Standard Library•C programs consist of pieces/modules called C programs consist of pieces/modules called

functionsfunctions–A programmer can create his own functionsA programmer can create his own functions

•Advantage: the programmer knows exactly how it worksAdvantage: the programmer knows exactly how it works

•Disadvantage: time consumingDisadvantage: time consuming

–Programmers will often use the C library functionsProgrammers will often use the C library functions•Use these as building blocksUse these as building blocks

–Avoid re-inventing the wheelAvoid re-inventing the wheel•If a premade function exists, generally best to use it If a premade function exists, generally best to use it

rather than write your ownrather than write your own

•Library functions carefully written, efficient, and portableLibrary functions carefully written, efficient, and portable

2020

History of C and CHistory of C and C++++•CC++++•C++, an extension of C, was developed in the early C++, an extension of C, was developed in the early

1980s1980s . .•C++ provides a number of features that "spruce C++ provides a number of features that "spruce

up" the C language, but more importantly, it up" the C language, but more importantly, it provides capabilities for provides capabilities for object-oriented object-oriented

programmingprogramming..•ObjectsObjects are essentially reusable software are essentially reusable software

componentscomponents that model items in the real world that model items in the real world..•AA modular, object-oriented design and modular, object-oriented design and

implementation implementation is eis easy to understand, correct and asy to understand, correct and modifymodify

•Object-oriented programs are easier to Object-oriented programs are easier to understand, correct and modifyunderstand, correct and modify..

2121

The C++ Standard LibraryThe C++ Standard Library

•C++ programs consist of pieces called C++ programs consist of pieces called classesclasses and and functionsfunctions

•C++ Standard LibraryC++ Standard Library : : –Rich collections of existing classes and functionsRich collections of existing classes and functions

•Reusable in new applicationsReusable in new applications

•TTwo parts to learning the C++ "worldwo parts to learning the C++ "world ". ".–LLearning the C++ language itselfearning the C++ language itself ; ;–LeaLearning how to use the classes and functions in rning how to use the classes and functions in

the C++ Standard Librarythe C++ Standard Library..

2222

Typical C++ Development Typical C++ Development EnvironmentEnvironment

.1.1EditEdit : P : Programmer writes program (and stores rogrammer writes program (and stores source code on disk)source code on disk)

.2.2PreprocessPreprocess : : Perform certain manipulations before Perform certain manipulations before compilationcompilation

.3.3CompileCompile : : Compiler translates C++ programs into Compiler translates C++ programs into machine languagesmachine languages

.4.4LinkLink : : Link object code with Link object code with the librariesthe libraries ( (missing missing functions and data)functions and data)

.5.5LoadLoad : : Transfer executable image to memoryTransfer executable image to memory

.6.6ExecuteExecute : : Execute the program one instruction at a Execute the program one instruction at a timetime

2323

2424

FlowchartFlowchartIt is a graphical way to describe It is a graphical way to describe

the solution of the problemthe solution of the problem . .Flow Chart Symbols

Start and End

Input / output

Selection

Calculation

Data Flow

2525

Input a,b

S = a + b

Output s

Problem : Compute and print the Problem : Compute and print the summation of two numberssummation of two numbers..

2626

Input n1,n2,n3

S = n1+n2+n3Average = s / 3

Output average

Problem : Compute and print the Problem : Compute and print the average of three numbersaverage of three numbers..

2727

Input r

A = 3.14 * R *R

Output A

Problem : Compute the area of Problem : Compute the area of the circle. Where area = 3.14 x the circle. Where area = 3.14 x R2R2

2828

Input Num

Output “Positive”

Num>0

True

False

Problem : Read any number from Problem : Read any number from the user, then print positive if it is the user, then print positive if it is positivepositive..

2929

Input Num

Output “Positive”

Num>0

TrueFalse

Output “Negative”

Problem : Read any number from Problem : Read any number from the user, then print positive if it the user, then print positive if it is positive and print negative is positive and print negative otherwiseotherwise..

3030

Input x, y

Output x

X>y

TrueFalse

Output y

Problem : Read Two numbers from Problem : Read Two numbers from the user, then print the greatest the user, then print the greatest oneone..

3131

Output “UIIT”

Count<=5True

False

Count = 1

Count = Count + 1

Problem : Print the word Problem : Print the word “UIIT” five times“UIIT” five times..

3232

Output I

I<=11True

False

I = 1

I = I + 2

Problem : Print the following Problem : Print the following numbers.numbers.

11335577991111

3333

Output I

I>=2True

False

I = 20

I = I - 3

Problem : Print the following Problem : Print the following numbers.numbers.

2020171714141111885522

3434

I<=10True

False

I = 1

I = I + 1

S = 0

S = S + NumOutput S

INPUT Num

Problem : Compute and print summation of Problem : Compute and print summation of any 10 numbers entered by the userany 10 numbers entered by the user..

3535

In Next LectureIn Next Lecture

•Introduction to C++ ProgrammingIntroduction to C++ Programming

•BooksBooks::–Text book: Text book: Object-Oriented Object-Oriented

Programming in C++ by Robert LaforeProgramming in C++ by Robert Lafore–Reference book:Reference book: C++, How to C++, How to

Program by Deitel & DeitelProgram by Deitel & Deitel

•Latest Editions of BothLatest Editions of Both..