An Object-Oriented Approach to Programming Logic and Design Chapter 7 Arrays.

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

Introduction to C Programming
Programming Logic and Design, Third Edition Comprehensive
Programming Logic and Design Sixth Edition
Chapter 10 Introduction to Arrays
Arrays.
Understanding Arrays and How They Occupy Computer Memory
Programming Logic and Design, Third Edition Comprehensive
VBA Modules, Functions, Variables, and Constants
Case, Arrays, and Structures. Summary Slide  Case Structure –Select Case - Numeric Value Example 1 –Select Case - String Value Example  Arrays –Declaring.
Programming with Collections Collections in Java Using Arrays Week 9.
An Introduction to Programming with C++ Fifth Edition
Arrays-Part 1. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
Introduction to Programming with C++ Fourth Edition
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Programming Logic and Design Fourth Edition, Comprehensive
Programming Logic and Design, Third Edition Comprehensive
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Processing Arrays Lesson 8 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
Programming Logic and Design, Second Edition, Comprehensive
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 10 Scott Marino.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Cis303a_chapt03-2a.ppt Range Overflow Fixed length of bits to hold numeric data Can hold a maximum positive number (unsigned) X X X X X X X X X X X X X.
Chapter 8 Arrays and Strings
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
6.3 List Boxes and Loops Some Properties, Methods, and Events of List Boxes List Boxes Populated with Strings List Boxes Populated with Numbers Searching.
Array Processing.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Chapter 8: Arrays.
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.
Chapter 6: Arrays: Lists and Tables
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
Arrays Chapter 8. Chapter 8 - Part 12 Variable and Variable Array Variable Stores only one value Variable Array Variable that has 1 symbolic name but.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
I Power Higher Computing Software Development High Level Language Constructs.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
An Introduction to Programming with C++ Sixth Edition Chapter 12 Two-Dimensional Arrays.
IT259 Foundation of Programming Using Java Unit 9 Seminar : (Chapter 8 ) Instructor : Vladimir Gubanov, PhD
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
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.
Data Storage So far variables have been able to store only one value at a time. What do you do if you have many similar values that all need to be stored?
Chapter 6: Using Arrays.
EGR 2261 Unit 10 Two-dimensional Arrays
An Introduction to Programming with C++ Sixth Edition
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 7 Arrays.
Chapter 5: Arrays: Lists and Tables
Visual Basic .NET BASICS
Programming Logic and Design Fifth Edition, Comprehensive
Introduction to Computer Programming IT-104
Presentation transcript:

An Object-Oriented Approach to Programming Logic and Design Chapter 7 Arrays

An Object-Oriented Approach to Programming Logic and Design 2 Objectives Understand arrays and how they occupy computer memory Manipulate an array to replace nested decisions Declare and initialize an array Understand the difference between initialized and uninitialized arrays Load array values from a file

An Object-Oriented Approach to Programming Logic and Design 3 Objectives (continued) Search an array for an exact match Use a constant to store an array size Search an array for a range match

An Object-Oriented Approach to Programming Logic and Design 4 Understanding Arrays and How They Occupy Memory Array: a series or list of variables in memory that have the same name and type but different subscripts (or indexes) Subscript (or index): a number that indicates the position of an item within the array Example: mailboxes in an apartment building – all have the same street address (name), but each has a different apartment number (subscript)

An Object-Oriented Approach to Programming Logic and Design 5 How Arrays Occupy Computer Memory Element: one item in the array; has a common name and a unique subscript, and stores a value Size of the array: the number of elements in the array Subscripts start at 0 An array’s elements are stored contiguously in memory

An Object-Oriented Approach to Programming Logic and Design 6 How Arrays Occupy Computer Memory (continued)

An Object-Oriented Approach to Programming Logic and Design 7 Manipulating an Array to Replace Nested Decisions

An Object-Oriented Approach to Programming Logic and Design 8 Manipulating an Array to Replace Nested Decisions (continued)

An Object-Oriented Approach to Programming Logic and Design 9 Manipulating an Array to Replace Nested Decisions (continued) Using an array can greatly reduce the amount of code required Subscript can be represented by a constant or a variable Using a variable for the subscript allows the use of a loop to cycle through all elements in an array

An Object-Oriented Approach to Programming Logic and Design 10 Manipulating an Array to Replace Nested Decisions (continued)

An Object-Oriented Approach to Programming Logic and Design 11 Manipulating an Array to Replace Nested Decisions (continued)

