Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal.

Slides:



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

MATH 224 – Discrete Mathematics
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.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
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 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
1 CS 201 Passing Function as Parameter & Array Debzani Deb.
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
1 ICS103 Programming in C Lecture 12: Arrays I. 2 Outline Motivation for One-dimensional Arrays What is a One-dimensional Array? Declaring One-dimensional.
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.
Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.
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.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
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.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
Loops Brent M. Dingle Texas A&M University Chapter 7 – part D (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
CSC 107 – Programming For Science. Today’s Goal  Become familiar with simple arrays  Declaring an array variable  Assigning data to array entries 
1 Working with Data Structures Kashef Mughal. 2 Chapter 5  Please review on your own  A few terms .NET Framework - programming model  CLR (Common.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Static arrays.
Pascal Programming Arrays and String Type.
Top Down Design Brent M. Dingle Texas A&M University Chapter 4 – Section 1 (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
ICS103 Programming in C Lecture 11: Arrays I
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Fall 2001(c)opyright Brent M. Dingle 2001 Simple Sorting Brent M. Dingle Texas A&M University Chapter 10 – Section 1 (and some from Mastering Turbo Pascal.
Computer Programming for Engineers
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
int [] scores = new int [10];
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
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.
Introduction to Arrays. Learning Objectives By the end of this lecture, you should be able to: – Understand what an array is – Know how to create an array.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Loops Brent M. Dingle Texas A&M University Chapter 7 – part C (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
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.
Loops Brent M. Dingle Texas A&M University Chapter 6 – Section 6.3 Multiway Branches (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Fall 2001(c)opyright Brent M. Dingle 2001 Multidimensional Arrays Brent M. Dingle Texas A&M University Chapter 10 – Section 2, part B (and some from Mastering.
Fall 2001(c)opyright Brent M. Dingle 2001 Abstract Data Types (ADTs) Brent M. Dingle Texas A&M University Chapter 8 – Sections 2 and 3 (and some from Mastering.
(c)opyright Brent M. Dingle 2001
Chapter 8 Arrays Objectives
Starting Out with Programming Logic & Design
Turbo Pascal Units (TPU)
Procedures Brent M. Dingle Texas A&M University
Chapter 8 Arrays Objectives
(c)opyright Brent M. Dingle 2001
C++ Array 1.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Complex Array Structures
Brent M. Dingle Texas A&M University Chapter 5 – Section 1
Presentation transcript:

Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)

Fall 2001(c)opyright Brent M. Dingle 2001 Many things all the same So we can now create new data types. But what if we have a bunch of things that are all the same type and sort of ‘go together?’ e.g. 4 Test scores How/ can we store those things ‘together’ in some way?

Fall 2001(c)opyright Brent M. Dingle 2001 Grades Say we wanted to write a program that read in 4 test scores for a student and we were to calculate the average of them. Currently we would need 4 variables to hold the test scores – one for each test

Fall 2001(c)opyright Brent M. Dingle 2001 Grades Program 1 – No Arrays PROGRAM Grades1; VAR grade1, grade2 : real; grade3, grade4 : real; avg : real; PROCEDURE GetGrade(var grade : real; grade_num : integer); BEGIN Write(‘Enter grade ’, grade_num, ‘ - -> ‘); Readln(grade); END; BEGIN { main } GetGrade(grade1, 1); GetGrade(grade2, 2); GetGrade(grade3, 3); GetGrade(grade4, 4); avg := grade1+grade2+grade3+grade4; avg := avg / 4; Writeln(‘Average = ‘, avg); END.

Fall 2001(c)opyright Brent M. Dingle 2001 A better way It almost seems silly to have 4 different variables that are so similar. And what if instead of 4 grades, there were 20? or 40? or 100? Would we really want to have 20, 40 or 100 variables? Think of all the typing… =) Fortunately there is a better way.

Fall 2001(c)opyright Brent M. Dingle 2001 What is an array? Arrays are collections of values all of the same type. Remember how a variable was like a box?

Fall 2001(c)opyright Brent M. Dingle 2001 Variables as Boxes

Fall 2001(c)opyright Brent M. Dingle 2001 Boxes are like drawers or shelves

Fall 2001(c)opyright Brent M. Dingle 2001 Arrays are like a stack of boxes – or shelves that are numbered

Fall 2001(c)opyright Brent M. Dingle 2001 Properties Every shelf is the same type. Every shelf has the same name BUT a unique number in square brackets. Each shelf has its own value.

Fall 2001(c)opyright Brent M. Dingle 2001 Indexing In the picture our indexing starts at 0. Indexing of arrays usually starts at 0 or 1. But it can start and end at any integer value (enumerated types can also be used as indices). Notice where the indexing starts is extremely important when reading and writing code.

Fall 2001(c)opyright Brent M. Dingle 2001 Indexing and math Indexing is very similar to subscripting in math. Notice the similarities of FOR loops of computer science and Summations of Math and how they both use indexing to simplify/compress the notation. Though computer science allows for more ‘options.’

Fall 2001(c)opyright Brent M. Dingle 2001 Declaring Arrays The best way to declare an array in Pascal is to create a Type for the desired array: TYPE ARY_0_TO_4_REAL = array [0..4] of real; VAR x : ARY_0_TO_4_REAL; So in the above we would have an array named x of type real containing 5 elements where the first element is at index = 0. Notice usually you will pick a better TYPE name, but for illustrative purposes ARY_0_TO_4_REAL was used.

Fall 2001(c)opyright Brent M. Dingle 2001 Accessing the values in arrays To access the values stored in an array you use the array name and the index of the desired element, where the index is placed in square brackets. So if we wanted to display the element at index 3 we would say: Writeln(x[3]); Let’s now go back to our Grade program, but this time we will use an array named grade of type real containing 4 elements where the first element is at index = 1.

Fall 2001(c)opyright Brent M. Dingle 2001 Grades Program 2 – Arrays PROGRAM Grades2; TYPE GRADE_ARRAY = array[1..4] of real; VAR grade : GRADE_ARRAY; avg : real; index : integer; PROCEDURE GetGrade(var grade : real; grade_num : integer); BEGIN Write(‘Enter grade ’, grade_num, ‘ --> ‘); Readln(grade); END;

Fall 2001(c)opyright Brent M. Dingle 2001 Grades Program 2 – Arrays (continued) BEGIN { main } avg := 0; FOR index := 1 to 4 DO BEGIN GetGrade(grade[index], index); avg := avg + grade[index]; END; avg := avg / 4; Writeln(‘Average = ‘, avg); END.

Fall 2001(c)opyright Brent M. Dingle 2001 Downsides to Arrays Functions cannot return arrays. You may go out of bounds of the array. i.e. You use an index that is invalid – out of range. For example if x was an array of 5 elements of type real with the first index starting at 1: x[0] would be out of bounds x[34] would be out of bounds If index = 5 then  x[index] would be valid but  x[index + 1] would NOT be valid Sometimes the compiler catches these errors (as in the first 2 cases above), sometimes it does not (as in the x[index + 1] case. Bad things may result.

Fall 2001(c)opyright Brent M. Dingle 2001 Problems (not graded) pages 333, 334 1, 2, 3, 4, 5, 6, 7, 8 Notice that in 7 and 8 you see arrays may be declared without using TYPE, however they also illustrate some of the problems that result (6 also hints at another problem). And in problem 8 don’t worry so much about the compiler directive.

Fall 2001(c)opyright Brent M. Dingle 2001 Array size Notice the size of every array is declared – or rather we cannot change the size of an array while the program is running. Sometimes it is said that array size is static (or non-dynamic).

Fall 2001(c)opyright Brent M. Dingle 2001 Array Initialization Notice that the value of each array element is NOT automatically initialized, so it is a good idea to run through a for loop to initialize every array. EX: FOR i := 1 to array_size DO BEGIN array_name[ i ] := initial_value; END;

Fall 2001(c)opyright Brent M. Dingle 2001 Using Arrays Often we may not know when writing the program how many items will need to be stored in an array, But we may know the maximum number of items that will ever be stored (or we chose a maximum). So sometimes we create an array from 1 to max_num items, initialize it to ‘invalid’ values and then use it as we see fit. This can waste a great deal of memory, but it works. See the programming example on pages to see how this works. This should also illustrate how partially filled and empty arrays come to be.

Fall 2001(c)opyright Brent M. Dingle 2001 Memory efficiency Memory used to be difficult to come by, today that is not so much a problem. However needlessly wasting memory can slow down a program. Some ways to save on memory usage are to limit the size of arrays to as small as possible, but still functional use variable parameters over value parameters when it is SAFE to do so.

Fall 2001(c)opyright Brent M. Dingle 2001 Accessing Arrays – ways to Accessing an array in order, say from the first element to the last is called sequential accessing. This is usually done using loops  a for loop is most common. Accessing an array in a fashion say like middle element, 3 rd element, 2 nd element, 23 rd element, 1 st element is called random accessing since you do not know which element will be accessed next.

Fall 2001(c)opyright Brent M. Dingle 2001 More Problems (not graded) pages 351 – 353 9,10, 11, 12, 13 14, 17

Fall 2001(c)opyright Brent M. Dingle 2001 End Arrays Chapter 9.1, 9.2 now complete.