computer programming –i - strawberry -...

Post on 23-Mar-2018

226 Views

Category:

Documents

6 Downloads

Preview:

Click to see full reader

TRANSCRIPT

ComputerProgramming–I

DevelopedByStrawberry

CourseOutcome•  AWerSuccessfulcompleIonofthiscourse,studentswillbeableto–  Illustrateflowchartandalgorithmforagivenproblem

– DevelopandExecuteCprogramsusingbasicprogrammingconstructs.

–  ImplementCprogramsusingvariousdatatypesandfuncIons

–  SolveproblemsusingPointers

DevelopedByStrawberry

Algorithms&Flowchart

DevelopedByStrawberry

WhatisanAlgorithm?

•  ComputerScienIstNiklausWirthstatedthat– Program=Algorithms+Data

– Algorithm:Procedureforsolvingaprobleminfinitenumberofsteps.

DevelopedByStrawberry

ExampleforAlgorithm

•  Makingapotoftea:1.  IfthekeJledoesnotcontainwater,thenfillthe

keJle.2.  PlugthekeJleintothepowerpointandswitchiton.3.  Iftheteapotisnotempty,thenemptytheteapot.4.  Placetealeavesintheteapot.5.  IfthewaterinthekeJleisnotboiling,thengoto

step5.6.  SwitchoffthekeJle.7.  PourwaterfromthekeJleintotheteapot.

DevelopedByStrawberry

ExampleforAlgorithm•  Makingapotoftea:

1.  IfthekeJledoesnotcontainwater,thenfillthekeJle.(Decisionmaking)

2.  PlugthekeJleintothepowerpointandswitchiton.

3.  If the teapot is not empty, then empty theteapot.(Decisionmaking)

4.  Placetealeavesintheteapot.5.  If thewater in thekeJle isnotboiling, thengo

tostep5.(Repe::on)6.  SwitchoffthekeJle.7.  PourwaterfromthekeJleintotheteapot.DevelopedByStrawberry

AlgorithmFeatures

•  Sequence(alsoknownasprocess)•  Decision(alsoknownasselecIon)•  RepeIIon(alsoknownasiteraIonorlooping)

DevelopedByStrawberry

Sequence

•  Each step or process in the algorithm isexecutedinthespecifiedorder.

DevelopedByStrawberry

Thedecisionconstructs–if…then..else

•  Outcomeofadecision–eithertrueorfalse.–  IftodayisFridaythencollectpay.

•  It iseither true that ‘today is Friday’or it is false that‘todayisnotFriday’.

•  Itcannotbeboth.•  GeneralForm:IfProposiIonIfFridaythenprocess1thencollectpayelseelseprocess2callonnextMonday

DevelopedByStrawberry

RepeIIon•  RepeataprocessorsequenceofprocessunIlacondiIonbecomestrue.

•  GeneralForm:RepeatRepeatProcess1FillwaterinkeJleProcess2UnIlKeJleisfull……………..ProcessNUnIlProposiIon

DevelopedByStrawberry

RepeIIon-While•  WhathappensifthekeJleisalreadyfull?•  Whileloopmoreappropriate.WhileproposiIonWhilekeJleisnotfullbeginfillwaterinkeJle.Process1

DevelopedByStrawberry

RepeIIon–Ifthengoto1.  FillsomewaterinkeJle2.  IfkeJlenotfullthengoto1.

DevelopedByStrawberry

Variables1.  Program consists of algorithm (Process) and

data.2.  Dataneedstobestored.3.  Data is contained(stored) inwhat is calleda

‘variable’.4.  The variable is a container of a value that

may change during the execuIon of theprogram.

DevelopedByStrawberry

Variables1.  Eachvariableinaprogramisgivenaname2.  Example: Water_Level, Water_Temprature,

Num1,Num2Howtousevariableinalgorithm?IfWater_Levelis0thenfillthekeJle.

DevelopedByStrawberry

Rulesforalgorithm1.  Eachalgorithmwillbe logicallyenclosedby

twostatementsSTARTandSTOP.

2.  Toacceptdatafromuser,theINPUTorREADstatementsaretobeused.

3.  Todisplayanyusermessage,or thecontentinavariablePRINTstatementwillbeused.

DevelopedByStrawberry

Rulesforalgorithm–ArithmeIcOperators

1.  ‘ß’Assignmentoperator xß 6 means the value 6 is assigned to thevariablex.ZßX+Y(ValueofX+ValueofY)assignedtoZZßX-YXß5Yß6ZßX*YZßX/Y

DevelopedByStrawberry

Rulesforalgorithm–RelaIonalOperators1.  ‘>’greaterthan

X>Y means if thevaluecontained inX is larger thanthat inYthen outcome of the expression is true else outcome ofexpressionisfalse.‘<=‘LessthanorequaltoX<=Y means that if the value held in X is either less than orequal to thevalueheld inY thenoutcomeof theexpression istrueelsefalse.OtherrelaIonaloperators:>,>=,=,!=(Notequalto)

