Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213
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
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
Faculty of Sciences and Social Sciences HOPE Counting & Trace Tables Pages 29 to 36 4
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 ?
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
Faculty of Sciences and Social Sciences HOPE Question: How do we record what we need to remember in algorithms ? 7
Faculty of Sciences and Social Sciences HOPE Question: How do we record what we need to remember in algorithms ? 8 Answer: Use variables
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
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
Faculty of Sciences and Social Sciences HOPE Representing a variable Number of people in the room 2-1 Current score A box represents an area of memory The box is labelled
Faculty of Sciences and Social Sciences HOPE Rules for naming variables NumberOfPeopleInRoom 2-1 CurrentScore
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 NumberOfPeopleInRoom 2-1 CurrentScore
Faculty of Sciences and Social Sciences HOPE Making variables in Java int NumberOfPeopleInRoom; String CurrentScore; 14
Faculty of Sciences and Social Sciences HOPE Making variables in Java int NumberOfPeopleInRoom; String CurrentScore; 15
Faculty of Sciences and Social Sciences HOPE Making variables in Java int NumberOfPeopleInRoom; String CurrentScore; NumberOfPeopleInRoom = 25; CurrentScore = " 2-1 " ; 16
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
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
Faculty of Sciences and Social Sciences HOPE This can be read as – becomes or – becomes equal to 19 :=
Faculty of Sciences and Social Sciences HOPE Fred := ??????? 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
Faculty of Sciences and Social Sciences HOPE Total := 0 Total := Total These 2 lines do 3 things ????? 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
Faculty of Sciences and Social Sciences HOPE display Sum Sum 156 Copies the contents of the variable Sum to the default output device
Faculty of Sciences and Social Sciences HOPE read(Fred) 23 This does three things 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
Faculty of Sciences and Social Sciences HOPE Reading input in Java int Fred; System.in.read(Fred); 24
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 ?
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).
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
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)
Faculty of Sciences and Social Sciences HOPE The Empty Trace Table 45 Step Number ABC Variables A := 6 B := 8 C := A A := B B := C
Faculty of Sciences and Social Sciences HOPE Here we go 46 Step Number ABC A := 6 B := 8 C := A A := B B := C
Faculty of Sciences and Social Sciences HOPE Next Step 47 Step Number ABC A := 6 B := 8 C := A A := B B := C
Faculty of Sciences and Social Sciences HOPE Next Step 48 Step Number ABC A := 6 B := 8 C := A A := B B := C
Faculty of Sciences and Social Sciences HOPE Next Step 49 A := 6 B := 8 C := A A := B B := C Step Number ABC
Faculty of Sciences and Social Sciences HOPE Next Step 50 A := 6 B := 8 C := A A := B B := C Step Number ABC
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.
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
Faculty of Sciences and Social Sciences HOPE Initialisation steps Step Number AnswerINumberIs I < 3 ? Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer
Faculty of Sciences and Social Sciences HOPE Initialisation steps Step Number AnswerINumberIs I < 3 ? Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer
Faculty of Sciences and Social Sciences HOPE Initialisation steps Step Number AnswerINumberIs I < 3 ? Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer
Faculty of Sciences and Social Sciences HOPE Now the Test: Is 0 < 3 ? Step Number AnswerINumberIs I < 3 ? True 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
Faculty of Sciences and Social Sciences HOPE Into the Loop Step Number AnswerINumberIs I < 3 ? True Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer Assume 5 is input
Faculty of Sciences and Social Sciences HOPE Into the Loop Step Number AnswerINumberIs I < 3 ? True Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer times 1 = 5
Faculty of Sciences and Social Sciences HOPE Into the Loop Step Number AnswerINumberIs I < 3 ? True Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer is added to I
Faculty of Sciences and Social Sciences HOPE Back to the test: Is 1 < 3 ? Step Number AnswerINumberIs I < 3 ? True Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer
Faculty of Sciences and Social Sciences HOPE Inside the loop again Step Number AnswerINumberIs I < 3 ? True Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer Assume 2 is input
Faculty of Sciences and Social Sciences HOPE Inside the loop again Step Number AnswerINumberIs I < 3 ? True Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer times 2 = 10
Faculty of Sciences and Social Sciences HOPE Inside the loop again Step Number AnswerINumberIs I < 3 ? True Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer is added to I
Faculty of Sciences and Social Sciences HOPE Back to the test: Is 2 < 3 ? Step Number AnswerINumberIs I < 3 ? True True 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
Faculty of Sciences and Social Sciences HOPE Inside the loop again Step Number AnswerINumberIs I < 3 ? True True Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer Assume 4 is input
Faculty of Sciences and Social Sciences HOPE Inside the loop again Step Number AnswerINumberIs I < 3 ? True True Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer times 4 = 40
Faculty of Sciences and Social Sciences HOPE Inside the loop again Step Number AnswerINumberIs I < 3 ? True True Answer := 1 I := 0 Number := 0 while (I is less than 3) begin read(Number) Answer := Answer * Number I := I + 1 end display Answer Add 1 to I
Faculty of Sciences and Social Sciences HOPE Back to the test: Is 3 < 3 ? Step Number AnswerINumberIs I < 3 ? True True False 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
Faculty of Sciences and Social Sciences HOPE Back to the test: Is 3 < 3 ? Step Number AnswerINumberIs I < 3 ? True True False 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
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
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
Faculty of Sciences and Social Sciences HOPE Next? Assessment due – this has been ed to you. if ( ! receivedAssessment) begin end else begin do assessment end