Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a.

Slides:



Advertisements
Similar presentations
Understanding the Need for Sorting Records
Advertisements

Chapter 9: Advanced Array Manipulation
Concepts of Database Management Seventh Edition
Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction.
An Introduction to Programming with C++ Fifth Edition
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
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
Introduction to Programming with C++ Fourth Edition
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Chapter 9 Introduction to Arrays
Programming Logic and Design Fourth Edition, Comprehensive
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
Addressing Modes Chapter 11 S. Dandamudi To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer,  S.
Concepts of Database Management, Fifth Edition
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.
12-1 Structured COBOL Programming Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout John.
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
IE 212: Computational Methods for Industrial Engineering
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 8 Arrays and Strings
Chapter 7 Array processing. Objectives To introduce arrays and the uses of arrays To develop pseudocode algorithms for common operations on arrays To.
Array Processing.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 6: Arrays: Lists and Tables
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
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.
Addressing Modes Chapter 6 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
Master File Update Processing. Objectives On completing this section you should be able to: w Distinguish between online processing and batch processing.
 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.
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.
An Introduction to Programming with C++ Sixth Edition
CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.
An Introduction to Programming with C++ Sixth Edition Chapter 12 Two-Dimensional Arrays.
Arrays.
Engineering Computing I Chapter 5 Pointers and Arrays.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
Array Applications. Objectives Design an algorithm to load values into a table. Design an algorithm that searches a table using a sequential search. Design.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Chapter 9 Introduction to Arrays Fundamentals of Java.
 Problem Analysis  Coding  Debugging  Testing.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Trace Tables In today’s lesson we will look at:
1-1 Logic and Syntax A computer program is a solution to a problem.
Computer Programming BCT 1113
Microsoft Visual Basic 2005: Reloaded Second Edition
Lecture 2 Introduction to Programming
JavaScript: Functions.
Programming Right from the Start with Visual Basic .NET 1/e
Algorithm Discovery and Design
Starting Out with Programming Logic & Design
Problem Solving.
Programming Logic and Design Fifth Edition, Comprehensive
Presentation transcript:

Introduction to Arrays

Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a list, or one-dimensional array. Input, output, and manipulate variables in a table, or two-dimensional array.

Objectives (Continued) Distinguish between row-major ordering and column-major ordering. Explain how the ordering of variables in a table affects the efficiency of processing.

Introduction Up to this point we have focused on reading, processing, and writing of single values. Each value we have used has been stored in a single, unique location and referred to as a single (or simple) variable. Each variable had its own name, such as COUNT, N, INPUT, ACCUM, etc. We need not always do things this way...

What If We want to input a list of ten items to ten consecutive storage locations. We could name them ITEM1, ITEM2, and so on, and use these names throughout the program.

So Far, So Good But what if we want to use 100 values? 1000 values? We could use the same approach but we learned a long time ago that the resulting program would be extremely large and cumbersome!

List Structures Suppose that instead of treating our ten input items as ten similar but separate data items, we treat them as a group of data items. We will set aside a storage area large enough to hold all ten values, and assign a name to the storage area. Data items stored and defined in this way are called lists, vectors, or arrays - depending on the programming language you are using.

Arrays Only the group of items stored in an array (vector, list) is given a name. An individual item in the group is referred to by its relative position in the group (going left to right). This position is identified by a subscript in parentheses following the group name.

Arrays (Continued) For example, assume we have reserved a storage area called INAREA for the ten numbers we described earlier. When we use the unsubscripted name INAREA we are referring to the entire group. The individual members of the group are referred to by the subscripted name - for example, the first member of the group is INAREA(1).

List Examples INAREA(1)INAREA(2)INAREA(3)INAREA(5)INAREA(4) INAREA INAREA(10)

Initialize One-Dimensional Array Start SUB = 0 DOUNTIL SUB = 10 SUB = SUB + 1 LIST(SUB) = 0 ENDDO Stop

Input One-Dimensional Array Start SUB = 0 DOUNTIL SUB = 10 SUB = SUB + 1 READ LIST(SUB) ENDDO Stop

Output One-Dimensional Array Start SUB = 0 DOUNTIL SUB = 10 SUB = SUB + 1 WRITE LIST(SUB) ENDDO Stop

Sample Problem 9.1 Problem: Compute and output the smallest number in a ten-element array called LIST.

