Mark Dixon Page 1 10 – Array Variables. Mark Dixon Page 2 C1 – In-class test Results mixed: –quite a few fails 34% (13/38) –some very good (91% max) ask.

Slides:



Advertisements
Similar presentations
Lecture Roger Sutton 21: Revision 1.
Advertisements

Mark Dixon Page 1 16 – Passing Data between pages: Forms, Sessions, & Query Strings.
Standard Algorithms. Many algorithms appear over and over again, in program after program. These are called standard algorithms You are required to know.
Mark Dixon Page 1 05 – Constants and Variables. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 mins short answer (5 - 6 words.
Mark Dixon, SoCCE SOFT 136Page 1 05 – Conditional Execution.
Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.
Mark Dixon, SoCCE SOFT 131Page 1 07 – Iterative Execution.
Mark Dixon Page 1 02 – Dynamic HTML (client-side scripting)
Mark Dixon, SoCCE SOFT 131Page 1 07 – Constants, Arrays, & Structures.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Mark Dixon, SoCCE SOFT 131Page 1 08 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 16 – Passing Data between pages: Sessions, Query Strings, & Self Posting.
Mark Dixon Page 1 02 – Queries: Query by Example.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.
Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Information Processing: Data-types, Variables, Operators & Functions.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Mark Dixon Page 1 10 – Iterative Execution. Mark Dixon Page 2 Questions: Variables Write a line of code to declare a variable called h Write a line of.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
02 – Client-side code: JavaScript
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using PhP.
Mark Dixon 1 07 – Variables. Mark Dixon 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40) What will.
Mark Dixon Page 1 3 – Web applications: Server-side code (JSP)
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
Mark Dixon Page 1 09 – Arrays. Mark Dixon Page 2 Questions: Loops What is the value of t, after this code executes? t = 0 For x = 4 To 6 t = t + x Next.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
First tutorial.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Mark Dixon 1 05 – Conditional Execution. Mark Dixon 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6 words) currently.
Mark Dixon Page 1 18 – Web applications: Server-side code (PhP)
Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL.
Mark Dixon 1 11 – Array Variables. Mark Dixon 2 Questions: Loops What is the value of t, after this code executes? t = 0 For x = 4 To 6 t = t + x Next.
By the end of this session you should be able to...
Mark Dixon Page 1 06 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.
Mark Dixon 1 08 – Variables. Mark Dixon 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40) What will.
Computers and Scientific Thinking David Reed, Creighton University Functions and Libraries 1.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Variables. Mark Dixon, SoCCE SOFT 131Page 2 Admin: Test (next week) In class test –teaching week 6 –university week.
Mark Dixon Page 1 03 – Dynamic HTML (client-side scripting)
04/11/ Arrays 1D Arrays Defining, Declaring & Processing.
Mark Dixon Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon 1 03 – Information Processing. Mark Dixon 2 Questions: Events Consider the following code: a) How many unique events does it contain? b) Name.
Mark Dixon, SoCCE SOFT 131Page 1 24 – Datatypes and Object Association.
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)
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Expressions, Operators & Functions.
1.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Mark Dixon Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon Page 1 08 – Variables. Mark Dixon Page 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40)
Mark Dixon Page 1 08 – Variables. Mark Dixon Page 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40)
Mark Dixon 1 06 – Conditional Execution. Mark Dixon 2 Admin: Test (next week) In class test –teaching week 7 50 minutes short answer (5 - 6 words max)
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
COMP1927 Course Introduction 16x1
31/01/ Selection If selection construct.
Mark Dixon Page 1 04 – Conditional Execution. Mark Dixon Page 2 Questions: Expressions a)What is the result of: 10 * Int( ) b)How many functions.
C OMPUTER P ROGRAMMING 1 Input and Variables. I/O S TATEMENTS : I NPUT & V ARIABLES Input is the term used to describe the transfer of information from.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Mark Dixon 1 13 – Parameters. Mark Dixon 2 Question: Arrays How many array variables are in the following code: Dim x Dim y Dim f(4) x = 12 y = 6 f(2)
Introduction to TouchDevelop Lesson 3 – Comments & Lists Created by S. Johnson
Structured Programming (COIT 29222) Term 2, 2009, Final Exam Date And Time  As advised by the university Open/Closed Book (3 hours)  Structured Programming,
Arrays and Loops. Learning Objectives By the end of this lecture, you should be able to: – Understand what a loop is – Appreciate the need for loops and.
Mark Dixon Page 1 09 – Iterative Execution. Mark Dixon Page 2 Questions: Variables Write a line of VBScript code to declare a variable called h Write.
Mark Dixon Page 1 03 – Information Processing: Expressions, Operators & Functions.
Mark Dixon, SoCCE SOFT 136Page 1 08 – Arrays & Structures.
6 – Iterative Execution.
08 – Iterative Execution.
Presentation transcript:

Mark Dixon Page 1 10 – Array Variables

Mark Dixon Page 2 C1 – In-class test Results mixed: –quite a few fails 34% (13/38) –some very good (91% max) ask more questions in tutorials do you understand? –understand roughly (follow when it is explained) –deep understanding, could you explain it in detail to someone else? don't just copy code do all exercises, ask for help (expected)

Mark Dixon Page 3 Questions: Loops What is the value of t, after this code executes? t = 0; for (x = 4; x <= 6; x++){ t = t + x; } 15

Mark Dixon Page 4 Questions: Loops Simplify the following code, so that it is easy to change the number of faces: parFaces.innerHTML = parFaces.innerHTML + " "; parFaces.innerHTML = parFaces.innerHTML + " "; parFaces.innerHTML = parFaces.innerHTML + " "; parFaces.innerHTML = parFaces.innerHTML + " "; parFaces.innerHTML = parFaces.innerHTML + " "; parFaces.innerHTML = parFaces.innerHTML + " "; parFaces.innerHTML = parFaces.innerHTML + " "; var f; for (f = 1; f<=7; f++){ parFaces.innerHTML = parFaces.innerHTML + " "; }

Mark Dixon Page 5 Session Aims & Objectives Aims –To introduce the main concepts involved in handling more complex (multi valued) data Objectives, after this week’s sessions, you should be able to: –declare arrays –assign values to array elements –use array elements –use for loops with arrays

Mark Dixon Page 6 Example: German Numbers User Requirements –describe user's objectives no mention of technology Software Requirements –Functional list facilities to be provided (often numbered) –Non-functional list desired characteristics (often more subjective) SPECIFICATION User Requirements –help people learn German numbers Software Requirements –Functional: –show German word for numbers (between 1 and 10) –user enter digits –check if correct –Non-functional should be easy to use

Mark Dixon Page 7 Example: German Numbers Problem: can't directly pick random word Can: pick random number 1 – eins 2 – zwei 3 – drei 4 – vier 5 – funf 6 – sechs 7 – sieben 8 – acht 9 – neun 10 – zehn

