Lecture 16 Arrays: Part 2 COMP1681 / SE15 Introduction to Programming.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
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,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Introduction to arrays Data in economy size packages.
An Introduction to Sorting Chapter 9. 2 Chapter Contents Selection Sort Iterative Selection Sort Recursive Selection Sort The Efficiency of Selection.
Arrays Chapter 6 Chapter 6.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Selection Sort -Reading p Reading p
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
CS102--Object Oriented Programming Lecture 5: – Arrays – Sorting: Selection Sort Copyright © 2008 Xiaoyan Li.
An Introduction to Sorting Chapter 8. 2 Chapter Contents Selection Sort Iterative Selection Sort Recursive Selection Sort The Efficiency of Selection.
Java: How to Program Arrays and ArrayLists Summary Yingcai Xiao.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
Searches & Sorts V Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.
Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search ; Reading p Selection Sort ; Reading p
Sorting Arrays. Selection Sort  One of the easiest ways to sort the elements of an array is by using the selection sort algorithm.  Assume that the.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Intro to CS – Honors I Basic Sorting GEORGIOS PORTOKALIDIS
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 10 l Array Basics l Arrays in Classes and Methods l Programming.
1 Week 9 l Array Basics l Arrays in Classes and Methods l Programming with Arrays and Classes l Sorting Arrays l Multidimensional Arrays Arrays.
COS 260 DAY 16 Tony Gauvin.
Lecturer: Dr. AJ Bieszczad Chapter 6 COMP 150: Introduction to Object-Oriented Programming 6-1 l Array Basics l Arrays in Classes and Methods l Programming.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming with Arrays and Classes l Sorting Arrays l Multidimensional Arrays Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Chapter 10. Arrays Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Computer Programming with JAVA.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
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.
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.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
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.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays Chapter 7.
Examples of Classes & Objects
Arrays 3/4 By Pius Nyaanga.
Chapter 7 Part 1 Edited by JJ Shepherd
Searching and Sorting Linear Search Binary Search ; Reading p
Chapter 7 Part 2 Edited by JJ Shepherd
Review of Classes and Arrays
CSS161: Fundamentals of Computing
Programming with Arrays 2
Arrays 3/4 June 3, 2019 ICS102: The course.
Chapter 6 Arrays.
Review of Classes and Arrays
1D Arrays and Lots of Brackets
Presentation transcript:

Lecture 16 Arrays: Part 2 COMP1681 / SE15 Introduction to Programming

SE15: Arrays15–2 Today’s Learning Objectives Find out what can go wrong with Arrays Learn how to write methods that use arrays as parameters or return arrays Learn how to use an array as an instance variable in a class Introduce the topic of sorting an Array

SE15: Arrays15–3 Lecture Outline Recap on Arrays Creating and accessing Arrays Common problems Arrays as method arguments Methods that return Arrays Searching Arrays Sorting Arrays Savitch sections 6.3 and 6.4

SE15: Arrays15–4 Indexed Variables as Method Arguments An indexed variable for an array a, such as a[i], can be used anywhere that you can use any other variable of the base type. So an indexed variable can be an argument to a method in exactly the same way as any other variable of the array’s base type can be used as an argument. printBar(temperature[index]);

SE15: Arrays15–5 Entire Arrays as Method Arguments The way you specify an array parameter in a method definition is similar to the way you declare an array. public static double calcAverage(double[] a) { double sum = 0; int i =0; for(i=0; i < a.length; i++) { sum = sum + a[i]; } return(sum/a.length); }

SE15: Arrays15–6 Methods that Return Arrays In Java a method may return an array. You specify the return type for a method in the same way that you specify a type for an array parameter. public static double[] calcDiffFromAverage(double[] a) { double average = calcAverage(a); double[] diff = new double [a.length]; for(int i=0; i < diff.length; i++) { diff[i] = Math.abs(a[i] - average); } return diff; }

SE15: Arrays15–7 Partially Filled arrays In some situations you need some but not all of the indexed variables in an array, such as a list that is not yet full. In these situations you need to keep track of how much of the array is used. This is normally done with an int variable such as the instance variable bookCount in the Library Class which tells the methods that the array consists of the array elements 0 – bookCount-1

SE15: Arrays15–8 Sorting Arrays Sort an array of ints to arrange the elements so that a[0] is the smallest, a[1] is the next smallest and so forth. for (index=0; index < a.length; index++) Place the index th smallest element in a[index] Savitch 6.4

SE15: Arrays15–9 Selection Sort a[0]a[1]a[2]a[3]a[4]a[5]a[6]Unsorted

SE15: Arrays15–10 Selection Sort for (index=0; index < a.length; index++) { // Place the correct value in a[index] indexOfNextSmallest = an index of the smallest value among a[index], a[index+1],…,a[a.length-1]; Interchange the values of a[index] and a[indexOfNextSmallest]; }

SE15: Arrays15–11 Summary Looked at example programs that Illustrate the pitfalls Show how to pass and return Arrays in methods Demonstrate how to use arrays as instance variables How to sort arrays using selection sort

SE15: Arrays15–12 Follow-up Work Savitch chapter 6 Re-write the sort program to sort an Array of Strings into Alphabetical order.