Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Introduction to Computing Dr. Nadeem A Khan

2 Lecture 4

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

4 Chapter 3

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

6 The Problem-Solving Process Input Processing Output

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

8 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

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

10 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

11 Flowcharts ► Special geometric symbols connected by arrows

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

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

14 Continued… Symbol Name Connector Off page Connector Predefined process Annotation

15 Hierarchy chart ► Shows overall program structure

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

17 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

18 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.

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

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

21 Decision Structure: Pseudocode What in case of multiple conditions?

22 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

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

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

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

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

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

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

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

30 Draw the Flowchart for the class average problem

31 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

32 Hierarchy Chart: Class Average Problem Class average program Get grade Compute sum Display average Calculate average

33 Chapter 4

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

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

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

37 ► 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

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

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

40 ► 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

41 ► 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

59 Local vs Form Variables

60 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

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

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

63 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

64 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

65 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”

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

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

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

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

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


Download ppt "Introduction to Computing Dr. Nadeem A Khan. Lecture 4."

Similar presentations


Ads by Google