Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Arrays Starting Out with C++ Early Objects Seventh Edition by.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 9: Advanced Array Manipulation
Programming Logic and Design Sixth Edition
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
An Introduction to Programming with C++ Fifth Edition
Chapter 2: Input, Processing, and Output
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
Introduction to Programming with C++ Fourth Edition
C++ for Engineers and Scientists Third Edition
Chapter 9 Introduction to Arrays
Programming Logic and Design Fourth Edition, Comprehensive
Lesson 7 Arrays CS 1 Lesson 7 -- John Cole1. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Arrays Starting Out with C++ Early Objects Seventh Edition by.
ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
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.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter Arrays, Timers, and More 8.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More About Array Processing 8.2 There Are Many Uses of Arrays and Many Programming.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 8 Arrays.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a.
11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 7: Arrays.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 7: Arrays. 7.1 Arrays Hold Multiple Values.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Arrays Hold Multiple Values 7.1.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Chapter 9: Sorting and Searching Arrays
New Structure Recall “average.cpp” program
Chapter 7 Arrays.
Starting Out with Programming Logic & Design
Programming Logic and Design Fifth Edition, Comprehensive
Presentation transcript:

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis Chapter 8: Arrays

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-2 Chapter Topics 8.1 Array Basics 8.2 Sequentially Searching an Array 8.3 Processing the Contents of an Array 8.4 Parallel Arrays 8.5 Two-Dimensional Arrays 8.6 Arrays of Three or More Dimension

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Array Basics An array allows you to store a group of items of the same data type together in memory –Why? Instead of creating multiple similar variables such as employee1, employee2, employee3 and so on… –It’s more efficient to create just one variable Declare String employees[50] Declare Real salesAmounts[7] –The number in the [ ] is the size of the array

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Array Basics The storage locations in an array are elements Each element of the array has a unique number called a subscript that identifies it – the subscript always starts at 0 Figure 8-1 Array subscripts

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Array Basics Assigning values can be done individually using a subscript… Set numbers[0] = 20 Set numbers[1] = 30 Set numbers[2] = 40 Set numbers[3] = 50 Set numbers[4[ = 60 But, it is much more efficient to use a Loop to step through the array

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Array Basics Figure 8-3 Contents of the hours array

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Array Basics Arrays can be initialized to 0 or specific values Declare String days[7] = “Sunday”, “Monday”, “Tuesday”, Wednesday”, “Thursday”, “Friday”, “Saturday” Array bounds checking should be performed to avoid use of an invalid subscript Days[7] = “Saturday” is invalid because there is no 7 index –A common error is running a loop one time more than is necessary, exceeding the bound of the array –Off-by-one Error

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8.1 Array Basics Partially Filled Array –Sometimes an array is only partially filled –To avoid processing the unfilled elements, you must have an accompanying integer variable that holds the number of items stored in the array. When the array is empty, 0 is stored in this variable The variable is incremented each time an item is added to the array The variable's value is used as the array's size when stepping through the array. 8-8

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Constant Integer SIZE = 100 Declare Integer values[SIZE] Declare Integer count = 0 Declare Integer number Declare Integer Index Display "Enter a number, or -1 to quit." Input number While (number != -1 AND count < SIZE) Set values[count] = number Set count = count + 1 Display "Enter a number, or -1 to quit." Input number End While Display "Here are the values you entered:" For index = 0 To count - 1 Display values[index] End For 8.1 Array Basics 8-9 Partially Filled Array Example The count variable holds the number of items stored in the array.

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8.1 Array Basics Optional Topic: The For Each Loop –Some languages provide a For Each loop –It works with an array, iterating once for each array element –During each iteration, the loop copies an element's value to a variable. 8-10

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8.1 Array Basics 8-11 Constant Integer SIZE = 5 Declare Integer numbers[SIZE] = 5, 10, 15, 20, 25 Declare Integer num For Each num In numbers Display num End For For Each Example

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Sequentially Searching an Array A sequential search algorithm is a simple technique for finding an item in a string or numeric array –Uses a loop to sequentially step through an array –Compares each element with the value being searched for –Stops when the value is found or the end of the array is hit Set found = False Set index = 0 While found == False AND index <= SIZE -1 If (array[index] == searchValue Then Set found = True Else Set index = index + 1 End If End While

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Processing the Contents of an Array Totaling the values in an array and calculating average –Loops are used to accumulate the values –Then, the total is simply divided by the size

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Example 8-14

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Processing the Contents of an Array Finding the highest & lowest values in an array The highest –Create a variable to hold the highest value –Assign the value at element 0 to the highest –Use a loop to step through the rest of the elements –Each iteration, a comparison is made to the highest variable –If the element is greater than the highest value, that value is then the assigned to the highest variable The lowest –Same process, but checks if the element is less than the lowest value

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Processing the Contents of an Array Copying an array can be done using loops For index = 0 to SIZE – 1 Set secondArray[index] = firstArray[index] End For Passing an Array as an Argument –Usually must pass the array and the size The module call getTotal(numbers, SIZE) The module header Function Integer getTotal (Integer array[], Integer arraySize)

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Parallel Arrays By using the same subscript, you can establish a relationship between data stored in two or more arrays Figure 8-14 The names and addresses arrays

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Two-Dimensional Arrays A two-dimensional array is like several identical arrays put together –Suppose a teacher has six students who take five tests Figure 8-17 Two- dimensional array with six rows and five columns

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Two-Dimensional Arrays Two size variables are required when declaring Constant Integer ROWS = 3 Constant Integer COLS = 4 Declare Integer values[ROWS][COLS] Accessing is done with two loops, and both subscripts For row = 0 To ROWS -1 For col = 0 To COLS – 1 Display “Enter a number.” Input values[row][col] End For

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Arrays of Three or More Dimensions Arrays can also be three or more dimensions Declare Real seats[3][5][8] Figure 8-22 A three- dimensional array