DepartmentofSoftware
TheUniversityofBabylon
LECTURENOTESONSTRACTUREPROGRAMMING USINGC++LANGUAGE
By Dr.SamaherHusseinAli
CollegeofInformationTechnology,UniversityofBabylon,Iraq Samaher_hussein@yahoo.com
3December2012
Algorithm and Flowchart
Intheperviouslecture,weexplainedthealgorithm ascollectionofstepsrequireto solveproblemanditmusthavethefollowingconditions:(Keepitsimple,Readthe fascinatingmanual,Makeyourdocumentationshortbutsweet,Everysubprogram shoulddosomethingandhidesomething,ProgramdefensivelyandGoodprogram isaprettyprogram) Inaddition,therearethreetypesofmethodstowriteanyalgorithm,themethods include(setofinstructions(i.e.,PseudoCode),FlowchartandWords)
Algorithm
Setof Instructions (pseudocode)
Flowchart
Words
3December2012
Dr.SamaherHusseinAli
NotesofLecture2
Algorithm and Flowchart
Example: Write the algorithm to compute the result of the following mathematical expression (R=a*b + c). Let: a and b are two variables from type integer while c is constant (i.e., c=2.1). Solution: Using the word method
1. 2. 3. 4. 5. 6. 7. Start Read the first number a Read the second number b Assignment value of the third number c Compute the result (i.e., R=a* b +c) Output R End
Using the Flowchart method
3December2012
Dr.SamaherHusseinAli
NotesofLecture2
Algorithm and Flowchart
Using the Pseudo Code (Set of Instructions) Method /* This is the code to calculate the result of mathematical expression */ # include <iostream .h> # include <conio.h> int a; int b; float c; float R; void main( ) { Cout<< Input the value of first number a:<<endl; Cin>>a; Cout<< Input the value of second number b:<<endl; Cin>>b; C=2.1; Cout<< what is the result?<<endl; R=a*b + c; Cout<<Result=<<R<<endl; getch( ); }
3December2012
SamaherHusseinAli
NotesofLecture2
Example 2: result1=(a*b+c ), result=(result1+2)/d
3December2012
Dr.SamaherHusseinAli
NotesofLecture2
Statements
3December2012
Dr.SamaherHusseinAli
NotesofLecture2
Comments
Commentsarepartsofthesourcecodedisregardedbythecompiler.Theysimplydo nothing.Theirpurposeisonlytoallowtheprogrammertoinsertnotesordescriptions embeddedwithinthesourcecode. C++supportstwowaystoinsertcomments: Thefirstofthem,knownaslinecomment,discardseverythingfromwherethepairof slashsigns(//)isfounduptotheendofthatsameline.Thesecondone,knownas blockcomment,discardseverythingbetweenthe/*charactersandthefirst appearanceofthe*/characters,withthepossibilityofincludingmorethanoneline.
3December2012
Dr.SamaherHusseinAli
NotesofLecture2
Variables
3December2012
Dr.SamaherHusseinAli
NotesofLecture2
Variable Types
3December2012
Dr.SamaherHusseinAli
NotesofLecture2
Variable Types
3December2012
Dr.SamaherHusseinAli
NotesofLecture2
Initialisation
3December2012
Dr.SamaherHusseinAli
NotesofLecture2