Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.

Slides:



Advertisements
Similar presentations
Programming with Microsoft Visual Basic 2008 Fourth Edition
Advertisements

Programming with Microsoft Visual Basic th Edition
Programming Logic and Design, Third Edition Comprehensive
Programming Logic and Design Eighth Edition
Chapter 9: Advanced Array Manipulation
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
1 Handling Exceptions COSC 1567 C++ Programming Lecture 11.
Programming Logic and Design Sixth Edition
Arrays.
Understanding Arrays and How They Occupy Computer Memory
Programming Logic and Design, Third Edition Comprehensive
Objectives In this chapter, you will learn about:
Chapter 2: Input, Processing, and Output
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
C++ for Engineers and Scientists Third Edition
An Object-Oriented Approach to Programming Logic and Design Chapter 7 Arrays.
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
Programming Logic and Design Sixth Edition
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
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.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
Programming Logic and Design, Second Edition, Comprehensive
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Programming Logic and Design Fifth Edition, Comprehensive
Array Processing.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
Chapter 8: Arrays.
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.
Programming Logic and Design Sixth Edition Chapter 5 Looping.
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition.
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
Chapter 5: Making Decisions
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Processing Arrays Lesson 9 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 4 Making Decisions.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Chapter 7: User-Defined Functions II
Chapter 2: Input, Processing, and Output
Programming Logic and Design Fourth Edition, Comprehensive
The Selection Structure
Chapter 7 Arrays.
User-Defined Functions
Iteration: Beyond the Basic PERFORM
Starting Out with Programming Logic & Design
Programming Logic and Design Fifth Edition, Comprehensive
Chapter 2: Input, Processing, and Output
Presentation transcript:

Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays

Programming Logic and Design, Fifth Edition, Comprehensive2 Objectives Understand arrays and how they occupy computer memory Manipulate an array to replace nested decisions Use a named constant to refer to an array’s size Declare and initialize an array Understand the difference between variable and constant arrays

Programming Logic and Design, Fifth Edition, Comprehensive3 Objectives (continued) Search an array for an exact match Use parallel arrays Search an array for a range match Learn about remaining within array bounds Use a for loop to process arrays

Programming Logic and Design, Fifth Edition, Comprehensive4 Understanding Arrays and How They Occupy Computer Memory Array: –Series or list of variables in computer memory –All variables share the same name –Each variable has a different subscript Subscript (or index): –Position number of an item in an array –Subscripts are always a sequence of integers

Programming Logic and Design, Fifth Edition, Comprehensive5 How Arrays Occupy Computer Memory Each item has same name and same data type Element: an item in the array Array elements are contiguous in memory Size of the array: number of elements it will hold

Programming Logic and Design, Fifth Edition, Comprehensive6 How Arrays Occupy Computer Memory (continued) Figure 6-1 Appearance of a three-element array and a single variable in computer memory

Programming Logic and Design, Fifth Edition, Comprehensive7 How Arrays Occupy Computer Memory (continued) All elements have same group name –Individual elements have unique subscript –Subscript indicates distance from first element –Subscripts are a sequence of integers Subscripts placed in parentheses or brackets following group name –Syntax depends on programming language

Programming Logic and Design, Fifth Edition, Comprehensive8 Manipulating an Array to Replace Nested Decisions Example: Human Resources Department Dependents report –List employees who have claimed 0 through 5 dependents Assume no employee has more than 5 dependents Application produces counts for dependent categories –Uses series of decisions Application does not scale up to more dependents

Programming Logic and Design, Fifth Edition, Comprehensive9 Figure 6-3 Flowchart of decision-making process using a series of decisions – the hard way

Programming Logic and Design, Fifth Edition, Comprehensive10 Figure 6-3 Pseudocode of decision-making process using a series of decisions – the hard way (continued)

Programming Logic and Design, Fifth Edition, Comprehensive11 Manipulating an Array to Replace Nested Decisions (continued) Array reduces number of statements needed Six dependent count accumulators redefined as single array Variable as a subscript to the array Array subscript variable must be: –Numeric with no decimal places –Initialized to 0 –Incremented by 1 each time the logic passes through the loop

Programming Logic and Design, Fifth Edition, Comprehensive12 Figure 6-4 Flowchart and pseudocode of decision-making process – but still a hard way

