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

Slides:



Advertisements
Similar presentations
Introduction to Programming
Advertisements

2.1 Program Construction In Java
Summer Computing Workshop. Introduction to Variables Variables are used in every aspect of programming. They are used to store data the programmer needs.
ABNIAC The following slide presentation is to acquaint the student with ABNIAC. The version used for presentation is the Java version, which can be found.
PSEUDOCODE & FLOW CHART
Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.
Introduction to C Programming
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 5: Steps in Problem Solving Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 8: Java: Selection and Repetition Stewart Blakeway.
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 6: Problem Solving Exercises Stewart Blakeway FML.
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Object Oriented Concepts 2 Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 7: Java basics Stewart Blakeway
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 12: Data Structures 1 Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Data Structures 3 Stewart Blakeway FML
Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving An Introduction Stewart Blakeway
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
Program Design and Development
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Introduction to C Programming
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
The switch Statement, DecimalFormat, and Introduction to Looping
Adapted from slides by Marie desJardins
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
CIS Computer Programming Logic
Faculty of Sciences and Social Sciences HOPE JavaScript Validation Regular Expression Stewart Blakeway FML
Algorithmic Problem Solving CMSC 201 Adapted from slides by Marie desJardins (Spring 2015 Prof Chang version)
Chapter 12: How Long Can This Go On?
1 Introduction to Flowcharting. 2 Writing a program Defining the problem –Write down what the program will do Planning –Write down the steps, draw a flowchart.
Faculty of Sciences and Social Sciences HOPE JavaScript Advanced Stewart Blakeway FML
By the end of this session you should be able to...
What does a computer program look like: a general overview.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
University of Sunderland CIF 102/FIF102 Fundamentals of DatabasesUnit 15 Programming in Microsoft Access using VBA Using VBA to add functionality.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
Chapter 2 Pseudocode. Objectives To introduce common words, keywords and meaningful names when writing pseudocode To define the three basic control structures.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 3: Algorithms Stewart Blakeway FML 213
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
CS 240 – Computer Programming I Lab Kalpa Gunaratna –
24 Background Building 25 Computing Terminology, and Speed/Velocity Module 4 Notes: Sensing, Or Operator, Conditional Logic 28 Module 4 Algorithms,
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
Comments, Conditional Statements Continued, and Loops Engineering 1D04, Teaching Session 4.
Computer Science 1000 Algorithms III. Multiple Inputs suppose I ask you to write a program that computes the area of a rectangle area = length * width.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Fundamentals of Algorithms MCS - 2 Lecture # 3. Representation of Algorithms.
Selection Using IF THEN ELSE CASE Introducing Loops.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Whatcha doin'? Aims: To start using Python. To understand loops.
REPETITION CONTROL STRUCTURE
Introducing Instructions
Repetition (While-Loop) version]
ECS10 10/10
Introduction to Programming
C# and the .NET Framework
Repetition-Counter control Loop
Incrementing ITP © Ron Poet Lecture 8.
Iterations Programming Condition Controlled Loops (WHILE Loop)
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Control Structure Senior Lecturer
Introduction to Programming
Flowcharts and Pseudo Code
Introduction to Programming
Presentation transcript:

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