Sample Problem 9.1 (2) Structure Chart looks something like this: OVERALL CONTROL A000 COMPUTE AND OUTPUT SMALL B010 INPUT ARRAY B000

Sample Problem 9.1 (3) Overall Control module: A000 Start Input Array (B000) Compute and Output Small (B010) Stop

Sample Problem 9.1 (4) Input Array (we’ve seen this already): B000 Enter SUB = 0 DOUNTIL SUB = 10 SUB = SUB + 1 READ LIST(SUB) ENDDO Return

Sample Problem 9.1 (5) Compute and Output SMALL: B010 Enter SMALL = LIST(1) SUB = 1 DOUNTIL SUB = 10 SUB = SUB + 1 IF LIST(SUB) < SMALL THEN SMALL = LIST(SUB) (ELSE) ENDIF ENDDO WRITE SMALL Return

Sample Problem 9.2 This time we are to compute and print the average of the ten values in LIST, and print the entries in LIST as well.

Sample Problem 9.2 (2) About the only thing different here is the processing of the members of the array. We added a new module called Output Array (which we have already seen). Compute and Output Average is the name of the module which computes the average of the array elements.

Sample Problem 9.2 (3) Pseudocode for Compute and Output Average: B020 Enter ACCUM = 0 SUB = 0 DOUNTIL SUB = 10 SUB = SUB + 1 ACCUM = ACCUM + LIST(SUB) ENDDO AVG = ACCUM / SUB WRITE ‘Average is’,AVG Return

Sample Problems These examples are pretty straightforward - all we’re doing is other manipulations of the array data. In examples 9.4 and 9.5 we create new arrays to contain the output of manipulations on values in the original array. Let’s move on to tables...

Table Structures In lists we used only a single subscript to define the position of a value in the list. This single subscript is also sometimes called a DIMENSION. (Personal note: the first programming language I learned was FORTRAN - in FORTRAN, arrays are defined with the word DIMENSION.)

Table Structures Sometimes it is desirable to treat data as having more than one dimension: for example, the air fare between two cities depends on where your trip starts and ends - two things are needed to decide the value.

A Typical Table

Initialize Two-Dimensional Array Start ROW = 1 DOUNTIL ROW > 4 COL = 1 DOUNTIL COL > 5 GRID(ROW,COL) = 0 COL = COL + 1 ENDDO ROW = ROW + 1 ENDDO Stop

Sample Problem 9.6 (Seating Chart Problem) We use the familiar Input Array module to read in the names and fill the chart, then an Output Array module to print it. To find a student’s seat, the name is input and the array is searched for an entry with that name. (See page 209.)

Sample Problem 9.7 This problem uses a two-dimensional array: each row contains a student name and the student’s average. The program is to find the highest average in the class. The array has 25 rows.

Sample Problem 9.7 (2) Solution begins on page 210 (structure chart). Flowchart and pseudocode for the “Compute and Output Highest Average” module are on page 213.

Sample Problem 9.8 Create a two-dimensional array called A. (Dimensions of A come from a header record - M rows of N columns each.) We will also create a one- dimensional array V containing N values.

Problem 9.6 (Continued) Each value in each row of A is multiplied by the value in the corresponding position in V: for example, A(M,1) is multiplied by V(1); A(M,2) is multiplied by V(2), etc., up to A(M,N) and V(N). Output is a two-dimensional array called T.

Solution Here is the overall control module - not too difficult: A000 Start Input two-dimensional array (B000) Input one-dimensional array (B010) Compute and output array (B020) Stop

Compute and Output Array B020 Enter ROW = 1 DOUNTIL ROW > M COL = 1 DOUNTIL COL > N T(ROW,COL) = A(ROW,COL) * V(COL) WRITE T(ROW,COL) on current line COL = COL + 1 ENDDO

Compute and Output Array (Continued) Skip to new line ROW = ROW + 1 ENDDO Return

Row-Major and Column-Major Order Different programming languages store data in different ways. Our algorithm needs to be written to allow efficient processing of the array. Row-major order: first subscript varies least rapidly, last one most rapidly) - i.e., data is stored by rows. Column-major order: first subscript varies most rapidly, last one least rapidly). Supermarket example.

Ordering of Array Data Pascal, C, Basic and COBOL store and process data in row-major order. (Some implementations of Basic may vary.) FORTRAN uses column-major order.

Enrichment Basic and Visual Basic Examples

Assignment 4 Chapter 9, page 226, Exercise 11 Due Tuesday