Programming Logic and Design, Fifth Edition, Comprehensive13 Figure 6-5 Flowchart and pseudocode of decision-making process using an array – but still a hard way

Programming Logic and Design, Fifth Edition, Comprehensive14 Manipulating an Array to Replace Nested Decisions (continued) Figure 6-5 Flowchart and pseudocode of decision-making process using an array – but still a hard way (continued)

Programming Logic and Design, Fifth Edition, Comprehensive15 Manipulating an Array to Replace Nested Decisions (continued) Figure 6-6 Flowchart and pseudocode of efficient decision-making process using an array

Programming Logic and Design, Fifth Edition, Comprehensive16 Figure 6-7 Flowchart and pseudocode for Dependents Report program

Programming Logic and Design, Fifth Edition, Comprehensive17 Using a Named Constant to Refer to an Array’s Size Avoid “magic numbers” (unnamed constants) Declare a named numeric constant to be used every time array is accessed Make sure any subscript remains less than the constant value Constant created automatically in many languages

Programming Logic and Design, Fifth Edition, Comprehensive18 Array Declaration and Initialization Declarations in different languages have two things in common: –Name the count array –Indicate there will be 20 separate numeric elements Table 6-1 Declaring a 20-element array named count in several common languages

Programming Logic and Design, Fifth Edition, Comprehensive19 Array Declaration and Initialization (continued) Initialize array elements num count[20] = 0 Make individual assignments count[0] = 5 No language allows assignment of more values than elements declared Initialization loop: loop structure that provides initial values to an array Use the loop control variable as the array subscript

Programming Logic and Design, Fifth Edition, Comprehensive20 Array Declaration and Initialization (continued) Figure 6-8 A loop that sets values for every element in an array

Programming Logic and Design, Fifth Edition, Comprehensive21 Variable and Constant Arrays Variable array: values may change during program execution –Values created during execution of application Constant array: assigned permanent and final values when program code written Hard-coded values are explicitly assigned to array elements Figure 6-9 Rents by floor

Programming Logic and Design, Fifth Edition, Comprehensive22 Figure 6-11 Program that produces tenant letters

Programming Logic and Design, Fifth Edition, Comprehensive23 Variable and Constant Arrays (continued) Figure 6-11 Program that produces tenant letters (continued)

Programming Logic and Design, Fifth Edition, Comprehensive24 Searching an Array for an Exact Match Sometimes must search through an array to find a value Example: mail-order business –Item numbers are three-digit, non-consecutive numbers –Customer orders an item, check if item number is valid –Create an array that holds valid item numbers –Search array for exact match

Programming Logic and Design, Fifth Edition, Comprehensive25 Searching an Array for an Exact Match (continued) Figure 6-12 Available items in mail-order company

Programming Logic and Design, Fifth Edition, Comprehensive26 Figure 6-13 Flowchart and pseudocode for program that verifies item availability

Programming Logic and Design, Fifth Edition, Comprehensive27 Figure 6-13 Flowchart and pseudocode for program that verifies item availability (continued)

Programming Logic and Design, Fifth Edition, Comprehensive28 Flag: variable that indicates whether an event occurred Technique for searching an array: –Set a subscript variable to 0 to start at the first element –Initialize a flag variable to false to indicate the desired value has not been found –Examine each element in the array –If the value matches, set the flag to True –If the value does not match, increment the subscript and examine the next array element Searching an Array for an Exact Match (continued)

Programming Logic and Design, Fifth Edition, Comprehensive29 Using Parallel Arrays Example: mail-order business –Two arrays, each with six elements Valid item numbers Valid item prices –Each price in valid item price array in same position as corresponding item in valid item number array Parallel arrays –Each element in one array associated with element in same relative position in other array Look through valid item array for customer item –When match found, get price from item price array

Programming Logic and Design, Fifth Edition, Comprehensive30 Figure 6-14 Flowchart and pseudocode of program that finds an item’s price

Programming Logic and Design, Fifth Edition, Comprehensive31 Figure 6-14 Flowchart and pseudocode of program that finds an item’s price (continued)

Programming Logic and Design, Fifth Edition, Comprehensive32 Using Parallel Arrays (continued) Figure 6-15 Typical execution of program that finds item’s price

