 # Introduction to Computing Dr. Nadeem A Khan. Lecture 4.

## Presentation on theme: "Introduction to Computing Dr. Nadeem A Khan. Lecture 4."— Presentation transcript:

Introduction to Computing Dr. Nadeem A Khan

Lecture 4

On the web site http://suraj.lums.edu.pk/~cs101a04/ Assignment 2 has been posted.

Chapter 3

Program Planning 1. Analyze 2. Design 3. Choose the Interface 4. Code 5. Test and Debug 6. Complete the Documentation

The Problem-Solving Process Input Processing Output

Program Design Tools ► Pseudo-code ► Flow Charts ► Hierarchy Charts

Postage stamp problem: How many stamps to put on a envelop given one stamp is needed for every five sheets of paper or a fraction thereof How many stamps to put on a envelop given one stamp is needed for every five sheets of paper or a fraction thereof

Pseudocode ► Abbreviated version of actual computer code in English-like statements

Pseudo code: Postage Stamp Problem ► Program – Determine the number of stamps for a letter Read Sheets Set the number of stamps to sheets / 5 Round the number of stamps Display the number of stamps

Flowcharts ► Special geometric symbols connected by arrows

Postage stamp problem Start Read sheets Set stamps = sheets/ 5 Display stamps Round stamps up to next whole number End input processing output processing

Elements of Flowcharts Symbol Name Flowline Terminal Input/Output Processing Decision

Continued… Symbol Name Connector Off page Connector Predefined process Annotation

Hierarchy chart ► Shows overall program structure

Hierarchy Charts: Postage Stamp Problem Postage Stamp Problem Read Sheets Calculate stamps Display stamps Set stamps = sheets/5 Round stamps to next whole number

Another problem: Given a street number of one-way street in New York, decide the direction of the street, either eastbound or westbound Given a street number of one-way street in New York, decide the direction of the street, either eastbound or westboundNote: Even numbered street: Eastbound Odd numbered street: Westbound

Decisions ► Sequence Structure – a sequence followed without skipping any line. ► Decision Structure – a structure which requires a decision for any lines of code to be executed.

Decision Structure: Pseudocode IF condition is TRUE THEN Process step(s) 1 ELSE Process step(s) 2 Process step(s) 2 END IF

Decision Structure: Flowchart Process Step (s) 2 Is Condition True Process Step (s) 1

Decision Structure: Pseudocode What in case of multiple conditions?

Decision Structure: Pseudocode IF condition 1 is TRUE THEN Process step(s) 1 ELSE IF condition 2 is TRUE THEN Process step(s) 2 Process step(s) 2ELSE Process step(s) 3 END IF

Pseudo-Code: Street Direction Problem Get Street IF Street is EVEN Display Eastbound ELSE Display Westbound END IF

Flow Chart: Street Direction Problem End Start Get Street Is street even? Display Westbound Display Eastbound

Hierarchy Chart: Street Direction Problem Street Direction Program Get Street Number Decide whether street number is odd or even Display direction

Still Another Problem ► Calculate and report the grade-point average of a class.

The Loop Structure ► A programming structure that executes instructions many times.

Flow chart: loop structure No Process Step(s) Is condition true ? Yes

Pseudo code: loop structure ► DO WHILE condition is TRUE Process Step(s) LOOP

Draw the Flowchart for the class average problem

Pseudo code: Class Average Problem 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 Problem Class average program Get grade Compute sum Display average Calculate average

Chapter 4

► Already known:  String  Single  Integer Data Types

► Strings Storage: 10 bytes + string length Range: 0 to app. 2 billions chars. Declaration: Dim strVarLen As String (declares a string of a variable length) (declares a string of a variable length) Data Types (Contd.)

► Fixed-Length Strings Storage: Length of string Range: 1 to app. 65, 400 chars. Declaration: Dim strFixLen As String * 2 (declares a string of a fix size of 2 chars.) Data Types (Contd.)

► Fixed-Length Strings Usage:Example Dim strText As String * 5 Let strText = “Hello” Picture1.Print strText Let strText = “H” Picture1.Print strText Let strText = “HelloWorld” Picture1.Print strText Data Types

► Fixed-Length Strings Usage:Result: Hello(Complete string) H…. (H followed 4 spaces) Hello(First 5 characters only) => The length is fix Data Types

► Integers Storage: 2 bytes Range: -32,768 to 32,767 Declaration: Dim intExample As Integer (declares intExample as an Integer variable) (declares intExample as an Integer variable) Data Types (Contd.)

► Integers Usage:Example Dim count As Integer Let count= 6 Picture1.Print count Let count= count+1 Picture1.Print count Let count= 6/5 Picture1.Print count Let count= 2.33333 * 2 Picture1.Print count Data Types

► Integer Usage:Result67 1 (rounding to lower value) 5(rounding to higher value) => takes only whole number values => takes only whole number values Data Types

► Long (Integer) Storage: 4 bytes Range: -2,147,483,648 to 2,147,483,647 Declaration: Dim lngExample As Long (declares lntExample as a long variable) (declares lntExample as a long variable) Data Types (Contd.)

► Long (Integer) Usage: Same as Integer Type except the range is much larger much larger Data Types (Contd.)

► Byte Storage: 1 byte Range: 0 to 255 Declaration: Dim bytExample As Byte (declares bytExample as a Byte type variable) (declares bytExample as a Byte type variable) Data Types (Contd.)

