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.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
1 Various Methods of Populating Arrays Randomly generated integers.
Lecture 1: Comments, Variables, Assignment. Definitions The formal (human-readable) instructions that we give to the computer is called source code The.
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Chapter 7 – Arrays.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
Chapter 7 & 8- Arrays and Strings
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
A tour around Java General introduction to aspects of the language (these will be covered in more detail later) After this tour you should have a general.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 27: More on ArrayList, Reference Semantics Command Line Arguments reading:
Sadegh Aliakbary Sharif University of Technology Spring 2011.
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.
+ ARRAYS - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015.
Programming Fundamentals I (COSC-1336), Lecture 8 (prepared after Chapter 7 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 8.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
DT249-Information Systems Research Practice Programming Revision Lecture 2 Lecturer: Patrick Browne.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Methods We write methods in our programs for many reasons:
Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println10. Testing of output is restricted to System.out.print and System.out.println.
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()
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?
Arrays An array is a data object that can hold multiple objects, all of the same type. We can think of an array as a storage box which has multiple compartments.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
COP 3540 Data Structures with OOP
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
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];
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Data Structures Arrays and Lists Part 2 More List Operations.
OOP Basics Classes & Methods (c) IDMS/SQL News
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 4 GEORGE KOUTSOGIANNAKIS Copyright: 2016 Illinois Institute of Technology/George Koutsogiannakis 1.
Bubble sort. Quite slow, but simple Principles: Compare 2 numbers next to each other (lets call it current and the one next to it) If the current number.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Midterm 2 Review Notes on the CS 5 midterm Take-home exam due by 5:00 pm Sunday evening (11/14) Hand in your solutions under the door of my office, Olin.
Introduction to programming in java Lecture 21 Arrays – Part 1.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
CSC111 Quick Revision.
Repetition (While-Loop) version]
Functions, variables, operators, loops Modularity
Building Java Programs Chapter 7
Building Java Programs
Lecture Notes – Week 4 Chapter 5 (Loops).
Building Java Programs
Review for Midterm 3.
Lec 17 Using Nested Loops and Objects in an Applet Class
Lec 21 More Fun with Arrays: For Loops
Presentation transcript:

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 – So an array algorithm is a well-defined specification for solving a problem involving arrays – Ask class: What stuff would you like to see done using arrays?

Some examples of array algorithms (section 7.5 in the book) – ``Resizing'' arrays (not in book?) Actually, you can't resize an array, you just make a bigger array and copy everything over – Testing equality of arrays Don't use the equals method on arrays; it does the same thing as == in this case – Counting matches – Finding a value – Finding minimum/maximum – Copying arrays (section 7.7)

4 Quiz Draw the following arrays int [] data = new int[8]; for (k=0; k<8; k++) data[k]=k; int [] data2 = new int[8]; for (k=0; k<8; k++) data2[k]=k-5; int [] data3 = new int[8]; for (k=0; k<8; k++) data3[k]=7-k;

5 How to Swap ?? One of the most useful algorithms Swap two int variables: int x=3, y=5, temp; 1) temp=x; 2) x=y; 3) y=temp; for swapping array cells, replace x with data[k], replace y with data[m] in lines 1) to 3) xytemp ?333?333

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

Two-dimensional arrays (section 7.6, advanced topic 7.2) – We start with a couple of observations: 1) Every array is defined to store some particular kind of object (or primitive) 2) Any array itself is an object – From observations 1) and 2) we conclude: We can define an array that holds other arrays! Such an array is called a two-dimensional array – To see how the syntax works, see example next slideexample Also see memory diagram representing the state after the above code is executedmemory diagram See diagram for a different way to conceptualize 2D arraysdiagram – We can also generalize these ideas to any number of dimensions we want

public class TwoDimensionalArray { public static void main(String[] args) { //Declaring a two dimensional array, with initialization that //already creates the secondary arrays for you double[][] a = new double[3][2]; System.out.println(a[2][1]); //prints 0.0 //Now let's do it "by hand" double[][] b = new double[3][]; System.out.println(b[2]); //This will print null at this point b[0] = new double[2]; b[1] = new double[2]; b[2] = new double[2]; b[0][1] = 3.2; System.out.println(b[0][1]); //prints 3.2 }

Lab20 More Practice with Arrays You'll be creating an applet that uses arrays to store a number of words, and select and combine them randomly.