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()

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
One Dimensional Arrays
Logic & program control part 3: Compound selection structures.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
CSE 115 Week 12 March 31 – April 4, Announcements March 31 – Exam 8 March 31 – Exam 8 April 6 – Last day to turn in Lab 7 for a max grade of 100%,
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Recognizing Patterns Counting: continually updating a value by a fixed amount Counting raindrops int dropCount = 0; //Raindrop counter while (dropCount.
Today’s quiz on 8.2 A Graphing Worksheet 1 will be given at the end of class. You will have 12 minutes to complete this quiz, which will consist of one.
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.
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
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.
CPSC 171 Introduction to Computer Science 3 Levels of Understanding Algorithms More Algorithm Discovery and Design.
For each primitive type there is a wrapper class for storing values of that type: Double d = new Double(29.95); Wrapper Classes Wrapper objects can be.
1 Arrays Chapter 13 Especially read 13.1 – 13.2 Text introduces vectors, which we will not cover, in 13.3.
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.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Lists in Python.
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
Chapter 2 - Algorithms and Design
CSC 204 Programming I Loop I The while statement.
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.
Standard Algorithms –search for an item in an array –count items in an array –find the largest (or smallest) item in an array.
By the end of this session you should be able to...
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
Semester Review. As we have discussed, Friday we will have in class time for you to work on a program, this program will come with instructions and you.
1 Three C++ Looping Statements Chapter 7 CSIS 10A.
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.
CompSci Arrays  Aggregate data type  Deal with items of same type  Lists of words  Numbers  Analogies  Mailboxes in post office  CD racks.
Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.
CS101 Computer Programming I Chapter 4 Extra Examples.
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.
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)
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
Overview Go over parts of quiz? Another iteration structure for loop.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
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.
Lecture 13: Arrays, Pointers, Code examples B Burlingame 2 Dec 2015.
More Array Access Examples Here is an example showing array access logic: const int MAXSTUDENTS = 100; int Test[MAXSTUDENTS]; int numStudents = 0;... //
Week 61 Introduction to Programming Ms. Knudtzon C Period Tuesday October 12.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Aquarium Lab Series Developed by Alyce BradyAlyce Brady of Kalamazoo CollegeKalamazoo College.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
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 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Data Structures Arrays and Lists Part 2 More List Operations.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
CS 116 Object Oriented Programming II Lecture 4 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Lecture 5: Layers of Control. Nested while Loops Problem Multiplying two numbers and outputting the result only if they are both less than 5. (i.e. Start.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Introduction to programming in java Lecture 21 Arrays – Part 1.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Lecture Notes – Week 3 Lecture-2
Lecture 5: For Loops Building Java Programs: A Back to Basics Approach
Lecture 8:The For Loop AP Computer Science Principles.
Lecture Notes – Week 2 Lecture-2
Decisions, decisions, decisions
Lec 21 More Fun with Arrays: For Loops
Presentation transcript:

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() just like you did in Lab5Lab5 index = Math.round(Math.random()*adjectives.length ); For Loops vs While loops – not that big a deal For Loops and Arrays – more ways to process Frogs and Toads – Pad class

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 – For loop that does the same thing 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

4 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

9 Quiz Draw the following arrays int [] data = new int[8]; data[0] = 5; for (k=1; k<8; k++) data[k]= data[k-1]*2; int [] data2 = new int[8]; for (k=0; k<8; k++) data2[k]=k+100;

Final Project

Each Week We'll spend Thursdays going over another aspect of the final project You'll get more out of these sessions if you foray ahead on your own before hand Extra credit: 1 point for each day you hand in early!! up to 20 max DO NOT hand your solutions to another student— you'll both get zero for whole project (one grade lower for class) It's supposed to be a challenge—do your best – I expect you to refer to previous labs/solutions for help