Lecture 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy