Presentation is loading. Please wait.

Presentation is loading. Please wait.

Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213

Similar presentations


Presentation on theme: "Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213"— Presentation transcript:

1 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk http://hopelive.hope.ac.uk/computing/

2 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What we have done so far Introduced Java as a programming language with three constructs: – Sequence – Selection – Repetition Introduced algorithms expressed in: – Structured English – Flow charts 2

3 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What we shall do today Introduce the concept of a variable Apply variables to algorithms Introduce the trace table as a method of checking the operation of a program by monitoring its variables 3

4 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Counting & Trace Tables Pages 29 to 36 4

5 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Any job we do requires short term memory Coming to class – Where is it? – What time does it start? Getting a round in – Who ordered what? 5 What time is it ?

6 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Some things we remember have to be continually updated Following a recipe – Where are we up to? Counting people coming into a room – What was the last count? Watching a game and you can’t see the scoreboard – What score is it? 6

7 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Question: How do we record what we need to remember in algorithms ? 7

8 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Question: How do we record what we need to remember in algorithms ? 8 Answer: Use variables

9 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables A variable is somewhere in memory you store a piece of information you want to remember 9

10 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables A variable is somewhere in memory you store a piece of information you want to remember As you work through a task, that piece of information stored in memory may get updated – its contents vary over time 10

11 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Representing a variable 11 14 Number of people in the room 2-1 Current score A box represents an area of memory The box is labelled

12 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Rules for naming variables 12 14 NumberOfPeopleInRoom 2-1 CurrentScore

13 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Rules for naming variables No spaces in the name Use capitals to make name more readable Preparing for converting to a computer programming language where normally no spaces are allowed in variable names 13 14 NumberOfPeopleInRoom 2-1 CurrentScore

14 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Making variables in Java int NumberOfPeopleInRoom; String CurrentScore; 14

15 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Making variables in Java int NumberOfPeopleInRoom; String CurrentScore; 15

16 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Making variables in Java int NumberOfPeopleInRoom; String CurrentScore; NumberOfPeopleInRoom = 25; CurrentScore = " 2-1 " ; 16

17 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Making variables in algorithms NumberOfPeopleInRoom := 25 CurrentScore := “2-1” 17 Making the variable and putting in the first value are done in one step

18 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Count := 0 This does 2 things 1.Establishes an area of memory called Count 2.Puts zero into this location 18 ????? Count 0

19 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE This can be read as – becomes or – becomes equal to 19 :=

20 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Fred := 12 20 ??????? 1 Fred 2 12 Fred There is no significance in the name of the variable It is not a keyword like while, if, … It is up to the programmer

21 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Total := 0 Total := Total + 1 21 These 2 lines do 3 things. 1 2 3 ????? Total 0 1 Set up the variable Total Initialise it to zero The new contents of Total become equal to the old contents plus 1

22 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE display Sum 22 156 Sum 156 Copies the contents of the variable Sum to the default output device

23 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE read(Fred) 23 This does three things 1 2 3 Establishes a variable called Fred ??? Fred Waits for input from the input device: For example key press 8 Places 8 into the variable Fred 8 Fred

24 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Reading input in Java int Fred; System.in.read(Fred); 24

25 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Putting it altogether so far 25 Brian := 0 Amanda := 0 read(Alma) while Alma greater than 0 begin Brian := Brian + 1 Amanda := Amanda + Alma read(Alma) end display Brian, Amanda

26 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Putting it altogether so far 26 Brian := 0 Amanda := 0 read(Alma) while Alma greater than 0 begin Brian := Brian + 1 Amanda := Amanda + Alma read(Alma) end display Brian, Amanda ? ? Amanda Brian ? Alma Variables

27 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Putting it altogether so far Brian := 0 Amanda := 0 read(Alma) while Alma greater than 0 begin Brian := Brian + 1 Amanda := Amanda + Alma read(Alma) end display Brian, Amanda 0 ? Amanda Brian ? Alma Variables

28 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Putting it altogether so far 28 Brian := 0 Amanda := 0 read(Alma) while Alma greater than 0 begin Brian := Brian + 1 Amanda := Amanda + Alma read(Alma) end display Brian, Amanda 0 ? Amanda Brian 0 Alma Variables

29 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Putting it altogether so far 29 Brian := 0 Amanda := 0 read(Alma) while Alma greater than 0 begin Brian := Brian + 1 Amanda := Amanda + Alma read(Alma) end display Brian, Amanda Assume that 6 is input 0 6 Amanda Brian 0 Alma Variables

30 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Putting it altogether so far 30 Brian := 0 Amanda := 0 read(Alma) while Alma greater than 0 begin Brian := Brian + 1 Amanda := Amanda + Alma read(Alma) end display Brian, Amanda Alma is greater than 0 so we go into loop body 0 6 Amanda Brian 0 Alma Variables

31 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Putting it altogether so far 31 Brian := 0 Amanda := 0 read(Alma) while Alma greater than 0 begin Brian := Brian + 1 Amanda := Amanda + Alma read(Alma) end display Brian, Amanda 1 6 Amanda Brian 0 Alma Variables

32 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Putting it altogether so far 32 Brian := 0 Amanda := 0 read(Alma) while Alma greater than 0 begin Brian := Brian + 1 Amanda := Amanda + Alma read(Alma) end display Brian, Amanda 1 6 Amanda Brian 6 Alma Variables

33 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Putting it altogether so far 33 Brian := 0 Amanda := 0 read(Alma) while Alma greater than 0 begin Brian := Brian + 1 Amanda := Amanda + Alma read(Alma) end display Brian, Amanda 1 4 Amanda Brian 6 Alma Variables Assume that 4 is input

34 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Putting it altogether so far 34 Brian := 0 Amanda := 0 read(Alma) while Alma greater than 0 begin Brian := Brian + 1 Amanda := Amanda + Alma read(Alma) end display Brian, Amanda 1 4 Amanda Brian 6 Alma Variables Alma is greater than 0 so we go into loop body

35 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Putting it altogether so far 35 Brian := 0 Amanda := 0 read(Alma) while Alma greater than 0 begin Brian := Brian + 1 Amanda := Amanda + Alma read(Alma) end display Brian, Amanda 2 4 Amanda Brian 6 Alma Variables

36 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Putting it altogether so far 36 Brian := 0 Amanda := 0 read(Alma) while Alma greater than 0 begin Brian := Brian + 1 Amanda := Amanda + Alma read(Alma) end display Brian, Amanda 2 4 Amanda Brian 10 Alma Variables

37 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Putting it altogether so far 37 Brian := 0 Amanda := 0 read(Alma) while Alma greater than 0 begin Brian := Brian + 1 Amanda := Amanda + Alma read(Alma) end display Brian, Amanda 2 0 Amanda Brian 10 Alma Variables Assume that 0 is input

38 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Putting it altogether so far 38 Brian := 0 Amanda := 0 read(Alma) while Alma greater than 0 begin Brian := Brian + 1 Amanda := Amanda + Alma read(Alma) end display Brian, Amanda 2 0 Amanda Brian 10 Alma Variables Alma is no longer greater than 0 so the loop is finished

39 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Putting it altogether so far 39 Brian := 0 Amanda := 0 read(Alma) while Alma greater than 0 begin Brian := Brian + 1 Amanda := Amanda + Alma read(Alma) end display Brian, Amanda 2 0 Amanda Brian 10 Alma Variables 2 10

40 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java version int Brian; int Stewart; int Alma; Brian = 0; Stewart = 0; System.in.read(Alma); while (Alma > 0) { Brian = Brian + 1; Stewart = Stewart + Alma; System.in.read(Alma); } System.out.println(Brian, Stewart); 40

41 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Good Practice: Variable Names 41 Variable names should reflect their intended purpose. This purpose, like a well designed user interface, should be guessable. This helps with the readability of a program, together with comments. What is the intended purpose of each of the variables: Brian, Stewart and Alma ?

42 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Better Variable Names 42 1 is added to the contents of Brian (initially zero) each time through the while loop. Each number input is stored in (not added to) the variable Alma. The value stored in Alma is added to the current value of Stewart (initially zero).

43 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE The Names at Last 43 Brian seems to be counting each loop. Alma seems to be storing each number input Stewart seems to be keeping a running total. Brian Count Alma Number Stewart Total Refer to your course booklet on page 31

44 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Trace Tables 44 The above method of keeping track of the contents of variables is obviously tedious. A better way would be to employ a tabular method. This is called a trace table. Consider the following algorithm : A := 6(Step 1) B := 8(Step 2) C := A(Step 3) A := B(Step 4) B := C(Step 5)

45 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE The Empty Trace Table 45 Step Number ABC 1 2 3 4 5 Variables A := 6 B := 8 C := A A := B B := C

46 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Here we go 46 Step Number ABC 16 2 3 4 5 A := 6 B := 8 C := A A := B B := C