DevelopedByStrawberry

Rulesforalgorithm–Logicaloperators1.  ‘AND’conjuncIonTheoutcomeof theexpression is truewhenbothproposiIonsaretrueotherwiseitisfalse.Xß2Yß1X=2ANDY=0X=2truebutY=0isfalseSooutcomeoftotalexpressionisfalse.

DevelopedByStrawberry

Rulesforalgorithm–Logicaloperators2.  ‘OR’DisjuncIonThe outcome of an expression is true when anyone of theproposiIonsistrueelseitisfalse.Xß2Yß1X=2ORY=0X=2istruebutY=0isfalseBut as ‘OR’ is used as one proposiIon is true result for enIreexpressionwillbetrue.

DevelopedByStrawberry

AlgorithmExampleAlgorithmforfindingthesumofanytwonumbers.SoluIon:LetthetwonumbersbeAandBandlettheirsumbeequaltoC.Then,thedesiredalgorithmisgivenasfollows:1.  START2.  PRINT“ENTERTWONUMBERS”3.  INPUTA,B4.  CßA+B5.  PRINTc6.  STOP

DevelopedByStrawberry

AlgorithmExampleAlgorithm for determining the remainder of a division operaIonwherethedividendanddivisorarebothintegers.SoluIon: Let N and D be the dividend and divisor, respecIvely.Assume Q to be the quoIent, which is an integer, and R to be theremainder.Thealgorithmforthegivenproblemisasfollows:1.  START2.  PRINT“ENTERDIVIDEND”3.  INPUTN4.  PRINT“ENTERDIVISOR5.  INPUTD6.  QßN/D(INTEGERDIVISION)7.  RßN-Q*D8.  PRINTR9.  STOP

DevelopedByStrawberry

AlgorithmExampleConstructthealgorithmforinterchangingthenumericvaluesoftwovariables.Hint: If A and B are two variables with values 5 and 6respecIvely. Interchange values ofA andB, so that valueofAwillbecome6andvalueofBwillbecomeA.UsethirdvariableCforit.

DevelopedByStrawberry

AlgorithmExampleSoluIon: Let the two variablesbeA andB. ConsiderC tobe athird variable that is used to store the value of one of thevariablesduringtheprocessofinterchangingthevalues.Thealgorithmforthegivenproblemisasfollows:1.  START2.  PRINT“ENTERTHEVALUEOFA&B”3.  INPUTA,B4.  CßA5.  AßB6.  BßC7.  PRINTA,B8.  END

DevelopedByStrawberry

AlgorithmExampleWriteanalgorithmthatcomparestwonumbersandprintseitherthemessage idenIfying the greater number or themessage staIng thebothnumbersareequal.SoluIon:LetAandBbetwovariablestorepresentthetwonumbersthat are being compared. The algorithm for this problem is given asfollows:1.  START2.  PRINT“ENTERTWONUMBERS”3.  INPUTA,B4.  IFA>BTHEN

PRINT“AISGREATERTHANB”5.  IFB>ATHENPRINT“BISGREATERTHANA”6.  IFA=BTHENPRINT“BOTHAREEQUAL”7.STOP

DevelopedByStrawberry

AlgorithmExampleWriteanalgorithmprintthelargestnumberamongthreenumbers.SoluIon:LetthethreenumbersberepresentedbyA,BandC.SoluIonforthealgorithmisasfollows:

SoluIon11.  START2.  PRINT “ENTER THREE

NUMBERS”3.  INPUTA,B,C4.  IFA>=BANDB>=CTHENPRINTA5.  IFB>=CANDC>=ATHENPRINTB6.  ELSEPRINTC7.STOP

SoluIon21.  START2.  PRINT“ENTERTHREE

NUMBERS”3.  INPUTA,B,C4.  IFA>CTHENIFA>CTHENPRINTAELSEPRINTCELSEIFB>CTHENPRINTBELSEPRINTC5.STOPDevelopedByStrawberry

AlgorithmExampleTakethreesidesofatriangleasinputandcheckwhetherthetrianglecanbedrawnornot.Classifythetriangleasequilateral,isosceles,orscalene.SoluIon: Let the length of three sides of triangle be represented by A,B and C.SoluIonforthealgorithmisasfollows:

SoluIon11.  START2.  PRINT“ENTERLENGTHOFTHREESIDESOFATRAINGLE”3.  INPUTA,B,C4.  IFA+B>CANDB+C>AANDA+C>BTHENPRINT”TRAINGLECANBEDRAWN”ELSEPRINT“TRIANBLECANNOTBEDRAWN”:GOTO6.5.  IFA=BANDB=CTHENPRINT“EQUILATERAL”ELSEIFA!=BANDB!=CTHENPRINT“SCALENE”ELSEPRINT“ISOSCELES”6.STOP

DevelopedByStrawberry

