An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
One Dimensional Arrays
Understanding the Need for Sorting Records
Chapter 9: Advanced Array Manipulation
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
Visual C++ Programming: Concepts and Projects
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
An Introduction to Programming with C++ Fifth Edition
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
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.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Chapter 9: Arrays and Strings
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Introduction to Programming with C++ Fourth Edition
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Chapter 9 Introduction to Arrays
Programming Logic and Design Fourth Edition, Comprehensive
Searching and Sorting Arrays
Ordered Arrays An array is ordered if the elements are in ascending or descending order. The array may be ordered numerically or alphabetically (which.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
IE 212: Computational Methods for Industrial Engineering
Chapter 9: Advanced Array Concepts
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Chapter 8 Arrays and Strings
Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 12 Manipulating Larger Quantities of Data.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
IT259 Foundation of Programming Using Java Unit 9 Seminar : (Chapter 8 ) Instructor : Vladimir Gubanov, PhD
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Chapter 9: Sorting and Searching Arrays
Arrays 2.
Computer Programming BCT 1113
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 5: Arrays: Lists and Tables
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Starting Out with Programming Logic & Design
CHAPTER 9 SORTING & SEARCHING.
Arrays.
Presentation transcript:

An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts

An Object-Oriented Approach to Programming Logic and Design 2 Objectives Remain within array bounds Use a for loop to process arrays Declare an array of objects Pass arrays to methods Sort array elements

An Object-Oriented Approach to Programming Logic and Design 3 Objectives (continued) Sort arrays of objects Use two-dimensional and multi-dimensional arrays Use a built-in Arrays class

An Object-Oriented Approach to Programming Logic and Design 4 Remaining Within Array Bounds An array has a finite size Size measured by number of elements or by number of bytes in the array Because an array’s elements are all the same type, array size in bytes is a multiple of the number of elements Out of bounds: when you try to use a subscript that is not within the range of the declared array’s subscripts

An Object-Oriented Approach to Programming Logic and Design 5 Remaining Within Array Bounds (continued)

An Object-Oriented Approach to Programming Logic and Design 6 Remaining Within Array Bounds (continued)

An Object-Oriented Approach to Programming Logic and Design 7 Using a for Loop to Process Arrays for loop can step through each element of an array Set the for loop to start at 0 and end at the highest subscript Remember: the highest subscript is one less than the number of elements (array size) A named constant can be used for the upper limit of the for loop

An Object-Oriented Approach to Programming Logic and Design 8 Using a for Loop to Process Arrays (continued)

An Object-Oriented Approach to Programming Logic and Design 9 Using a for Loop to Process Arrays (continued) A more efficient way:

An Object-Oriented Approach to Programming Logic and Design 10 Declaring an Array of Objects An array can hold elements of any type, such as numbers, strings, objects, etc. For an array of objects, declare the array with the type of the object (i.e., the class name) Examples: Employee emp[7] Employee emp[NUM_EMPLOYEES]

An Object-Oriented Approach to Programming Logic and Design 11 Declaring an Array of Objects (continued) If the class constructor requires parameters, you must provide the values when you declare the object array Example: Employee emp[NUM_EMPLOYEES] = {101, 12.45}, {103, 7.50}, {119, 13.25}, {213, 15.00}, {218, 8.40}, {395, 16.00}, {405, 9.00}

An Object-Oriented Approach to Programming Logic and Design 12 Declaring an Array of Objects (continued) When using an object’s method, place the subscript after the array name and before the dot preceding the method name Example: numeric MAX = NUM_EMPLOYEES – 1 for x = 0 to MAX print emp[x].getEmpNum(),” “, emp[x].getEmpSal() endfor

An Object-Oriented Approach to Programming Logic and Design 13 Passing Arrays to Methods An array element can be passed as a parameter just like any simple variable An array element is passed “by value” Only a copy of the value of the array element is given to the method; the original array element remains unchanged

An Object-Oriented Approach to Programming Logic and Design 14 Passing Arrays to Methods (continued)

An Object-Oriented Approach to Programming Logic and Design 15 Passing Arrays to Methods (continued)

An Object-Oriented Approach to Programming Logic and Design 16 Passing Arrays to Methods (continued) You can pass an entire array as a parameter The array is passed “by reference”: a pointer to the original array is given to the method Any changes made to the array in the method are made to the actual array

An Object-Oriented Approach to Programming Logic and Design 17 Passing Arrays to Methods (continued) Example: constant numeric NUM_ELEMENTS = 4 constant numeric MAX = NUM_ELEMENTS - 1 numeric someNums[NUM_ELEMENTS] = 5, 10, 15, 20 methodGetsArray(someNums,MAX)

An Object-Oriented Approach to Programming Logic and Design 18 Passing Arrays to Methods (continued)

An Object-Oriented Approach to Programming Logic and Design 19 Passing Arrays to Methods (continued)

An Object-Oriented Approach to Programming Logic and Design 20 Sorting Array Elements Sorting: arranging a series of objects in some logical order Ascending order: smallest at the beginning, largest at the end Descending order: largest at the beginning, smallest at the end Sorting can be done by comparing two values, and swapping their positions to achieve the correct order

An Object-Oriented Approach to Programming Logic and Design 21 Sorting Array Elements (continued) Use a temporary variable to hold the current value when swapping values Example: If valA > valB then temp = valA valA = valB valB = temp endif

An Object-Oriented Approach to Programming Logic and Design 22 Sorting Array Elements (continued) Bubble sort: compare pairs of items, swapping if out of order, until the smallest item “bubbles” up to the top of the list Steps: 1.Place all values to be sorted into an array 2.Compare the first two numbers in the array 3.If not in order, swap them 4.Compare 3rd value with 2nd value and swap if necessary 5.Continue will all values in same manner

An Object-Oriented Approach to Programming Logic and Design 23 Sorting Array Elements (continued) The list may have to be processed several times to get all items into order. for b = 0 to ARRAY_SIZE - 2 if someNums[b] > someNums[b + 1] then temp = someNums[b] someNums[b] = someNums[b + 1] someNums[b + 1] = temp endif endfor

An Object-Oriented Approach to Programming Logic and Design 24 Sorting Array Elements (continued) At first pass, the largest value “sinks” to the bottom of the list Therefore, you do not have to compare the last value on the next pass. Each successive pass through the array places the next largest value in the element in the next last position, so one less value needs to be compared

An Object-Oriented Approach to Programming Logic and Design 25 Sorting Array Elements (continued)

An Object-Oriented Approach to Programming Logic and Design 26 Sorting Arrays of Objects Arrays of objects can be sorted in a similar fashion The comparison of objects may be different Objects are usually compared by comparing a field value Example: Sort an Employee object by calling getEmpSal()

An Object-Oriented Approach to Programming Logic and Design 27 Sorting Arrays of Objects (continued)

An Object-Oriented Approach to Programming Logic and Design 28 Using Two-Dimensional and Multidimensional Arrays A one-dimension array can be envisioned as a column of values.

An Object-Oriented Approach to Programming Logic and Design 29 Using Two-Dimensional and Multidimensional Arrays (continued) A two-dimension array can be envisioned as a table, with rows and columns.

An Object-Oriented Approach to Programming Logic and Design 30 Using Two-Dimensional and Multidimensional Arrays (continued) To declare a two-dimension array, use two sets of brackets for the number of rows and columns Example: numeric someNumbers[3][4] Two dimensional array can be initialized in the declaration. Example: numeric someNumbers[3][4] = {8, 9, 10, 11}, {1, 3, 12, 15}, {5, 9, 44, 99}

An Object-Oriented Approach to Programming Logic and Design 31 Using Two-Dimensional and Multidimensional Arrays (continued) numeric rents[4][3] = (400, 450, 510}, {500, 560, 630}, {625, 676, 740}, {1000, 1250, 1600}

An Object-Oriented Approach to Programming Logic and Design 32 Using a Built-In Arrays Class Many OOP languages provide an Arrays class containing useful methods for manipulating arrays Most methods are overloaded to handle different types of data If the language you are using does not have a built-in Arrays class, consider building one yourself

An Object-Oriented Approach to Programming Logic and Design 33 Using a Built-In Arrays Class (continued)

An Object-Oriented Approach to Programming Logic and Design 34 Using a Built-In Arrays Class (continued)

An Object-Oriented Approach to Programming Logic and Design 35 Using a Built-In Arrays Class (continued)

An Object-Oriented Approach to Programming Logic and Design 36 Summary Subscript values must stay in bounds for loop is useful for processing every element in an array Array can hold elements of any type When passed as parameters, array elements are passed by value When an entire array is passed as a parameter, it is passed by reference

An Object-Oriented Approach to Programming Logic and Design 37 Summary (continued) Arrays are useful for sorting items into an ascending or descending order To sort an array of objects, compare using a field in the object Single dimension array is a column of values Two-dimension array is a table of values A built-in Arrays class usually provides common methods for manipulating arrays