Processing Arrays Lesson 8 McManusCOP10001. Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Programming Logic and Design Sixth Edition
Arrays Chapter 6.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
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.
C++ for Engineers and Scientists Third Edition
An Object-Oriented Approach to Programming Logic and Design Chapter 7 Arrays.
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.
Chapter 9 Introduction to Arrays
 Pearson Education, Inc. All rights reserved Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
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
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 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.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
 2006 Pearson Education, Inc. All rights reserved Arrays.
A First Book of ANSI C Fourth Edition
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.
Chapter 8 Arrays Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Chapter 8 Arrays and Strings
 Data is always stored in a logical way so that it can be accessed efficiently. Ex:A telephone directory  The way data is stored is called the structure.
Array Processing.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
 Pearson Education, Inc. All rights reserved Arrays.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
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.
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.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
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.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Processing Arrays Lesson 9 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
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.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
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.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Arrays – two dimensional Chapter 14 This chapter explains how to do the following with a two dimensional array: Declare, use indices, obtain size, pass.
 2008 Pearson Education, Inc. All rights reserved JavaScript: 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.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Objectives You should be able to describe: One-Dimensional Arrays
EGR 2261 Unit 10 Two-dimensional Arrays
Chapter 7 Arrays.
Introduction To Programming Information Technology , 1’st Semester
Programming Logic and Design Fifth Edition, Comprehensive
Introduction to Computer Programming IT-104
Presentation transcript:

Processing Arrays Lesson 8 McManusCOP10001

Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements of an Array Two-Dimensional Arrays –Loading a Two-Dimensional Array –Printing a Two-Dimensional Array –Accumulating the Rows and Columns of a Two- Dimensional Array McManusCOP10002

Overview cont. Multidimensional Arrays Table Look-Up Technique –Sequential Search –Binary Search The Pointer Technique –Frequency Distribution –Cross Tabulation McManusCOP10003

Arrays A consecutive group of memory locations that all have the same name and the same data type. Are the simplest structured type. –Component access is by a position number (an index) that indicates the component’s position within the collection. ExamplesMyArray(3) MyArray(n) McManusCOP10004

Simple Arrays Are declared at compile time. –Compiler reserves the appropriate amount of contiguous memory to hold the array. Occupy storage in memory –System must have enough memory –Memory may be reserved for more than one array in one statement, but not recommended. –Examples: Dim (99) As Integerin VB declares an array of 100 Integer elements Dim s(14) As Stringin VB declares an array of 15 String elements Int age[12];in C ++ declares an array of 12 Integer elements McManusCOP10005

Array Initialization McManusCOP10006 Private Sub cmdPrint_Click() Dim anarray(9) As Integer ‘10 Elements Dim x As Integer ‘Used as Loop Counter lblDisplay.Text = "Index" & Space(3) & _ "Value" ‘Heading For x = 0 To anarray.GetUpperbound(0) _ Step 1 anarray(x) = 5 lblDisplay.Text = x & Space(7)& anarray(x) Next x End Sub VB Code What will it display?

A Different Method of Initialization McManusCOP10007 Private Sub cmdPrint_Click() Dim anarray(9) As Integer ‘10 Elements Dim x As Integer ‘Used as Loop Counter lblDisplay.Text = "Index" & Space(3) & _ "Value" ‘Heading For x = 0 To anarray.GetUpperbound(0) _ Step 1 anarray(x) = x lblDisplay.Text = x & Space(7)& anarray(x) Next x End Sub VB Code What will it display?

Array Initialization –In VB, by default, the first array index is initialized to 0 and VB won’t allow you to change the default. –However, other languages, like Pascal, will allow the programmer to initialize the array to something other than zero, but you have to do it... Note…not all languages have this facility… In other languages, you have to initialize the array. McManusCOP10008

abccdef Array Type Declaration type CharArray = array [1..7] of Char; Allocate storage for the array var LetterArray : CharArray; LetterArray LetterArray[3] = ‘c’ the element in Position 3 is "c" McManusCOP10009 Pascal Code Index Element

