The median again The steps of our algorithm: Read the size of the list, N. Declare and instantiate an array of integers, "list". Read the elements of list.

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.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Arrays.
Chapter 5 Arrays and Vectors An array allows you to group data items together in a structure that is processed via an index. They allow you to process.
Liang, Chpt 5 continued. Sorting arrays The telephone book is easy to use, because the entries are sorted Sorting is a common task, and many many algorithms.
Arrays Chapter 6 Chapter 6.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
Horstmann chapter 8 continued. Sorting arrays The telephone book is easy to use, because the entries are sorted Sorting is a common task, and many many.
1 CS100J 14 March 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Chapter 9: Arrays and Strings
Building Java Programs Chapter 7.5
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
1 CS100J 25 October 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
7. Arrays. Topics Declaring and Using Arrays Some Array Algorithms Arrays of Objects Variable Length Parameter Lists Two-Dimensional Arrays The ArrayList.
Arrays. Arrays in Java  Arrays in Java are objects.  Like all objects are created with the new keyword.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Arrays Adapted from materials created by Dr. Donald Bell, Cal Poly 2000 (updated February 2004)
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
Get Longest Run Index (FR) public int getLongestRunIndex(int []values) { int maxRunStart = -1, maxRunLength = 1; int runStart = 0, runLength = 1; for(int.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
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.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Arrays Chapter 7. 2 Declaring and Creating Arrays Recall that an array is a collection of elements all of the _____________ Array objects in Java must.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Two Dimensional Arrays. Students will be able to: code algorithms to solve two- dimensional array problems. use 2-D arrays in programs. pass two-use 2-D.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Vector program patterns Vectors contain many elements, so loops are common. Counted processing // Print the first 3 elements. for (int i = 0; i < 3; i++)
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
COS 312 DAY 12 Tony Gauvin. Ch 1 -2 Agenda Questions? First Progress report due Assignment corrected 1 MIA – 2 A’s, 2 B’s, 1 D, 1 F and 1 MIA – Code\pig.
ARRAYS Multidimensional realities Image courtesy of
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
1 CS Oct 2008 Arrays. Reading: Secs 8.1, 8.2, 8.3 Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long, and.
Java Programming Language Lecture27- An Introduction.
Data Structures (CS212D) Week # 2: Arrays.
Outline Declaring and Using Arrays Arrays of Objects
Presentation transcript:

The median again The steps of our algorithm: Read the size of the list, N. Declare and instantiate an array of integers, "list". Read the elements of list. Check each element to see if it's the median. The last step goes like this: for (int i = 0; i < N; i++) if (/* list[i] is the median */) { System.out.println("median = " + list[i]; break; }

Exercise Write a method to count the members of an array that are smaller than some value: int countSmaller (int refVal, int[] list)

An answer for the exercise // countSmaller: return the number of entries // in list that are smaller than refVal. int countSmaller (int refVal, int[] list) { int count = 0; for (int i = 0; i < list.length; i++) if (list[i] < refVal) count++; return count; } // or... count += (list[i] < refVal) ? 1 : 0;

Finishing off the median Now that we have countSmaller( ), if (/* list[i] is the median */) becomes if (countSmaller(list[i], list) == list.length/2) -- so our median-finding loop is: for (int i = 0; i < list.length; i++) if (countSmaller(list[i], list) == list.length/2) System.out.println("median = " + list[i]; break; }

Another exercise Rewrite all this median-finding stuff as a method: int median (int[] list) Does it return the value of the median or the index of the list element that is the median?

The parameter of main( ) public static void main (String[] args) If your program contains: for (int i = 0; i < args.length; i++) System.out.println(args[i]); then it will print the arguments of the command that started the program. How does the command get arguments? java Program hi there or in CodeWarrior, set "Parameters" in the "Java Target" area of the Project Manager window.

Two-dimensional arrays A table of marks: A1A2Test Jim2815 Mary9718 Jim's row is row 0; Mary's row is row 1. A1's column is column 0; A2's column is column 1. col 0col 1col 2 row 0(0,0)(0,1)(0,2) row 1(1,0)(1,1)(1,2) We can't do this with a simple array (or Vector); we need something 2-D.

Using a two-dimensional array Declaring: int[][] table; Creating: table = new int[2][3]; // [row size][column size] Filling with values: for (int row = 0; row < table.length; row++) for (int col = 0; col < table[0].length; col++) table[row][col] = Integer.parseInt(in.readLine());

Arrays vs Vectors 1.Vectors can grow (and shrink). Arrays cannot. 2.Vectors are just another class. Arrays are a fundamental part of the Java language. 3.Vectors can contain a mixture of types. An array can contain only one type of element. 4.Vector elements must be (references to) objects. An array's element type can be either a class type or a primitive type. Vectors can be inefficient, while sometimes arrays are not inefficient in the same ways. But "inefficiency" is always "inefficient in the context of a particular application", and anyway we don't have the tools even to talk about efficiency properly yet.

An array example: the transpose A 2-D array corresponds to a mathematical matrix: One simple matrix operation transposes the matrix elements: