Lec 21 More Fun with Arrays: For Loops

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
The switch Statement, DecimalFormat, and Introduction to Looping
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
A revision guide for programming with python. 1.A program is a set of instructions that tells a computer what to do. 2.The role of a programmer is to.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Lec 19 Array Intro. Agenda Arrays—what are they Declaring Arrays Constructing Arrays Accessing Array cells.
Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Lec 20 More Arrays--Algorithms. Agenda Array algorithms (section 7.5 in the book) – An algorithm is a well-defined specification for solving a problem.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Agenda Basic Logic Purpose if statement if / else statement
Overview Go over parts of quiz? Another iteration structure for loop.
Lec 21 More Fun with Arrays: For Loops. Agenda Some backfill for Lab 20: – Using an array in an applet or class – array instance variables – using Math.random()
Often being different. Control flow By default Java (and therefore Processing) executes lines of a program one after the other –Doesn’t matter what happened.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Chapter 5: Loops Tarik Booker CS 201 California State University, Los Angeles.
UCT Department of Computer Science Computer Science 1015F Iteration
The for Statement A most versatile loop
Arrays Chapter 7.
CS 106A, Lecture 27 Final Exam Review 1
Visual Basic 6.0 Final Review
CSC111 Quick Revision.
CprE 185: Intro to Problem Solving (using C)
REPETITION CONTROL STRUCTURE
Introduction To Repetition The for loop
The switch Statement, and Introduction to Looping
GC211Data Structure Lecture2 Sara Alhajjam.
Chapter 5: Control Structures II
ECS10 10/10
Lecture 4: Program Control Flow
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Repetition-Counter control Loop
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
LOOPS.
MATLAB: Structures and File I/O
Cookies BIS1523 – Lecture 23.
Siti Nurbaya Ismail Senior Lecturer
Programming for Art: Algorithms
Arrays.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
File Handling Programming Guides.
Lecture Notes – Week 3 Lecture-2
PC01 Term 3 Project Snake. PC01 Term 3 Project Snake.
Algorithm Discovery and Design
Search,Sort,Recursion.
Lec 4: while loop and do-while loop
Lecture 13: Two-Dimensional Arrays
Lecture 5: For Loops Building Java Programs: A Back to Basics Approach
Building Java Programs
Arrays.
Lecture 8:The For Loop AP Computer Science Principles.
Console.WriteLine(“Good luck!”);
SSEA Computer Science: Track A
Search,Sort,Recursion.
Building Java Programs
For loops Taken from notes by Dr. Neil Moore
CS2011 Introduction to Programming I Selections (I)
Lecture Notes – Week 2 Lecture-2
Java Programming Loops
Suggested self-checks: Section 7.11 #1-11
Building Java Programs
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Decisions, decisions, decisions
Chap 7. Advanced Control Statements in Java
Agenda Packages and classes Types and identifiers Operators
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Presentation transcript:

Lec 21 More Fun with Arrays: For Loops

Agenda Some backfill for Lab 20: For Loops vs While loops Using an array in an applet or class array instance variables For Loops vs While loops not that big a deal Nested for Loops For Loops and Arrays more ways to process Plotting data on the screen

For loops Demo While loop that prints the numbers 1 to 10 For loop that does the same thing While loop that adds the numbers from 50 to 60 Declaring the loop counter in the for header scope limited to loop "body" Changing the loop increment Ascending or descending loops Nested for loops – used to draw a grid of squares

Arrays – Using for loops For loops are a more compact way of processing arrays Instead of this: System.out.print(a[0]); System.out.print(a[1]); System.out.print(a[2]); System.out.print(a[3]); System.out.print(a[4]); System.out.print(a[5]); You can do this: for (k=0; k<6; k++) System.out.print(a[k]);

For Loop and Array Demo Assigning a sequence of values to an array suppose we want data to have 10,20,30,40,50 Putting a set of random values into an array Defining array cell k using previous cell k-1 new value = old value / 2 Counting how many 3's are in an array

Searching for a value in an array Suppose you want to know if 13 is in the data array. Naive approach: for (int k=0; k<11; k++){ if (data[k]==13) output "found 13 at cell" + k else output "13 not found" } the problem ? if data[0] doesn't match, you can't tell after looking at one cell that it's not there

A solution? assume value 13 is not in the array boolean found = false; visit each cell in the array, if there's a match display "found 13 at cell " + k, set found to true otherwise, do nothing (no else) but continue to check next cell After loop, if found is still false, say "not found"

Finding the max in the array: set max = 0 visit every cell using counter k if data cell k is bigger than max set max to data cell value at position k set maxindex equal to k (to remember the k value) when the loop ends, max will have largest value in array, maxindex will tell you which cell it was in

Quiz Determine the values generated and stored in the following array int [] data = new int[8]; data[0] = 5; for (k=1; k<8; k++) data[k]= data[k-1]*2;

Quiz 2. Determine the values generated and stored in the following array int [] data2 = new int[8]; for (k=0; k<8; k++) data2[k]=k+100;

Final Project