Programming Logic and Design, Fifth Edition, Comprehensive33 Improving Search Efficiency Using an Early Exit Program should stop searching the array when a match is found Setting a variable to a specific value instead of letting normal processing set it Early exit: leaving a loop as soon as a match is found –Improves efficiency The larger the array, the better the improvement by doing an early exit

Programming Logic and Design, Fifth Edition, Comprehensive34 Figure 6-16 Flowchart and pseudocode of the loop that finds item’s price, exiting the loop as soon as it is found

Programming Logic and Design, Fifth Edition, Comprehensive35 Searching an Array for a Range Match Sometimes programmers want to work with ranges of values in arrays Example: mail-order business –Read customer order data, determine discount based on quantity ordered First approach: –Array with as many elements as each possible order quantity –Store appropriate discount for each possible order quantity

Programming Logic and Design, Fifth Edition, Comprehensive36 Searching an Array for a Range Match (continued) Figure 6-18 Usable – but inefficient – discount array

Programming Logic and Design, Fifth Edition, Comprehensive37 Searching an Array for a Range Match (continued) Drawbacks of first approach: –Requires very large array, uses a lot of memory –Stores same value repeatedly –How do you know you have enough elements? Customer can always order more Better approach: –Create four discount array elements for each discount rate –Parallel array with discount range Use loop to make comparisons

Programming Logic and Design, Fifth Edition, Comprehensive38 Searching an Array for a Range Match (continued) Figure 6-19 Superior discount array Figure 6-20 The DISCOUNT_RANGE array using the low end of each range

Programming Logic and Design, Fifth Edition, Comprehensive39 Figure 6-21 Program that determines discount rate

Programming Logic and Design, Fifth Edition, Comprehensive40 Remaining within Array Bounds Every array has finite size –Number of elements in the array –Number of bytes in the array Arrays composed of elements of same data type Elements of same data type occupy same number of bytes in memory Number of bytes in an array always a multiple of number of array elements Access data using subscript containing a value that accesses memory occupied by the array

Programming Logic and Design, Fifth Edition, Comprehensive41 Figure 6-22 Determining the month string from user’s numeric entry

Programming Logic and Design, Fifth Edition, Comprehensive42 Remaining within Array Bounds (continued) Program logic assumes every number entered by the user is valid When invalid subscript is used: –Some languages stop execution and issue an error –Other languages access a memory location outside of the array Invalid array subscript is a logical error Out of bounds: using a subscript that is not within the acceptable range for the array Program should prevent bounds errors

Programming Logic and Design, Fifth Edition, Comprehensive43 Figure 6-23 Program that uses a selection to ensure a valid subscript

Programming Logic and Design, Fifth Edition, Comprehensive44 Figure 6-24 Program that uses a loop to ensure a valid subscript

Programming Logic and Design, Fifth Edition, Comprehensive45 Using a FOR Loop to Process Arrays for loop: single statement –Initializes loop control variable –Compares it to a limit –Alters it for loop especially convenient when working with arrays –To process every element Must stay within array bounds Highest usable subscript is one less than array size

Programming Logic and Design, Fifth Edition, Comprehensive46 Using a FOR Loop to Process Arrays (continued) Figure 6-25 Pseudocode that uses a for loop to print month names

Programming Logic and Design, Fifth Edition, Comprehensive47 Figure 6-26 Pseudocode that uses a more efficient for loop to print month names

Programming Logic and Design, Fifth Edition, Comprehensive48 Summary Array: series or list of variables in memory –Same name and type –Different subscript Use a variable as a subscript to the array to replace multiple nested decisions Declare and initialize all elements in an array with a single statement Initialize array values within an initialization loop Some array values determined during program execution –Other arrays have hard-coded values

Programming Logic and Design, Fifth Edition, Comprehensive49 Summary (continued) Search an array: –Initialize the subscript –Test each array element value in a loop –Set a flag when a match is found Parallel arrays: each element in one array is associated with the element in second array –Elements have same relative position For range comparisons, store either the low- or high-end value of each range

Programming Logic and Design, Fifth Edition, Comprehensive50 Summary (continued) Access data in an array –Use subscript containing a value that accesses memory occupied by the array Subscript is out of bounds if not within defined range of acceptable subscripts for loop convenient tool for working with arrays –Process each element of an array from beginning to end