59
Introduction to Introduction to Computing Computing Dr. Nadeem A Khan Dr. Nadeem A Khan

Introduction to Computing Dr. Nadeem A Khan. Lecture 5

  • View
    223

  • Download
    4

Embed Size (px)

Citation preview

Page 1: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

Introduction to Introduction to ComputingComputing

Dr. Nadeem A KhanDr. Nadeem A Khan

Page 2: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

Lecture 5Lecture 5

Page 3: Introduction to Computing Dr. Nadeem A Khan. Lecture 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.

Page 4: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

Chapter 3Chapter 3

Page 5: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

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

Page 6: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

The Problem-Solving Process The Problem-Solving Process

Input

Processing

Output

Page 7: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

Program Design ToolsProgram Design Tools

►Pseudo-codePseudo-code

►Flow ChartsFlow Charts

►Hierarchy Hierarchy ChartsCharts

Page 8: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

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

Page 9: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

PseudocodePseudocode

►Abbreviated version of actual Abbreviated version of actual computer code in English-like computer code in English-like statementsstatements

Page 10: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

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

Page 11: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

FlowchartsFlowcharts

►Special geometric symbols Special geometric symbols connected by arrows connected by arrows

Page 12: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

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

Page 13: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

Elements of FlowchartsElements of Flowcharts

Symbol Name

Flowline

Terminal

Input/Output

Processing

Decision

Page 14: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

Continued…Continued…

Symbol Name

Connector

Off page Connector

Predefined process

Annotation

Page 15: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

Hierarchy chartHierarchy chart

►Shows overall program structureShows overall program structure

Page 16: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

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

Page 17: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

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

Page 18: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

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.

Page 19: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

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

Page 20: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

Decision Decision Structure:Structure:FlowchartFlowchart Process

Step (s) 2

Is Condition True

Process Step (s)

1

Page 21: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

Decision Structure: Decision Structure: PseudocodePseudocode

What in case of multiple What in case of multiple conditions?conditions?

Page 22: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

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

Page 23: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

Pseudo-Code: Street Direction Pseudo-Code: Street Direction ProblemProblem

Get Street

IF Street is EVEN

Display Eastbound

ELSE

Display Westbound

END IF

Page 24: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

Flow Flow Chart: Chart: Street Street DirectioDirectionnProblem Problem

End

Start

Get Street

Is street even?

Display Westbound

Display Eastbound

Page 25: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

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

Page 26: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

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.

Page 27: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

The Loop StructureThe Loop Structure

► A programming structure that A programming structure that executes instructions many times.executes instructions many times.

Page 28: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

Flow chart: Flow chart: loop structureloop structure No

Process Step(s)

Is condition true ?

Yes

Page 29: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

Pseudo code: loop structurePseudo code: loop structure

► DO WHILEDO WHILE condition is TRUE condition is TRUE

Process Step(s)Process Step(s)

LOOPLOOP

Page 30: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

Draw the Flowchart for the class Draw the Flowchart for the class average problemaverage problem

Page 31: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

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

Page 32: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

Hierarchy Chart: Class Average Hierarchy Chart: Class Average ProblemProblem

Class average program

Get grade

Compute sum

Display average

Calculate

average

Page 33: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

Chapter 4Chapter 4

Page 34: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

► Already known:Already known:

StringString

SingleSingle

IntegerInteger

Data TypesData Types

Page 35: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

► 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.)

Page 36: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

► 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.)

Page 37: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

► 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

Page 38: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

► 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

Page 39: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

► 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.)

Page 40: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

► 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

Page 41: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

► 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

Page 42: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

► 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.)

Page 43: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

► 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.)

Page 44: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

► 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.)

Page 45: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

►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.)

Page 46: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

► 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.)

Page 47: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

► 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.)

Page 48: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

► 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.)

Page 49: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

► 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.)

Page 50: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

► 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.)

Page 51: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

► 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.)

Page 52: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

► 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.)

Page 53: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

► 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.)

Page 54: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

► 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.)

Page 55: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

► 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.)

Page 56: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

► 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.)

Page 57: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

► 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.)

Page 58: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

►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.)

Page 59: Introduction to Computing Dr. Nadeem A Khan. Lecture 5

Local vs Form VariablesLocal vs Form Variables