47 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Next Step 47 Step Number ABC 16 28 3 4 5 A := 6 B := 8 C := A A := B B := C

48 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Next Step 48 Step Number ABC 16 28 36 4 5 A := 6 B := 8 C := A A := B B := C

49 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Next Step 49 A := 6 B := 8 C := A A := B B := C Step Number ABC 16 28 36 48 5

50 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Next Step 50 A := 6 B := 8 C := A A := B B := C Step Number ABC 16 28 36 48 56

51 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Tracing a Loop 51 Answer := 1(Step 1) I := 0(Step 2) Number := 0(Step 3) while (I is less than 3 )(Step 4) begin read(Number)(Step 5) Answer := Answer * Number(Step 6) I := I + 1(Step 7) end display Answer Multiply Steps are numbered wherever variable contents are changed or conditions are tested.

52 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Here is the Blank Trace Table Step Number AnswerINumberIs I < 3 ? 52 Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer 12345671234567

53 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Initialisation steps Step Number AnswerINumberIs I < 3 ? 11 53 Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer 12345671234567

54 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Initialisation steps Step Number AnswerINumberIs I < 3 ? 11 20 54 Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer 12345671234567

55 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Initialisation steps Step Number AnswerINumberIs I < 3 ? 11 20 30 55 Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer 12345671234567

56 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Now the Test: Is 0 < 3 ? Step Number AnswerINumberIs I < 3 ? 11 20 30 4True 56 Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer 12345671234567

57 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Into the Loop Step Number AnswerINumberIs I < 3 ? 11 20 30 4True 55 57 Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer 12345671234567 Assume 5 is input

58 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Into the Loop Step Number AnswerINumberIs I < 3 ? 11 20 30 4True 55 65 58 Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer 12345671234567 5 times 1 = 5

59 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Into the Loop Step Number AnswerINumberIs I < 3 ? 11 20 30 4True 55 65 71 59 Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer 12345671234567 1 is added to I

60 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Back to the test: Is 1 < 3 ? Step Number AnswerINumberIs I < 3 ? 11 20 30 4True 55 65 71 4 60 Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer 12345671234567

61 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Inside the loop again Step Number AnswerINumberIs I < 3 ? 11 20 30 4True 55 65 71 4 52 61 Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer 12345671234567 Assume 2 is input

62 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Inside the loop again Step Number AnswerINumberIs I < 3 ? 11 20 30 4True 55 65 71 4 52 610 62 Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer 12345671234567 5 times 2 = 10

63 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Inside the loop again Step Number AnswerINumberIs I < 3 ? 11 20 30 4True 55 65 71 4 52 610 72 63 Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer 12345671234567 1 is added to I

64 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Back to the test: Is 2 < 3 ? Step Number AnswerINumberIs I < 3 ? 11 20 30 4True 55 65 71 4 52 610 72 4True 64 Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer 12345671234567

65 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Inside the loop again Step Number AnswerINumberIs I < 3 ? 11 20 30 4True 55 65 71 4 52 610 72 4True 54 65 Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer 12345671234567 Assume 4 is input

66 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Inside the loop again Step Number AnswerINumberIs I < 3 ? 11 20 30 4True 55 65 71 4 52 610 72 4True 54 640 66 Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer 12345671234567 10 times 4 = 40

67 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Inside the loop again Step Number AnswerINumberIs I < 3 ? 11 20 30 4True 55 65 71 4 52 610 72 4True 54 640 73 67 Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer 12345671234567 Add 1 to I

68 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Back to the test: Is 3 < 3 ? Step Number AnswerINumberIs I < 3 ? 11 20 30 4True 55 65 71 4 52 610 72 4True 54 640 73 4False 68 Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer 12345671234567

69 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Back to the test: Is 3 < 3 ? Step Number AnswerINumberIs I < 3 ? 11 20 30 4True 55 65 71 4 52 610 72 4True 54 640 73 4False 69 Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer 12345671234567

70 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE That’s the End of the Trace Table. 70 Last line of Program: display Answer 40 Output the contents of variable Answer

71 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java Version int Answer; int Number; int I; Answer = 1; I = 0; Number = 0; while (I < 3) { System.in.read(Number); Answer = Answer * Number; I = I + 1; } System.out.println(Answer); 71

72 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Next? Assessment due – this has been emailed to you. if ( ! receivedAssessment) begin email blakews@hope.ac.ukblakews@hope.ac.uk end else begin do assessment end


Download ppt "Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213"

Similar presentations


Ads by Google