An Object-Oriented Approach to Programming Logic and Design 12 Manipulating an Array to Replace Nested Decisions (continued) Because the action taken was the same for each element of the array, the code can be significantly shortened Using a variable for the subscript replaces the need for decision structures in this example

An Object-Oriented Approach to Programming Logic and Design 13 Manipulating an Array to Replace Nested Decisions (continued)

An Object-Oriented Approach to Programming Logic and Design 14 Manipulating an Array to Replace Nested Decisions (continued)

An Object-Oriented Approach to Programming Logic and Design 15 Manipulating an Array to Replace Nested Decisions (continued) Subscript –Can have any legal name –Must be numeric with no decimal places –Must be initialized to 0 –Must be incremented by 1 each time the logic passes through a loop Arrays make code easier to read

An Object-Oriented Approach to Programming Logic and Design 16 Array Declaration and Initialization Declaring an array requires the type, the name, and the number of elements Example: numeric count[20] Individual array elements must be initialized If elements will contain different values, initialize each element separately Some languages allow declaration and initialization in a single statement Example: numeric count[0] = 5

An Object-Oriented Approach to Programming Logic and Design 17 Array Declaration and Initialization (continued ) If all elements in an array will have the same initial value, use a loop Providing values for an array is called populating the array

An Object-Oriented Approach to Programming Logic and Design 18 Initialized and Uninitialized Arrays Uninitialized array: does not receive values for the elements until run time Initialized array: values for the elements are “hard-coded” by the programmer An array can be thought of as a table

An Object-Oriented Approach to Programming Logic and Design 19 Initialized and Uninitialized Arrays (continued)

An Object-Oriented Approach to Programming Logic and Design 20 Loading an Array from a File Hard-coded values are inconvenient – only the programmer can change them Initializing an array by reading values from a file makes the program more flexible

An Object-Oriented Approach to Programming Logic and Design 21 Loading an Array from a File (continued)

An Object-Oriented Approach to Programming Logic and Design 22 Searching an Array for an Exact Match Use a loop to search through an array, element by element The loop control variable can be used as the array subscript variable Flag: a variable used to indicate if an event has occurred Start with flag = “No”; set it to “Yes” if you find the desired value in an array element

An Object-Oriented Approach to Programming Logic and Design 23 Searching an Array for an Exact Match (continued)

An Object-Oriented Approach to Programming Logic and Design 24 Using Parallel Arrays Parallel arrays: two arrays in which each element in one array is associated with the element in the same relative position in the other array Example: one array holds product numbers, the other holds the prices for each product

An Object-Oriented Approach to Programming Logic and Design 25 Using Parallel Arrays (continued)

An Object-Oriented Approach to Programming Logic and Design 26 Improving Search Efficiency Using an Early Exit When using a loop to search an array, exit the loop if the item is found in an array element Early Exit: leaving a loop when a match is found

An Object-Oriented Approach to Programming Logic and Design 27 Improving Search Efficiency Using an Early Exit (continued)

An Object-Oriented Approach to Programming Logic and Design 28 Using a Constant to Store an Array Size Instead of hard-coding the array size, use a named constant or variable Advantage: only the variable or constant needs to be changed to change the array size Named constant identifiers should be written in all caps Example: NUM_PRODUCTS

An Object-Oriented Approach to Programming Logic and Design 29 Using a Constant to Store an Array Size (continued)

An Object-Oriented Approach to Programming Logic and Design 30 Searching an Array for a Range Match Range match: finding a value within a contiguous range of values Example: range is 0 through 8

An Object-Oriented Approach to Programming Logic and Design 31 Searching an Array for a Range Match (continued)

An Object-Oriented Approach to Programming Logic and Design 32 Searching an Array for a Range Match (continued)

An Object-Oriented Approach to Programming Logic and Design 33 Summary Array: a series of variables in memory which all have the same name but different subscripts Each array element has a unique subscript to identify it from other array elements Subscript (or index): a number indicating the position of the element within the array Use a variable for the subscript to allow more programmatic control of the array

An Object-Oriented Approach to Programming Logic and Design 34 Summary (continued) Array element values can be hard-coded, or assigned at run time Array element values can be loaded from a file Searching an array involves examining each element –Loop through all elements –Use a flag to indicate when the matching value is found Flag: a variable that indicates if an event has occurred

An Object-Oriented Approach to Programming Logic and Design 35 Summary (continued) Parallel arrays are two arrays in which each element of one array is related to the element in the same subscript position in the other array When comparing a range of values, use either the low-end or high-end values of each range