► Byte Usage: Same as Integer Type except the range is positive and much smaller positive and much smaller Data Types (Contd.)

► Boolean Storage: 2 bytes Range: TRUE(1) or FALSE(0) Declaration: Dim blnState As Boolean (declares a Boolean type variable blnState) Data Types (Contd.)

► Boolean Usage:Example Dim blnExample As Boolean Let blnExample= FALSE Picture1.Print blnExample Let blnExample= 1 Picture1.Print blnExample Let blnExample= 6 Picture1.Print blnExample Let blnExample= -8*7+5.2 Let blnExample= -8*7+5.2 Picture1.Print blnExample Data Types (Contd.)

► Boolean Usage:Example FALSE FALSE TRUE TRUE =>Values other than 0 are TRUE =>Values other than 0 are TRUE Data Types (Contd.)

► Single (Precision Floating-Point) Storage: 4 bytes Range: -3.4…E38 to -1.4…E-45 (negative) 1.4…E-45 to 3.4…E38 (positive) Declaration: Dim sngAverage As Single (declares a Single type variable sngAverage) Data Types (Contd.)

► Double (Precision Floating-Point) Storage: 8 bytes Range: -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: Dim dblAverage As Double (declares a Double type variable dblAverage) Data Types (Contd.)

► Double Usage:Example Dim sngValue As Single, dblValue As Double Let sngValue= 1/3 Picture1.Print sngValue Let dblValue= 1/3 Picture1.Print dblValue Data Types (Contd.)

► Double Usage:Result 0.3333333(Single precision) 0.333333333333333 (Double precision) => Value of 1/3 represented more accurately by double than by single by double than by single Data Types (Contd.)

► Double Usage:Example Dim sngValue As Single, dblValue As Double Let sngValue= 1/3 Let sngValue= sngValue * 100000 Picture1.Print sngValue Let dblValue= 1/3 Let dblValue= dblValue * 100000 Picture1.Print dblValue Data Types (Contd.)

► Double Usage:Result 33333.34(Single precision; rounding error) 33333.3333333333 (Double precision) => - The decimal point is floating; - Eventually both will be subjected to rounding errors value increases to large values value increases to large values - Still Double will remain more precise than Single Data Types (Contd.)

► Currency Storage: 8 bytes Range: -922,337,203,685,477.5808 to 922,337,203,685,477.5807 922,337,203,685,477.5807Declaration: Dim curRevenue As Currency (declares a Currency type variable curRevenue) Data Types (Contd.)

► Currency Usage:Example Dim curValue As Currency Let curValue= 1/3 Picture1.Print curValue Let curValue= 100*1/3 Picture1.Print curValue Data Types (Contd.)

► Currency Usage:Result 0.3333 33333.3333 =>- The decimal point is NOT floating; - Could be used for currency and scientific research - No rounding problems for high values Data Types (Contd.)

► Even more data types  Date Variable: for date and time  Object Variable  Variant Variable  ………. Read the book for more info. Data Types (Contd.)

Local vs Form Variables

Not only +,-,*,^ But also: But also: \ opeartor e.g: 5.1\2.04= 2 \ opeartor e.g: 5.1\2.04= 2 MOD operator e.g: 15.2 MOD 6=3 MOD operator e.g: 15.2 MOD 6=3 => First round to closest integers before operation More on Operators

More on Operators (Contd.) ► Operator Precedence 1. ^ 2.- operator (indicating a negative value) 3. * and / operator 4. \ operator 5. MOD operator 6. + and - operator

Built-in Functions ► Take one or more input values ► Return an output value ► Already seen:  Val(..), Str\$(..), Asc(..), Chr(..)

Built-in Numeric Functions ► Sqr: calculates square-root and returns the value as double data type e.g: e.g:  Sqr(9) is 3  Sqr(0) is 0  Sqr(2) is 1.414214  Sqr(30*2+4) is 8

Built-in Numeric Functions ► Int: Greatest integer less than and equal to a number e.g: e.g:  Int(2.7) is 2  Int(3) is 3  Int(-2.7) is -3

Built-in String Functions ► Left\$, Mid\$, Right\$, UCase\$, Trim\$ Left\$(“fanatic”, 3) is “fan”Right\$(“fanatic”, 3) is “tic” Left\$(“12/15/93”,2) is “12”Right\$(“12/15/93”, 2) is “93” Mid\$(“fanatic”, 5,1) is “t”Mid\$(“12/15/93”,4, 2) is “15” Ucase(“Disk”) is “DISK”Ucase(“12two”) is “12TWO” Trim\$(“ 1 2 ”) is “1 2”Trim\$(“-12 ”) is “-12”

Built-in Functions (Contd.) ► Len Len(“Shenandoah”) is ? Len(“Just a moment”) is ? Len(“m”) is ?

Built-in Functions (Contd.) ► Len Len(“Shenandoah”) is 10 Len(“Just a moment”) is 13 Len(“m”) is 1

Built-in Functions (Contd.) ► Instr Instr(“Shenandoah”, “nand”) is ? Instr(“Just a moment”, “ ”) is ? Instr(“Croissant”, “ist”) is ?

Built-in Functions (Contd.) ► Instr Instr(“Shenandoah”, “nand”) is 4 Instr(“Just a moment”, “ ”) is 5 Instr(“Croissant”, “ist”) is 0

Built-in Functions (Contd.) Abs(10) is 10 Abs(-10) is 10 Sgn(10) is 1 Sgn(0) is 0 Sgn(-10) is -1