Accessing Array Elements To access the array called numbers, call the array by name and include in () the index (or location) of the value to be accessed. numbers(2)  23; numbers(4)  Note: the second element in the array is not 23. Source of off-by-one errors Print numbers(2) + numbers(3) will print 51 McManusCOP An array called Numbers Index

Some Examples Numbers( 2 ) + Numbers( 3 ) = 12 Numbers( ) value stored in 5 is 6 Numbers( 2 ) + 3valued stored in 2 is = 8 Numbers( ) value stored in 4 is 3 Numbers( 2 * 3 )value stored in 6 is 4 Numbers( ) generates a syntax error McManusCOP100011

How Random Access Works The computer calculates the addresses of an array for you. McManusCOP Index_Location = Base + (Index * Element_Length) Index_Location = ( 3 * 2 ) Index_Location(3) = 1006 which is the Address for the element stored at Index 3. Index Elements 1000 Base 2 bytes 2 bytes 2 bytes 2 bytes 2 bytes 2 bytes 2 bytes

Storing Data in an Array Must be read into an array one element at a time. Must be displayed one element at a time. for Index := 1 to MaxItems do Read (DataArray[Index]); for Index := 1 to MaxItems do WriteLn (Index :4, DataArray[Index] :8:1); McManusCOP Pascal Code

One-Dimensional Arrays The simplest Array Structure Ex. Array - Age Variable Name AGE (1) = 32 AGE (2) = 54 AGE (3) = 25 AGE (4) = 36 AGE (5) = 45 AGE (6) = 20 AGE (7) = 28 AGE (8) = 50 McManusCOP The Number in the Parentheses refers to the Box number in the Array, the Element Number Index Elements

Accessing Arrays Individual array elements and entire arrays can be passed as parameters. Arrays can also be copied from one array to another. constMaxSize = 100; type IndexRange = 1..MaxSize; TestArray = array [IndexRange] of Real; var XArray, YArray : TestArray; {declares both as arrays} XArray := YArray {the code: copies YArray to XArray} McManusCOP Both arrays must be of the same data type. Pascal Code

Entering Data into an Array To Load an Array, use a Loop –If you know the number of elements, then use the automatic-counter loop (For loop) –If you don’t know the number of elements, then use an indicator code with the Repeat/Until or the While/While-end loop McManusCOP100016

McManusCOP Algorithm For Index = 1 To N Step 1 AnArray(Index) = X Next Index Index = Counter N = Number of elements in AnArray AnArray(Index) = Element Index in Array X = data entered into AnArray(Index) AnArray(Index) = X Index <= N Index = Index + 1 Index = 1 False True Flowchart Pseudocode

McManusCOP Algorithm Index = 0 Repeat Index = Index + 1 AnArray(Index) = X Until Index > 10 X = data entered into AnArray(Index) Index = 0 Index = Index + 1 AnArray(Index) = X Until Index > 10 True False Pseudocode Flowchart

McManusCOP Algorithm Index = 1 AnArray(Index) = Readvalue While AnArray(Index) <> Sentinel AnArray(Index) = Readvalue End While Sentinel = end test value Readvalue = value being read in from user Index= 1 AnArray (Index) = Readvalue True False AnArray (Index) = Readvalue AnArray (Index) <> Sentinel Flowchart Pseudocode

Printing an Array McManusCOP Algorithm For Index= 1 To N Step 1 Print AnArray(Index) Next Index N = Total number of elements AnArray(Index) = ith element of Array Print AnArray (Index) Index <= N Index = Index + 1 Index = 1 False True implicit Implicit Step Note: Not all languages begin with 1 as the default first element. Some, like VB, begin with 0 Flowchart Pseudocode

