Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.

Slides:



Advertisements
Similar presentations
CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Data Types & Arrays.
Advertisements

Understanding the Need for Sorting Records
Chapter 9: Advanced Array Manipulation
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.
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
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
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.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
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.
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
Programming Logic and Design Fourth Edition, Comprehensive
Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
11 Chapter 8 ARRAYS Continued. 22 MULTI-DIMENSIONAL ARRAYS A one-dimensional array is useful for storing/processing a list of values. For example: –The.
© 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.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Chapter 8 Arrays and Strings
Searching and Sorting Chapter Sorting Arrays.
Array Processing.
19/10/20151 Data Structures Arrays. 219/10/2015 Learning Objectives Explain initialising arrays and reading data into arrays. Design and write routine/s.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
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.
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.
Chapter 6: Arrays: Lists and Tables
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
C++ for Engineers and Scientists Second Edition Chapter 11 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.
Introduction to C++ Programming Language Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Working with Arrays in MATLAB
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
IN THE NAME OF ALLAH WHO IS THE MOST BENEFICENT AND MOST MERCIFUL.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter Arrays, Timers, and More 8.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
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.
Array Applications. Objectives Design an algorithm to load values into a table. Design an algorithm that searches a table using a sequential search. Design.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
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.
Arrays 2.
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 7 Arrays.
Chapter 5: Arrays: Lists and Tables
Chapter 9: More About Data, Arrays, and Files
Presentation transcript:

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 5: Arrays: Lists and Tables

One-Dimensional Arrays A one-dimensional array is a list of related data of the same data type, referenced by the same name. An array is composed of elements, each of which is referenced by the array name and the element’s position in the array. Arrays are stored in consecutive memory locations. The position of an element in an array is called a subscript or an index. –In the text example, Scores[3], Scores is the array name and 3 is the subscript.

5-3 Declaring Arrays When an array is declared, the computer sets aside memory to store the values of the elements of the array. Declaring an array means telling the computer what is the data type of the array and how many elements will be stored in it. Pseudocode to declare arrays: Declare Height[5] Of Integers declares an array with 5 elements, all of which are integers Note: the first element of an array has subscipt 0 Address: Height[0]Height[1]Height[2]Height[3]Height[4] Content:

5-4 Filling (loading) an Array Here is how to load an array with 52 values. The values represent a salesperson’s sales for each week of the year. Declare WeeklySales[52] Of Reals Declare K As Integer For K = 0 Step 1 to 51 Write “Enter sales for week #”, K + 1 Input WeeklySales[K] End For Note that the sales for Week 1 correspond to the element WeeklySales[0] and the sales for Week 52 correspond to the element WeeklySales[51 ].

5-5 Parallel Arrays Parallel arrays can be used when array variables are related to each other by position, or subscript. Tip: It is necessary that the data in each array be loaded in the correct order. If we have arrays NumberGrade and LetterGrade (as below), each element of NumberGrade is related to an element in LetterGrade by the position in the array. NumberGrade: LetterGrade: “A” ”B” ”C” ”D” ”F”

5-6 Why Use Arrays? Arrays reduce the number of variable names in the program. Sales[K] versus Sales1, Sales2, SalesN, …. Arrays improve efficiency by allowing data to be read into the program once but processed as many times as necessary. Arrays increase the flexibility of the program. Arrays reduce the number of If statements needed in selection processing. If Sales[K] Then rather than If Sales1 Then … If Sales2 Then … If Sales3 Then …

Searching and Sorting Arrays If we have multiple, related arrays, with the elements ordered correctly, we can find values in one array based on a search key value in another array. A search key is a known value for one of the arrays. In the text example, a specific value of ‘Flight’ can be used to find the Origin, Time, and Gate of that flight. It can be used as a search key. This is called a table lookup or serial search.

5-8 The Serial Search 3 steps: –Load, or populate, the arrays. –Search the array that contains the key to find a match. –Display the key and corresponding values in the non-key arrays with the same subscript as the one with the key value, if a match is found. –Display an ‘unsuccessful search’ message if no matches are found.

5-9 The Serial Search (continued) Use a flag, or switch, to decide which message to print. A flag is a variable that is set to zero or one, depending on the results of an operation. The value of the flag can be checked later in the program with an If statement to determine which action to take.

5-10 The Serial Search (continued) Pseudocode for the serial search of an array named KeyData containing N elements: Set the subscript (here named Index) of the current table key equal to 0 Set a flag (here named Found) equal to 0 While (Found = 0) AND (Index < N) If KeyData[Index] = Key Then Set Found = 1 End If Set Index = Index + 1 End While If Found = 1 Then Display array element with subscript Index – 1 Else Display an “unsuccessful search” message End If

5-11 The Bubble Sort Technique To sort data means to arrange it in a particular numerical or alphabetical order, ascending or descending. To sort small amounts of data, use the bubble sort technique. –Make several passes through the data. –Compare adjacent pairs of data. –If the pairs are not in order, swap them. –Continue until all data is processed.

5-12 Swapping Values To swap two values, you must store one of them in a temporary storage location while you make the swap. To interchange array elements Array[K] and Array[K+1], the pseudocode for the swap looks like this: Set Temp = Array[K] Set Array[K] = Array[K+1] Set Array[K+1] = Temp Walk through the swap routine with a few values to see how it works. For example, if Array[K] = 6 and Array[K +1] = 18 at first, after the swap, Array[K] will contain the value 18 and Array[K+1] will contain the value 6. What will be the value in Temp ?

Other Uses of Arrays String Processing: –Some languages do not contain string data types, but implement strings as an array of character data type. –Declare the data type of an array in your program: Declare Profit[50] Of Integers Declare Month[10] Of Characters –Array size is the number of elements in an array. –String length is the number of characters of a particular element of a character array.

5-14 String Length and the Length Function The length of a string is the number of characters it contains. The Length function returns the number of characters in a given string. For example: Declare Str[10] Of Characters Set Str = “Hello!” Write Length(Str) The output will be 6 because the string “Hello!” is made up of 6 characters.

Two-Dimensional Arrays In a two-dimensional array, the value of an element depends on two factors instead of just one. Here is how to declare a two-dimensional array: Declare Profit[12, 24] Of Integers A two-dimensional array is like a matrix or an electronic spreadsheet. –Two factors can be, for example, Row and Column or Test Number and Student. –In the example in the text, Boynton’s (Student #2) scores can be referenced as: Score[1,0],Score[1,1], Score[1,2], Score[1,3], Score[1,4]. –Notice the ‘ 1 ’ subscript for Boynton remains constant, while the subscript for Test changes.

5-16 Using Two-Dimensional Arrays Use with nested loops to read in multiple values, multiple times. In the text example, for each student input, we load 5 tests scores. This allows us to process multiple tests for multiple students. Can use in parallel array processing Some languages allow for processing arrays of more than two dimensions. These are called higher-dimensional arrays.

5-17 Nested Loops Help Load Arrays To input data into a two-dimensional array, use a nested loop. A two-dimensional array named Working contains the names of 20 work-study students in one department and the hours worked in each of the 15 weeks of the semester. The array is declared as follows (below). It is assumed that the students are identified by number (1 through 20). The pseudocode to load this array is shown on the next slide. Declare Working[20, 15]

5-18 Nested Loops Help Load Arrays (continued) Declare Working[20, 15] For Student = 0 Step 1 to 19 Write “Enter hours worked each week” Write “for student # “, Student + 1 For Week = 0 Step 1 to 14 Input Working[Student, Week] End For(Week) End For(Student)

5-19 Pseudocode Language (Ch 5) Declare a one-dimensional array: Declare ThisArray[N] Of Integers Declare ThatArray[N] Of Reals Declare StringArray[N] Of Characters Declare a two-dimensional array: Declare ThisArray[X, Y] declares an array with X rows and Y columns