Mark Dixon Page 8 Example: German Numbers v0 var n; function btnStart_onClick(){ n = 1 + parseInt(Math.random() * 10) if (n == 1){ parQuest.innerText = "What is eins?"; }else{ if (n == 2){ parQuest.innerText = "What is zwei?"; }else{ if (n == 3){ parQuest.innerText = "What is drei?"; }else{ if (n == 4){ parQuest.innerText = "What is vier?"; }else{ if (n == 5){ parQuest.innerText = "What is funf?"; }else{ if (n == 6){ parQuest.innerText = "What is sechs?"; }else{ if (n == 7){ parQuest.innerText = "What is sieben?"; }else{ if (n == 8){ parQuest.innerText = "What is acht?"; }else{ if (n == 9){ parQuest.innerText = "What is neun?"; }else{ if (n == 10){ parQuest.innerText = "What is zehn?"; } function btnCheck_onClick(){ if (parseInt(txtAns.value) == n){ parRes.innerText = "Correct!"; }else{ parRes.innerText = "Sorry, please try again." + n; } function btnStart_onClick(){ n = 1 + parseInt(Math.random() * 10) if (n == 1){ parQuest.innerText = "What is eins?"; }else{ if (n == 2){ parQuest.innerText = "What is zwei?"; }else{ if (n == 3){ parQuest.innerText = "What is drei?"; }else{ if (n == 4){ parQuest.innerText = "What is vier?"; }else{ if (n == 5){ parQuest.innerText = "What is funf?"; }else{ if (n == 6){ parQuest.innerText = "What is sechs?"; }else{ if (n == 7){ parQuest.innerText = "What is sieben?"; }else{ if (n == 8){ parQuest.innerText = "What is acht?"; }else{ if (n == 9){ parQuest.innerText = "What is neun?"; }else{ if (n == 10){ parQuest.innerText = "What is zehn?"; } Pick random number Use If statements –one inside another

Mark Dixon Page 9 Array Variables (what) Value multiple values – stored in single variable Index index – identifies individual values (called elements) the value of element 3 is 155 last element

Mark Dixon Page 10 Arrays Variables

Mark Dixon Page 11 Array Variables (Declaration) General syntax: var varname = new Array(lastElement); Specific examples: var HR = new Array(16); var x = new Array(8);

Mark Dixon Page 12 Array Variables (Assignment) General syntax: arrayname[index] = expression Specific examples: HR[0] = 134; HR[5] = b; x[5] = 23.87; x[7] = (y ) / 2;

Mark Dixon Page 13 Questions: Arrays Consider the following code: var x; var res; var age = new Array(2); x = 2; age[0] = 19.6; age[1] = 11.23; age[2] = 15.37; res = age[x]; How many arrays are there? Name the array(s). What is in res after the code executes? age

Mark Dixon Page 14 Arrays: why? (declaration) var Name = new Array(4); Name[0] = "Bob"; Name[1] = "Sally"; Name[2] = "Jo"; Name[3] = "Fred"; Name[4] = "Alison"; var Name1; var Name2; var Name3; var Name4; var Name5; Name1 = "Bob"; Name2 = "Sally"; Name3 = "Jo"; Name4 = "Fred"; Name5 = "Alison"; Single array declaration5 variable declarations

Mark Dixon Page 15 Arrays: why? (use) var Num; Num = parseInt(Math.random() * 5); Res = Name[Num]; var Num; Num = parseInt(Math.random() * 5) if (Num == 0){ Res = Name1; }else if (Num == 1){ Res = Name2; }else if (Num == 2){ Res = Name3; }else if (Num == 3){ Res = Name4; }else{ Res = Name5; } Single line of code picks any element

Mark Dixon Page 16 Example: German Numbers v1 var Nums = new Array(10); var n; function window_onLoad(){ Nums[1] = "eins"; Nums[2] = "zwei"; Nums[3] = "drei"; Nums[4] = "vier"; Nums[5] = "funf"; Nums[6] = "sechs"; Nums[7] = "sieben"; Nums[8] = "acht"; Nums[9] = "neun"; Nums[10] = "zehn"; } function btnStart_onClick(){ n = 1 + parseInt(Math.random() * 10); parQuest.innerText = "What is " + Nums[n] + "?"; } function btnCheck_onClick(){ if (parseInt(txtAns.value) == n){ parRes.innerText = "Correct!"; }else{ parRes.innerText = "Sorry, please try again." + n; } Array Declaration Array Use Array Assignment

Mark Dixon Page 17 Example: German Numbers v0 vs. v1 var Nums = new Array(10); var n; function window_onLoad(){ Nums[1] = "eins"; Nums[2] = "zwei"; Nums[3] = "drei"; Nums[4] = "vier"; Nums[5] = "funf"; Nums[6] = "sechs"; Nums[7] = "sieben"; Nums[8] = "acht"; Nums[9] = "neun"; Nums[10] = "zehn"; } function btnStart_onClick(){ n = 1 + parseInt(Math.random() * 10); parQuest.innerText = "What is " + Nums[n] + "?"; } function btnCheck_onClick(){ if (parseInt(txtAns.value) == n){ parRes.innerText = "Correct!"; }else{ parRes.innerText = "Sorry, please try again." + n; } var n; function btnStart_onClick(){ n = 1 + parseInt(Math.random() * 10) if (n == 1){ parQuest.innerText = "What is eins?"; }else{ if (n == 2){ parQuest.innerText = "What is zwei?"; }else{ if (n == 3){ parQuest.innerText = "What is drei?"; }else{ if (n == 4){ parQuest.innerText = "What is vier?"; }else{ if (n == 5){ parQuest.innerText = "What is funf?"; }else{ if (n == 6){ parQuest.innerText = "What is sechs?"; }else{ if (n == 7){ parQuest.innerText = "What is sieben?"; }else{ if (n == 8){ parQuest.innerText = "What is acht?"; }else{ if (n == 9){ parQuest.innerText = "What is neun?"; }else{ if (n == 10){ parQuest.innerText = "What is zehn?"; } function btnCheck_onClick(){ if (parseInt(txtAns.value) == n){ parRes.innerText = "Correct!"; }else{ parRes.innerText = "Sorry, please try again." + n; } v0v1 25 lines 50 lines

Mark Dixon Page 18 Error: Subscript Out of Range Index too big/small var x = new Array(3); x[0] = 9; x[1] = 5; x[2] = 21; x[3] = 23; x[4] = 12; x = 5;

Mark Dixon Page 19 Error: Type mismatch index missing var x = new Array(3); x[0] = 9; x[1] = 5; x[2] = 21; x[3] = 23; x = 5;

Mark Dixon Page 20 Error: Type mismatch index given for non-array variable: b var x = new Array(3); var b; x[0] = 9; x[1] = 5; x[2] = 21; x[3] = 23; b[3] = 12;

Mark Dixon Page 21 Questions: Arrays Write a line of code that declares an array called Books with 56 elements Write a line of code that assigns the value 45 to the 18 th element of the array. Write some code that makes the background red, but only when the 12 th array element is larger than 6 var Books = new Array(55); Books[17] = 45; if (Books[11] >6){ document.bgColor = "red"; }

Mark Dixon Page 22 Example: Capital Cities SPECIFICATION User Requirements –help people learn Capital Cities Software Requirements –Functional: –ask user for capital of random country –user enter capital –check if correct –Non-functional should be easy to use

Mark Dixon Page 23 Example: Capital Cities Two arrays – stored in same order: CountryCity 0UK 1France 2Spain 3Greece 0London 1Paris 2Madrid 3Athens

Mark Dixon Page 24 Example: Capital Cities var Country = new Array(4); var City = new Array(4); var Num; function window_onLoad(){ Country[1] = "UK"; City[1] = "London"; Country[2] = "France"; City[2] = "Paris"; Country[3] = "Spain"; City[3] = "Madrid"; Country[4] = "Greece"; City[4] = "Athens"; } function btnStart_onClick(){ Num = 1 + parseInt(Math.random() * 3); parQuest.innerText = "What is the capital of " + Country[Num] + "?"; } How many array: –declarations? –assignments?

Mark Dixon Page 25 Question: Arrays Write a statement that will decide whether the answer given by the user is correct: if (txtNum.value == City[Num]){ var Country = new Array(4); var City = new Array(4); var Num; function window_onLoad(){ Country[1] = "UK"; City[1] = "London"; Country[2] = "France"; City[2] = "Paris"; Country[3] = "Spain"; City[3] = "Madrid"; Country[4] = "Greece"; City[4] = "Athens"; } function btnStart_onClick(){ Num = 1 + parseInt(Math.random() * 3); parQuest.innerText = "What is the capital of " + Country[Num] + "?"; }

Mark Dixon Page 26 Example: Drinks v1 Clears array Displays array Total of array Searches array

Mark Dixon Page 27 Example: Drinks v1 var Units = new Array(6); var curUnit; function window_onLoad(){ curUnit = 0; } function btnAdd_onClick(){ Units[curUnit] = parseInt(txtUnit.value); curUnit = curUnit + 1; } function btnClear_onClick(){ Units[0] = 0; Units[1] = 0; Units[2] = 0; Units[3] = 0; Units[4] = 0; Units[5] = 0; Units[6] = 0; curUnit = 0; } function btnShow_onClick(){ lblRes.innerText = ""; lblRes.innerText = lblRes.innerText + Units[0] + " "; lblRes.innerText = lblRes.innerText + Units[1] + " "; lblRes.innerText = lblRes.innerText + Units[2] + " "; lblRes.innerText = lblRes.innerText + Units[3] + " "; lblRes.innerText = lblRes.innerText + Units[4] + " "; lblRes.innerText = lblRes.innerText + Units[5] + " "; lblRes.innerText = lblRes.innerText + Units[6]; } ….

Mark Dixon Page 28 Example: Drinks v1 …. function btnTotal_onClick(){ var total; total = 0; total = total + Units[0]; total = total + Units[1]; total = total + Units[2]; total = total + Units[3]; total = total + Units[4]; total = total + Units[5]; total = total + Units[6]; lblRes.innerText = total; } function btnFind_onClick(){ if (txtUnit.value == Units[0]){ lblRes.innerText = "Found in slot 0"; }else if (txtUnit.value == Units[1]){ lblRes.innerText = "Found in slot 1"; }else if (txtUnit.value == Units[2]){ lblRes.innerText = "Found in slot 2"; }else if (txtUnit.value == Units[3]){ lblRes.innerText = "Found in slot 3"; }else if (txtUnit.value == Units[4]){ lblRes.innerText = "Found in slot 4"; }else if (txtUnit.value == Units[5]){ lblRes.innerText = "Found in slot 5"; }else if (txtUnit.value == Units[6]){ lblRes.innerText = "Found in slot 6"; }else{ lblRes.innerText = "Not Found"; }

Mark Dixon Page 29 Array Algorithms Common tasks to many programs: –Reset all elements –Display all elements –Total all elements –Search all elements –Find maximum value –Find minimum value –Average –Sort

Mark Dixon Page 30 Example: Drinks v2 (Reset) Use loop counter variable (i) as array index:

Mark Dixon Page 31 Example: Drinks v2 (Total)

Mark Dixon Page 32 Tutorial Exercise: German Numbers Task 1: Complete German Numbers Example from lecture. You will need to complete the code for checking the user's answer Task 2: Modify your page so that it disables and enables the buttons appropriately Task 3: Modify your page so that the text box gets the focus (cursor) after the start button is pressed Task 4: Modify your page to allow the user 3 attempts only. Task 5: Modify your page to prevent same random number appearing twice –store used numbers –use while loop, new value different from previous Task 6: Modify your page so that it plays appropriate sounds when the user gets the answer right/wrong

Mark Dixon Page 33 Tutorial Exercise: Capital Cities Task 1: Complete Capital Cities Example from the lecture, adding some more cities. You will need to complete the code for checking the user's answer Task 2: Modify your page so that it hides and shows the buttons appropriately Task 3: Modify your page to allow the user 3 attempts only. Task 4: Modify your page so that it is case in-sensitive (i.e. user can type upper or lower case) Task 5: Modify your page so that it displays an appropriate picture of the selected capital city. Hint: create another array for the file names. Task 6: Modify your page so that it plays appropriate sounds when the user gets the answer right/wrong

Mark Dixon Page 34 Tutorial Exercise: Drinks Task 1: Get the Drinks v2 example (from the lecture) working. You have the code for Add, Clear, & Show but not for Total and Find Task 2: Modify your page so that it displays a meaningful message when all elements of the array are used up (not the error dialogue below).