AlgorithmExampleConstruct an algorithm for incremenIng the value of a variable that startswith aniniIalvalueof1andstopswhenthevaluebecomes5.SoluIon:LetthevariableberepresentedbyC.Thealgorithmforthesaidproblemisgivenasfollows:1.  START2.  Cß13.  WHILEC<=54.  BEGIN5.  PRINTC6.  CßC+17.  END

DevelopedByStrawberry

AlgorithmExampleWriteanalgorithmfortheaddiIonofNgivennumbers.SoluIon:LetthesumofNgivennumbersberepresentedbyS.1.  START2.  PRINT“HOWMANYNUMBERS?”3.  INPUTN4.  Sß05.  Cß16.  PRINT“ENTERNUMBER”7.  INPUTA8.  SßS+A9.  CßC+110.  IFC<=NTHENGOTO611. PRINTS12. STOP

DevelopedByStrawberry

Whatisaprogramflowchart?•  Atoolusedbyprogrammerstodevelopprogramlogic

•  Logicdiagram•  DiagrammaIcrepresentaIonofalgorithm

Start

Open Files

Read a Record

Write a Line

EOF?

No Yes

Close Files Stop

DevelopedByStrawberry

Whydoprogrammersuseflowchartstodevelopprogramlogic?

•  Flowchartsareagoodvisualreferenceofwhattheprogramwilldo

•  ServeasprogramdocumentaIonandasacommunicaIonstool

•  AllowtheprogrammertotestalternaIvesoluIons

The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.

DevelopedByStrawberry

Whataretheflowchartsymbols?The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.

The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.

DevelopedByStrawberry

TerminalSymbolThe image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.

The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.Designates the beginning or end of a program or

routine

START STOP

DevelopedByStrawberry

Input/Output(I/O)SymbolThe image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.

The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.

Denotes an operation involving an input or output device

OPEN FILES

READ A RECORD

WRITE A LINE

CLOSE FILES

DevelopedByStrawberry

ProcessSymbolThe image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.

The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.

Used for move instructions and arithmeIc instructions

AVERAGE = (SCORE1 +

SCORE2) / 2

MOVE SCORE1 TO

ACCUMULATOR

DevelopedByStrawberry

DecisionSymbolThe image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.

The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.

Designates that different logic paths will be followed based on a condition occurring in a program

IS A = B? Yes

No

CODE = ?

1 2 3 4 5

DevelopedByStrawberry

PreparaIonSymbolThe image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.

The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.

Assigns initial values to areas in storage and does whatever else is necessary to get ready for processing

COUNTER = 0

DevelopedByStrawberry

AnnotaIonSymbolThe image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.

The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.

Describes or shows comments on the flowchart

AVERAGE = (SCORE1 +

SCORE2) / 2

CALCULATE AVERAGE

DevelopedByStrawberry

FlowlineSymbolThe image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.

The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.

Links other symbols and indicates the sequence of operations

• Normal flow is top to bottom and left to right

• Arrowheads must be used when the flow is other than the normal flow

DevelopedByStrawberry

ConnectorSymbolThe image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.

The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.Connects parts of the flowchart when the

use of flowlines would be confusing or otherwise undesirable

X : 3 A=

<>

DevelopedByStrawberry

Off-pageConnectorSymbolThe image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.

The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.

Connects parts of the flowchart from one page to another

COMPAREX TO 3 =

< >

p. 2B

DevelopedByStrawberry

MoreRules...•  AvoidintersecIngflowlines

START

STOP

B

C

B

A

C

A

START

STOPDevelopedByStrawberry

Morerules

•  AvoidmulIpleflowlinesenteringasymbol

DevelopedByStrawberry

ExampleinSecIon2.4(page34)

•  InstrucIons– Open–  Read– Move– Write–  Close–  Stop

•  Areasinstorage–  Inputareas– Outputareas– Workareas

Start

Open Files

Read a Record

Write a line

Close Files

Stop

DevelopedByStrawberry

StartandStop•  Start

– FirstinstrucIonofflowchart•  Stop

– LastinstrucIonexecuted•  StartandStopinstrucIonsareinterminalsymbols

Start

Open Files

Read a Record

Write a line

Close Files

Stop

DevelopedByStrawberry

Flowchartexampleforaddingtwonumbers

START

InputA,B

Print“EntervalueforAandB”

CalculateC=A+B

PrintC

STOPDevelopedByStrawberry

FlowchartExample•  DrawaflowchartforcalculaIngthesimpleinterestusingtheformula

•  SI=(P*T*R)/100•  WherePdenotestheprincipalamount,TImeandRrateofinterest.

DevelopedByStrawberry

FlowchartExample•  Drawaflowcharttofindthesumofthefirst50naturalnumbers.

DevelopedByStrawberry

FlowchartExample•  DrawaflowcharttofindthelargestofthreenumbersA,BandC.

DevelopedByStrawberry

top related