View
223
Download
4
Embed Size (px)
Citation preview
Introduction to Introduction to ComputingComputing
Dr. Nadeem A KhanDr. Nadeem A Khan
Lecture 5Lecture 5
On the web site On the web site http://suraj.lums.edu.pk/~cs101a0http://suraj.lums.edu.pk/~cs101a0
4/4/
Assignment 2 has been Assignment 2 has been posted. posted.
Chapter 3Chapter 3
Program PlanningProgram Planning
1.1. AnalyzeAnalyze
2.2. DesignDesign
3.3. Choose the Choose the InterfaceInterface
4.4. CodeCode
5.5. Test and DebugTest and Debug
6.6. Complete the Complete the DocumentationDocumentation
The Problem-Solving Process The Problem-Solving Process
Input
Processing
Output
Program Design ToolsProgram Design Tools
►Pseudo-codePseudo-code
►Flow ChartsFlow Charts
►Hierarchy Hierarchy ChartsCharts
Postage stamp problem:Postage stamp problem:
How many stamps to put on a How many stamps to put on a envelop given one stamp is envelop given one stamp is needed for every five sheets of needed for every five sheets of paper or a fraction thereofpaper or a fraction thereof
PseudocodePseudocode
►Abbreviated version of actual Abbreviated version of actual computer code in English-like computer code in English-like statementsstatements
Pseudo code: Postage Stamp Pseudo code: Postage Stamp ProblemProblem
►Program – Determine the number of Program – Determine the number of stamps for a letterstamps for a letter
Read SheetsRead Sheets
Set the number of stamps to sheets / 5Set the number of stamps to sheets / 5
Round the number of stamps Round the number of stamps
Display the number of stamps Display the number of stamps
FlowchartsFlowcharts
►Special geometric symbols Special geometric symbols connected by arrows connected by arrows
Postage Postage stamp stamp
problemproblem
Start
Read sheets
Set stamps = sheets/ 5
Display stamps
Round stamps up to next
whole number
End
input
processing
output
processing
Elements of FlowchartsElements of Flowcharts
Symbol Name
Flowline
Terminal
Input/Output
Processing
Decision
Continued…Continued…
Symbol Name
Connector
Off page Connector
Predefined process
Annotation
Hierarchy chartHierarchy chart
►Shows overall program structureShows overall program structure
Hierarchy Charts: Postage Hierarchy Charts: Postage Stamp ProblemStamp Problem
Postage Stamp
Problem
Read
Sheets
Calculate
stamps
Display
stamps
Set stamps = sheets/5
Round stamps to next whole
number
Another problem:Another problem:
Given a street number of one-way Given a street number of one-way street in New York, decide the street in New York, decide the direction of the street, either direction of the street, either eastbound or westboundeastbound or westbound
Note: Note: Even numbered street: EastboundEven numbered street: EastboundOdd numbered street: WestboundOdd numbered street: Westbound
DecisionsDecisions
►Sequence StructureSequence Structure – a sequence – a sequence followed without skipping any line.followed without skipping any line.
►Decision StructureDecision Structure – a structure – a structure which requires a decision for any lines which requires a decision for any lines of code to be executed.of code to be executed.
Decision Structure: Decision Structure: PseudocodePseudocode
IF condition is TRUE THENIF condition is TRUE THENProcess step(s) 1Process step(s) 1
ELSEELSE Process step(s) 2Process step(s) 2
END IFEND IF
Decision Decision Structure:Structure:FlowchartFlowchart Process
Step (s) 2
Is Condition True
Process Step (s)
1
Decision Structure: Decision Structure: PseudocodePseudocode
What in case of multiple What in case of multiple conditions?conditions?
Decision Structure: Decision Structure: PseudocodePseudocode
IF condition 1 is TRUE THENIF condition 1 is TRUE THENProcess step(s) 1Process step(s) 1
ELSE IF condition 2 is TRUE ELSE IF condition 2 is TRUE THENTHEN Process step(s) 2Process step(s) 2
ELSEELSEProcess step(s) 3Process step(s) 3
END IFEND IF
Pseudo-Code: Street Direction Pseudo-Code: Street Direction ProblemProblem
Get Street
IF Street is EVEN
Display Eastbound
ELSE
Display Westbound
END IF
Flow Flow Chart: Chart: Street Street DirectioDirectionnProblem Problem
End
Start
Get Street
Is street even?
Display Westbound
Display Eastbound
Hierarchy Chart: Street Hierarchy Chart: Street Direction ProblemDirection Problem
Street Directio
n Program
Get Street
Number
Decide whether street
number is odd or even
Display direction
Still Another ProblemStill Another Problem
► CCalculate and report the grade-point alculate and report the grade-point average of a class.average of a class.
The Loop StructureThe Loop Structure
► A programming structure that A programming structure that executes instructions many times.executes instructions many times.
Flow chart: Flow chart: loop structureloop structure No
Process Step(s)
Is condition true ?
Yes
Pseudo code: loop structurePseudo code: loop structure
► DO WHILEDO WHILE condition is TRUE condition is TRUE
Process Step(s)Process Step(s)
LOOPLOOP
Draw the Flowchart for the class Draw the Flowchart for the class average problemaverage problem
Pseudo code: Class Average Pseudo code: Class Average ProblemProblem
INITIALIZE Counter and Sum to 0
DO WHILE there are more data
Get the next Grade
Add the Grade to the Sum
Increment the Counter
LOOP
Compute Average = Sum/Counter
Display Average
Hierarchy Chart: Class Average Hierarchy Chart: Class Average ProblemProblem
Class average program
Get grade
Compute sum
Display average
Calculate
average
Chapter 4Chapter 4
► Already known:Already known:
StringString
SingleSingle
IntegerInteger
Data TypesData Types
► StringsStrings
Storage:Storage: 10 bytes + string length10 bytes + string length
Range:Range: 0 to app. 2 billions chars. 0 to app. 2 billions chars.
Declaration:Declaration:
Dim strVarLen As String Dim strVarLen As String
((declares a string of a variable declares a string of a variable length)length)
Data Types Data Types (Contd.)(Contd.)
► Fixed-Length StringsFixed-Length Strings
Storage:Storage: Length of stringLength of string
Range:Range: 1 to app. 65, 400 chars. 1 to app. 65, 400 chars.
Declaration:Declaration:
Dim strFixLen As String * 2Dim strFixLen As String * 2
(declares a string of a fix size of 2 (declares a string of a fix size of 2 chars.)chars.)
Data Types (Contd.)Data Types (Contd.)
► Fixed-Length StringsFixed-Length StringsUsage: Usage:
ExampleExampleDim strText As String * 5Dim strText As String * 5
Let strText = “Hello” Let strText = “Hello” Picture1.Print strTextPicture1.Print strTextLet strText = “H” Let strText = “H” Picture1.Print strTextPicture1.Print strTextLet strText = “HelloWorld” Let strText = “HelloWorld” Picture1.Print strTextPicture1.Print strText
Data TypesData Types
► Fixed-Length StringsFixed-Length StringsUsage: Usage:
Result:Result:HelloHello (Complete string) (Complete string)
H…. H…. (H followed 4 spaces)(H followed 4 spaces)
HelloHello (First 5 characters (First 5 characters only)only)
=> The length is fix=> The length is fix
Data TypesData Types
► IntegersIntegers
Storage:Storage: 2 bytes2 bytes
Range:Range: -32,768 to 32,767 -32,768 to 32,767
Declaration:Declaration:
Dim intExample As IntegerDim intExample As Integer
(declares intExample as an Integer (declares intExample as an Integer variable)variable)
Data Types Data Types (Contd.)(Contd.)
► IntegersIntegersUsage: Usage:
ExampleExampleDim count As IntegerDim count As IntegerLet count= 6 Let count= 6 Picture1.Print countPicture1.Print countLet count= count+1Let count= count+1Picture1.Print countPicture1.Print countLet count= 6/5 Let count= 6/5 Picture1.Print countPicture1.Print countLet count= 2.33333 * 2Let count= 2.33333 * 2Picture1.Print countPicture1.Print count
Data TypesData Types
► IntegerIntegerUsage: Usage:
ResultResult667 7 1 1 (rounding to lower value)(rounding to lower value)55 (rounding to higher value)(rounding to higher value)
=> takes only whole number values=> takes only whole number values
Data TypesData Types
► Long (Integer)Long (Integer)
Storage:Storage: 4 bytes4 bytes
Range:Range: -2,147,483,648 to -2,147,483,648 to 2,147,483,647 2,147,483,647
Declaration:Declaration:
Dim lngExample As LongDim lngExample As Long
(declares lntExample as a long variable)(declares lntExample as a long variable)
Data Types Data Types (Contd.)(Contd.)
► Long (Integer)Long (Integer)
Usage:Usage: Same as Integer Type except the Same as Integer Type except the range isrange is
much largermuch larger
Data Types Data Types (Contd.)(Contd.)
► ByteByte
Storage:Storage: 1 byte1 byte
Range:Range: 0 to 255 0 to 255
Declaration:Declaration:
Dim bytExample As ByteDim bytExample As Byte
(declares bytExample as a Byte type (declares bytExample as a Byte type variable)variable)
Data Types Data Types (Contd.)(Contd.)
►ByteByte
Usage:Usage: Same as Integer Type except the Same as Integer Type except the range isrange is
positive and much smallerpositive and much smaller
Data Types Data Types (Contd.)(Contd.)
► BooleanBoolean
Storage:Storage: 2 bytes2 bytes
Range:Range: TRUE(1) or FALSE(0) TRUE(1) or FALSE(0)
Declaration:Declaration:
Dim blnState As BooleanDim blnState As Boolean
(declares a Boolean type variable (declares a Boolean type variable blnState)blnState)
Data Types Data Types (Contd.)(Contd.)
► BooleanBooleanUsage: Usage:
ExampleExampleDim blnExample As BooleanDim blnExample As BooleanLet blnExample= FALSE Let blnExample= FALSE Picture1.Print blnExamplePicture1.Print blnExampleLet blnExample= 1Let blnExample= 1Picture1.Print blnExamplePicture1.Print blnExampleLet blnExample= 6Let blnExample= 6Picture1.Print blnExamplePicture1.Print blnExample
Let blnExample= -8*7+5.2Let blnExample= -8*7+5.2Picture1.Print blnExamplePicture1.Print blnExample
Data Types Data Types (Contd.)(Contd.)
► BooleanBooleanUsage: Usage:
ExampleExample FALSEFALSE
TRUETRUE
TRUETRUE
TRUETRUE
=>Values other than 0 are TRUE=>Values other than 0 are TRUE
Data Types Data Types (Contd.)(Contd.)
► Single (Precision Floating-Point)Single (Precision Floating-Point)
Storage:Storage: 4 bytes4 bytes
Range:Range: -3.4…E38 to -1.4…E-45 -3.4…E38 to -1.4…E-45 (negative) (negative)
1.4…E-45 to 3.4…E38 (positive)1.4…E-45 to 3.4…E38 (positive)
Declaration:Declaration:
Dim sngAverage As SingleDim sngAverage As Single
(declares a Single type variable sngAverage)(declares a Single type variable sngAverage)
Data Types Data Types (Contd.)(Contd.)
► Double (Precision Floating-Point)Double (Precision Floating-Point)
Storage:Storage: 8 bytes8 bytes
Range:Range: -1.7…E308 to -4.9…E-324 (negative) -1.7…E308 to -4.9…E-324 (negative)
4.9…E-324 to 1.7…E308 (positive)4.9…E-324 to 1.7…E308 (positive)
Declaration:Declaration:
Dim dblAverage As DoubleDim dblAverage As Double
(declares a Double type variable dblAverage)(declares a Double type variable dblAverage)
Data Types Data Types (Contd.)(Contd.)
► DoubleDoubleUsage: Usage:
ExampleExampleDim sngValue As Single, dblValue As DoubleDim sngValue As Single, dblValue As Double
Let sngValue= 1/3 Let sngValue= 1/3
Picture1.Print sngValuePicture1.Print sngValue
Let dblValue= 1/3Let dblValue= 1/3
Picture1.Print dblValuePicture1.Print dblValue
Data Types Data Types (Contd.)(Contd.)
► DoubleDoubleUsage: Usage:
ResultResult0.33333330.3333333 (Single precision)(Single precision)
0.333333333333333 (Double precision)0.333333333333333 (Double precision)
=> Value of 1/3 represented more => Value of 1/3 represented more accuratelyaccurately
by double than by single by double than by single
Data Types Data Types (Contd.)(Contd.)
► DoubleDoubleUsage: Usage:
ExampleExampleDim sngValue As Single, dblValue As Dim sngValue As Single, dblValue As
DoubleDouble
Let sngValue= 1/3Let sngValue= 1/3
Let sngValue= sngValue * 100000 Let sngValue= sngValue * 100000
Picture1.Print sngValuePicture1.Print sngValue
Let dblValue= 1/3Let dblValue= 1/3
Let dblValue= dblValue * 100000 Let dblValue= dblValue * 100000
Picture1.Print dblValuePicture1.Print dblValue
Data Types Data Types (Contd.)(Contd.)
► DoubleDoubleUsage: Usage:
ResultResult33333.3433333.34 (Single precision; rounding error)(Single precision; rounding error)
33333.3333333333 (Double precision)33333.3333333333 (Double precision)
=> - The decimal point is floating;=> - The decimal point is floating;
- Eventually both will be subjected to rounding errors- Eventually both will be subjected to rounding errors
value increases to large valuesvalue increases to large values
- Still Double will remain more precise than Single - Still Double will remain more precise than Single
Data Types Data Types (Contd.)(Contd.)
► CurrencyCurrency
Storage:Storage: 8 bytes8 bytes
Range:Range: -922,337,203,685,477.5808 to -922,337,203,685,477.5808 to
922,337,203,685,477.5807 922,337,203,685,477.5807
Declaration:Declaration:
Dim curRevenue As CurrencyDim curRevenue As Currency
(declares a Currency type variable (declares a Currency type variable curRevenue)curRevenue)
Data Types Data Types (Contd.)(Contd.)
► CurrencyCurrencyUsage: Usage:
ExampleExampleDim curValue As CurrencyDim curValue As Currency
Let curValue= 1/3Let curValue= 1/3
Picture1.Print curValuePicture1.Print curValue
Let curValue= 100*1/3 Let curValue= 100*1/3
Picture1.Print curValuePicture1.Print curValue
Data Types Data Types (Contd.)(Contd.)
► CurrencyCurrencyUsage: Usage:
ResultResult0.33330.3333
33333.3333 33333.3333
=>=> - The decimal point is NOT floating;- The decimal point is NOT floating;
- Could be used for currency and scientific - Could be used for currency and scientific research research
- No rounding problems for high values- No rounding problems for high values
Data Types Data Types (Contd.)(Contd.)
►Even more data types Even more data types Date Variable: for date and timeDate Variable: for date and time Object VariableObject Variable Variant VariableVariant Variable ………………..
Read the book for more info.Read the book for more info.
Data Types Data Types (Contd.)(Contd.)
Local vs Form VariablesLocal vs Form Variables