Two-Dimensional Arrays A two-dimensional array is a block of memory locations associated with a single memory variable name and designated by row and column numbers Each element is written as A (Row#,Column#) –The row number is always first, and the column number second –Note: This is different than in Excel McManusCOP100021

Two-Dimensional Arrays For a two-dimensional array, information is stored in rows and columns. –Requires two indexes: The first, by convention, represents the row The second represents the column. Cannot use the same index for both. –Ex. mArray (x, y) or mArray(row, col) In VB int TwoDimArray[ r ],[ c ] In C ++ McManusCOP100022

Two-Dimensional Array McManusCOP Rows Columns 1 st Element Subscripts (indexes) 0,3 1,1 1,3 0,1 0,2 1,2 1,0 0,

Loading a Two-Dimensional Array You load a Two-dimensional Array with nested loops—one loop for each dimension –When the data is loaded row by row, the outer loop represents the row, and the inner loop represents the column –This order of the loops allows the row number to stay constant while the column number varies McManusCOP100024

Loading a Two-Dimensional Array Algorithm For R = 0 To 4 Step 1 Sum = Sum + Sales(R,C) For C = 0 To 4 Step 1 Total = Total + Sales(R,C) Next C Next R McManusCOP Flowchart Enter Exit C = 0 to 4 Sum = Sum + Sales(R,C) Total = Total + Sales(R,C) C R = 0 to 4 R 0,3 1,1 1,3 0,1 0,2 1,2 1,0 0, ,3 3,1 3,3 2,1 2,2 3,2 3,0 2, ,1 4,3 4,2 4, ,4 1, ,4 3, ,4 25 rows columns An array called Sales Pseudocode

Multidimensional Arrays Arrays with 3 or more dimensions –can facilitate an understanding of the data, –improve the readability of algorithms, and –facilitate processing since the data can be processed through the use of three or more nested loops. McManusCOP100026

Multidimensional Arrays The recommended variable names for the indexes for a 3-dimensional array would be –R for Row –C for Column –D for Depth –V for Volume These arrays are processed much like 2- dimensional arrays McManusCOP For each additional dimension, an additional loop is used to manipulate the dimension

Arrays and Lists How do they differ? McManusCOP100028

Arrays vs. Lists Arrays Arrays use Random Access Arrays use Indices to access elements Arrays are declared at Compile Time Array elements are stored in contiguous memory Lists Lists use Sequential Access Lists use Pointers Lists are declared at Run Time List elements are stored in any available location McManusCOP100029

Attempt the end, and never stand to doubt; Nothing’s so hard, but search will find it out. Robert Herrick McManusCOP100030

Table Look-up Technique A common application for arrays is using a value to look up another value in a table Two (of many) methods for looking up values in an array or table –The Sequential search method –The Binary search method McManusCOP100031

Sequential Search This method is used when you don’t know the element number, but you do know the value of the element The methodology is simple. –Given the search value of the element you want to find, element one is tested to see if it matches the search variable. If it does, the flow of the program drops out of the loop. If it doesn’t, the element number is incremented and loops back to test again McManusCOP100032

Binary Search This is a High speed search This search technique involves comparing the mid-element of all or part of the array. –If it compares then it drops you out of the loop. –If it does not, the program checks to see if the value is lower or higher than the middle value. McManusCOP [First] [Middle] [Last] Item FM-1MM+1L

Binary Search cont. –The boundaries are then reset to select a new section of the array. –The program continues to divide the array in half until the desired element is found A 1,000 element array would take less than 10 comparisons McManusCOP100034

The Pointer Technique Method using arrays to specify the value of an element in one array as the element number in another array –The value of the element in the first array points to element in the second array Techniques –Frequency Distribution and –Cross-Tabulation McManusCOP100035

Frequency Distribution A tally of one type of value in an array –Example: how many students in a school are in each class or how many of a company’s customers live in each zip code area The result of a frequency distribution is a one dimensional array that contains the value for each element number McManusCOP100036

Frequency Distribution Example McManusCOP IndexValues (Age Groups) IndexFrequency (# in each Group)

Cross-Tabulation This uses the pointer technique for calculating statistics from a questionnaire –Example: How many students are in each major and each class The result of this cross-tabulation is a two- dimensional array containing the tally for the combination of the majors and classes –The majors would be the rows and the classes would be the columns McManusCOP100038

Cross Tabulation Example Yes (1) No (2) Females (1) 3225 Males (2) 5142 McManusCOP100039

Next? McManusCOP100040