Professional Documents
Culture Documents
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
Algorithms g Pseudocode Controlstructures Theif selectionstatement Theifelse selectionstatement Thewhile loop Countercontrolledloops C ll dl Sentinelcontrolledcontrolledloops Assignmentoperators Incrementanddecrementoperators
Ing.SaviourM.Baldacchino,ICTSystemsEngineering. 2
UniversityofMalta Oct2009
11/4/2009
UniversityofMalta Oct2009
Isanartificialandinformallanguagethathelps youdevelopalgorithms d l l ith SimilartoeverydayEnglishbutnotacomputer language Helpsyouthinkoutaprogrambefore attemptingtowritethecode Carefullyprepared,pseudocodemayeasilybe convertedtoaCprogram Consistsonlyofactionstatements(definitions arenotstatementsbutmessagestothe compiler)
Ing.SaviourM.Baldacchino,ICTSystemsEngineering. 4
UniversityofMalta Oct2009
11/4/2009
ThesequencestructureisinherentlybuiltintoC
Ing.SaviourM.Baldacchino,ICTSystemsEngineering. 5
UniversityofMalta Oct2009
Graphicalrepresentationofanalgorithm Aredrawnusingspecialpurposesymbols Symbolsareconnectedbyflowlines and directionarrows Completealgorithmsstartandendwith ovals havingthewordsBeginandEnd Forportionsofanalgorithmsmallcircles areusedinstead Actions arerepresentedbyrectangles Decisions arerepresentedbythe diamond symbol
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
Begin
Actionsymbol
Decision symbol
Begin
6
UniversityofMalta Oct2009
11/4/2009
Chasthreetypesofselectionstructures:
Theif statement
Calledasingleselectionstatement Eitherperformsnactionifaconditionistrue Orskipstheactioniftheconditionisfalse Calledthedoubleselectionstatement Performsanactionistheconditionistrue Performsadifferentactioniftheconditionisfalse Calledthemultipleselectionstatement Performsoneofmanydifferentactionsdependingonthevalue
Theifelse statement
Theswitch statement
ofanexpression
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
Chasthreetypesofrepetitionstructures: yp p
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
11/4/2009
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
Flowchart
true
grade>=60
printPassed
false
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
10
11/4/2009
Flowchart:
printFailed
false
grade>=60
true
printPassed
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
12
11/4/2009
UniversityofMalta Oct2009
if(grade>=90) g printf(A\n); elseif(grade>=80) printf(B\n); elseif(grade>=70) printf(C\n); elseif(grade>=60) l if( d 6 ) printf(D\n); else printf(F\n);
UniversityofMalta Oct2009 Ing.SaviourM.Baldacchino,ICTSystemsEngineering. 14
11/4/2009
UniversityofMalta Oct2009
UniversityofMalta Oct2009
11/4/2009
Flowchart:
true
Product<=1000
Product=2*product
false
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
17
Illustrationofalgorithmdevelopment g p
Aclassof10studentstookaquiz.Thegrades (integersbetween0and100)areavailable. Determinetheclassaverage
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
18
11/4/2009
Pseudocode
1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
UniversityofMalta Oct2009
#include <stdio.h> int main( void ) oid { int counter; int grade; int total; int average; total = 0; counter = 1; while( counter <= 10 ) { printf( Enter grade: ); scanf( %d, &grade ); ( , g total = total + grade; counter = counter + 1; } average = total / 10; printf( Class average is %d\n, average ); return 0 }
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
20
10
11/4/2009
Generalizationoftheclassaverageproblem Noindicationisgivenofhowmanygradesareto beentered. Thiscontrolstructureisoftencalledindefinite repetition becausethenumberofrepetitionsis notknown Avalueisneededtoindicateendofdataentry Thesentinelvaluemustbechosensothatit cannotbeconfusedwithavalidinputvalue 1isanacceptablevalueforthisproblem
Ing.SaviourM.Baldacchino,ICTSystemsEngineering. 21
Developaclassaveragingprogramthatwillprocessan arbitrarynumberofgradeseachtimetheprogramisrun
UniversityofMalta Oct2009
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
22
11
11/4/2009
UniversityofMalta Oct2009
12
11/4/2009
/*prints3.45*/ /*prints3.4*/ /* i t */
UniversityofMalta Oct2009
Notalways100%precise y p Buttheyareadequatefornumerous applications Normallydevelopthroughdivision Example:10dividedby3gives3.3333 recurring Computerallocatesonlyafixedamountof C ll l fi d f memoryspacetoholdsuchavalue Thestoredfloatingpointvaluecanonlybe anapproximation
Ing.SaviourM.Baldacchino,ICTSystemsEngineering. 26
UniversityofMalta Oct2009
13
11/4/2009
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
27
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
28
14
11/4/2009
c=c+3;
Thearithmeticassignmentoperatorsare:
+= = = *= /= %=
Ing.SaviourM.Baldacchino,ICTSystemsEngineering. 29
UniversityofMalta Oct2009
Unaryincrementoperator++ Unarydecrementoperator U d t t Preincrement:++a;incrementsaby1,thenuse thenewvalueofaintheexpressioninwhichit resides Postincrement:a++;usethevalueofainthe expressioninwhichitresides,thenincrementits valueby1 Thesameappliesfora anda Whenusedinastatementbyitselfthe preincrementandpostincrementhavethesame effect
Ing.SaviourM.Baldacchino,ICTSystemsEngineering. 30
UniversityofMalta Oct2009
15
11/4/2009
g Algorithms Pseudocode Controlstructures Theif selectionstatement Theifelse selectionstatement Thewhile loop Countercontrolledloops C ll dl Sentinelcontrolledcontrolledloops Assignmentoperators Incrementanddecrementoperators
Ing.SaviourM.Baldacchino,ICTSystemsEngineering. 31
UniversityofMalta Oct2009
Discussion
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
32
16