Arrays: Iteration Working through an array algorithmically.

Slides:



Advertisements
Similar presentations
LOOPS Loops are lines of code that can be run more than one time. Each time is called an iteration and in for loops there is also an index that is changing.
Advertisements

Basic Algorithms on Arrays. Learning Objectives Arrays are useful for storing data in a linear structure We learn how to process data stored in an array.
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
CS0007: Introduction to Computer Programming Introduction to Arrays.
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
Introduction to Programming Workshop 2 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
Loops CS 103 February 19, 2007 Presented by Nate.
Working with arrays (we will use an array of double as example)
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
Loops CS 103 February 13, 2009 Author: Nate Hamm.
Fundamentals of Algorithms MCS - 2 Lecture # 11
Trace Tables In today’s lesson we will look at:
Canvas and Arrays in Apps
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Intro CS – Loops & Creating Shapes
Repetition Structures Chapter 9
CS1010 Discussion Group 11 Week 8 – Searching and Sorting.
Simple Sorting Algorithms
Java's for Statement.
CS1371 Introduction to Computing for Engineers
Class07 PHP: loops and includes
slides created by Marty Stepp
Topics Introduction to Repetition Structures
Linked node problem 3 What set of statements turns this picture:
Programming for Art: Algorithms
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
Manipulating Pictures, Arrays, and Loops part 2
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Arrays, For loop While loop Do while loop
What is an Array? Why Arrays? Programming Examples.
Python I/O.
Introduction to Processing Digital Sounds part 2
MATLAB – Basic For Loops
Writing Methods AP Computer Science A.
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Unit 3: Lesson 9-Looping and Random Numbers
For Loops.
Topics Introduction to Repetition Structures
Do … Loop Until (condition is true)
Then I use the monthArray and put the index/subscript/pointer in square bracketts to define the specific one that can be referenced by that name with that.
slides created by Marty Stepp and Hélène Martin
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Some Common Issues: Iteration
What's wrong with Easter jokes? They crack you up
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
A LESSON IN LOOPING What is a loop?
Loops and Arrays in JavaScript
Suggested self-checks: Section 7.11 #1-11
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
Manipulating Pictures, Arrays, and Loops
CS100J Lecture 14 Previous Lecture
Input a number and print its table, using FOR loop.
Introduction to Programming
Print the following triangle, using nested loops
The for-statement.
slides adapted from Marty Stepp
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
Arrays.
Insertion Sort and Shell Sort
slides created by Marty Stepp
Software Development Techniques
while Loops Looping Control Statement
Functions Overview © 2018 Kris Jordan.
Visual Studio Code Walkthrough
The main Function, introcs Library, and Prompting
if-then-else Conditional Control Statement
Parameters and Arguments
The for Loop © 2018 Kris Jordan.
Presentation transcript:

Arrays: Iteration Working through an array algorithmically. © 2018 Kris Jordan

Example Setup import { print } from "introcs"; export let main = async () => { let a: number[] = [1, 2, 3]; print(a); a[0] = a[0] * a[0]; a[1] = a[1] * a[1]; a[2] = a[2] * a[2]; }; main(); In VSCode: Start the Development Server View Terminal npm run pull npm start Open the File Explorer Pane Right click on the src folder Select "New folder" Name it: x-arrays Right click on the x-functions folder Select "New file" Name it: array-loop-app.ts In array-loop-app.ts, write out the code to the right. It has no errors, so review carefully if yours has any. © Kris Jordan

Processing an Array Manually Suppose you have silly problem: You want to square each element of a number array with length 3. Here's one way to do so: let a: number[] = [1, 2, 3]; print(a); a[0] = a[0] * a[0]; a[1] = a[1] * a[1]; a[2] = a[2] * a[2]; © Kris Jordan

Notice there's a pattern... Each of these lines is exactly the same, only the index number in each of them is changing: What if we introduced an index variable and incremented it between each line: Now each of those lines is exactly the same and we're repeating them over and over... a[0] = a[0] * a[0]; a[1] = a[1] * a[1]; a[2] = a[2] * a[2]; let i = 0; a[i] = a[i] * a[i]; i++; © Kris Jordan

Processing an Array with a Loop When you find yourself writing repetitive code, ask yourself: Could I let a loop handle the repetition for me? In this case... yes! Notice by starting i at 0 and repeating while i < a.length we now have an algorithm for squaring an array of numbers of any length! for (let i = 0; i < a.length; i++) { a[i] = a[i] * a[i]; } © Kris Jordan