Arrays In this section of notes you will be introduced to a composite type where all elements must be of the same type (homogeneous): arrays Homogeneous.

Slides:



Advertisements
Similar presentations
Program CheckPass; var TestScore, ExamScore : integer; FinalScore : real; Status : string; begin write(‘Enter the Test score:’); readln(Testscore); write(‘Enter.
Advertisements

James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
James Tam Records You will learn in this section of notes how to create a new, composite type, that can be composed of different types of elements.
James Tam Multi-Dimensional Arrays In Pascal In this section of notes you will learn about how and when to use multi- dimensional arrays.
James Tam Pointers In this section of notes you will learn about another type of variable that stores addresses rather than data.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type arrays.
James Tam Arrays In this section of notes you will be introduced to a composite type where all elements must be of the same type (homogeneous): arrays.
James Tam Records You will learn in this section of notes how to create a new, composite type, that can be composed of different types of elements.
James Tam Multi-Dimensional Arrays In Pascal In this section of notes you will learn about how and when to use multi- dimensional arrays.
Lecture 05 - Arrays. Introduction useful and powerful aggregate data structure Arrays allow us to store arbitrary sized sequences of primitive values.
J. Michael Moore From James Tam’s material Multi-Dimensional Arrays CSCE 110.
James Tam Records You will learn in this section of notes how to create a new, composite type, that can be composed of different types of elements.
J. Michael Moore Arrays CSCE 110 From James Tam’s material.
James Tam Records You will learn in this section of notes how to create a new, composite type, that can be composed of different types of elements.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 2, Lecture 2.
James Tam Composite Types You will learn in this section of notes how to create single and generic instances of non-homogeneous composite types that are.
J. Michael Moore From James Tam’s material Multi-Dimensional Arrays CSCE 110.
James Tam Arrays / Lists In this section of notes you will be introduced to new type of variable that consists of other types.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
J. Michael Moore From James Tam's material Records CSCE 110.
James Tam Multi-Dimensional Arrays In Pascal In this section of notes you will learn about how and when to use multi- dimensional arrays.
James Tam Records You will learn in this section of notes how to create a new, composite type, that can be composed of different types of elements.
J. Michael Moore Arrays CSCE 110. J. Michael Moore Typical (although simplified) Problem Write a program that will track student grades in a class. The.
James Tam Pointers In this section of notes you will learn about another type of variable that stores addresses rather than data.
Chapter 8 Arrays and Strings
James Tam Lists In this section of notes you will be introduced to new type of variable that consists of other types.
James Tam Pointers In this section of notes you will learn about another type of variable that stores addresses rather than data.
1 The CONST definition CONST Pi = , City = ‘New York’; Constant identifiers are used when you do not want the value of an identifier to change why.
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Introduction to Pascal The Basics of Program writing.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday)
Programming, an introduction to Pascal
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
James Tam Multi-Dimensional Arrays In Pascal In this section of notes you will learn about how and when to use multi- dimensional arrays.
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.
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Computer Programming BCT 1113
Array, Strings and Vectors
Chapter 6 Arrays Lecturer: Mrs Rohani Hassan
Programming For Nuclear Engineers Lecture 6 Arrays
Complex data types Complex data types: a data type made of a complex of smaller pieces. Pascal has four very commonly used complex data types: strings,
Objectives Identify the built-in data types in C++
JavaScript: Functions.
Arrays in C.
CSCE 210 Data Structures and Algorithms
Lecture 6 C++ Programming
Arrays … The Sequel Applications and Extensions
Arrays, Part 1 of 2 Topics Definition of a Data Structure
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
CNG 140 C Programming (Lecture set 8)
EKT150 : Computer Programming
Introduction To Programming Information Technology , 1’st Semester
CMSC202 Computer Science II for Majors Lecture 03 – Arrays and Functions Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Arrays In this section of notes you will be introduced to a homogeneous composite type, one-dimensional arrays One-dimensional arrays in Pascal.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Suggested self-checks: Section 7.11 #1-11
C++ winter2008(by:J.Razjouyan)
CS150 Introduction to Computer Science 1
C++ Array 1.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CS Problem Solving and Object Oriented Programming Spring 2019
Presentation transcript:

Arrays In this section of notes you will be introduced to a composite type where all elements must be of the same type (homogeneous): arrays Homogeneous composite types in Pascal

Types Of Variables Pascal Variables 2. Aggregate (composite) Simple (atomic) Homogenous(arrays) Heterogeneous(records) integer char boolean real Homogeneous composite types in Pascal

Types Of Variables Pascal Variables Simple (atomic) 2. Aggregate (composite) Homogenous (arrays) Heterogeneous(records) integer char boolean real Homogeneous composite types in Pascal

Example Problem Write a program that will track the percentage grades for a class of students. The program should allow the user to enter the grade for each student. Then it will display the grades for the whole class along with the average.

Why Bother With Composite Types? For a compilable example look in Unix under: /home/231/examples/arrays/classList1.p const CLASS_SIZE = 5; begin var stu1 : real; var stu2 : real; var stu3 : real; var stu4 : real; var stu5 : real; var total : real; var average : real; Homogeneous composite types in Pascal

Why Bother With Composite Types? (2) write('Enter grade for student number 1: '); readln(stu1); write('Enter grade for student number 2: '); readln(stu2); write('Enter grade for student number 3: '); readln(stu3); write('Enter grade for student number 4: '); readln(stu4); write('Enter grade for student number 5: '); readln(stu5); total := stu1 + stu2 + stu3 + stu4 + stu5; average := total / CLASS_SIZE; writeln('The average grade is ', average:6:2, '%'); Homogeneous composite types in Pascal

With Bother With Composite Types? (3) (* Printing the grades for the class. *) writeln('Student1: ', stu1:6:2); writeln('Student2: ', stu2:6:2); writeln('Student3: ', stu3:6:2); writeln('Student4: ', stu4:6:2); writeln('Student5: ', stu5:6:2); end. Homogeneous composite types in Pascal

With Bother With Composite Types? (3) (* Printing the grades for the class. *) writeln('Student1: ', stu1:6:2); writeln('Student2: ', stu2:6:2); writeln('Student3: ', stu3:6:2); writeln('Student4: ', stu4:6:2); writeln('Student5: ', stu5:6:2); end. NO! Homogeneous composite types in Pascal

What’s Needed A composite variable that is a collection of another type. The composite variable can be manipulated and passed throughout the program as a single entity. At the same time each element can be accessed individually. What’s needed…an array! Homogeneous composite types in Pascal

Declaring Arrays As with any other variable, you must first create an array in memory by declaring an instance. Format: name: array [low index..high index] of element type; Example: const CLASS_SIZE = 5; : : var classGrades : array [1..CLASS_SIZE] of real; classGrades [1] [2] [3] [4] [5] Homogeneous composite types in Pascal

Accessing Data In The Array To manipulate an array you need to first indicate which array is being accessed Done via the name of the array e.g., “classGrades” If you are accessing a single element, you need to indicate which element that you wish to access. Done via the array index e.g., “classGrades[2]” classGrades [1] [2] [3] [4] [5] Using only the name of the array refers to the whole array classGrades [1] [2] [3] [4] [5] Use the array name and a subscript (the ‘index’) refers to a single element Homogeneous composite types in Pascal

Assigning Data To The Array Format: (Whole array) (One element) name of array := value; name of array [index] := value; Examples (assignment via the assignment operator): firstArray := secondArray; classGrades [1] := 100; Homogeneous composite types in Pascal

Assigning Data To The Array (2) Examples (assigning values via read or readln): (Single element) readln(classGrades[1]); (Whole array – all elements) for i: = 1 to CLASS_SIZE do begin write('Input grade for student No. ', i, ': '); readln(classGrades[i]); end; Homogeneous composite types in Pascal

Assigning Data To The Array (3) Example: (Whole array – all elements: Character arrays only) var charArray : array [1..SIZE] of char; readln(charArray); Important note: arrays cannot be passed as a parameters to read or readln (except for one-dimensional character arrays) Homogeneous composite types in Pascal

Accessing The Data In The Array Examples (displaying information): (Single element) writeln(classGrades[1]); (Whole array – all elements) for i := 1 to CLASS_SIZE do writeln('Grade for student No. ', i:2, ' ', classGrades[i]:6:2); Homogeneous composite types in Pascal

Accessing The Data In The Array (2) Example: (Whole array – all elements: Character arrays only) var charArray : array [1..SIZE] of char; write(charArray); Important note: arrays cannot be passed as a parameters to write or writeln (except for one-dimensional character arrays) Homogeneous composite types in Pascal

Revised Version Using An Array For a compilable example look in Unix under: /home/231/examples/arrays/classList2.p const CLASS_SIZE = 5; begin var classGrades : array [1..CLASS_SIZE] of real; var i : integer; var total : real; var average : real; total := 0; Homogeneous composite types in Pascal

Class Example Using An Array (2) for i := 1 to CLASS_SIZE do begin write('Enter grade for student no. ', i, ': '); readln (classGrades[i]); total := total + classGrades[i]; end; average := total / CLASS_SIZE; writeln; writeln('The average grade is ', average:6:2, '%'); writeln('Grade for student no. ', i, ' is ', classGrades[i]:6:2, '%'); Homogeneous composite types in Pascal

Passing Arrays As Parameters Declare a type for the array. e.g. const CLASS_SIZE = 5; type Grades = array [1..CLASS_SIZE] of real; Declaring a type does not create an instance A type only describes the attributes of a new kind of variable that can be created and used. No memory is allocated. Homogeneous composite types in Pascal

Passing Arrays As Parameters (2) Declare an instance of this type. e.g., var lecture01 : Grades; Memory is allocated! Pass the instance to functions/procedures as you would any other parameter. (Function/procedure call) displayGrades (lecture01, average); (Function/procedure definition) procedure displayGrades (lecture01 : Grades; average : real); Homogeneous composite types in Pascal

Passing Arrays As Parameters: An Example The full example can be found in Unix under /home/231/examples/classList3.p): program classList (input, output); const CLASS_SIZE = 5; type Grades = array [1..CLASS_SIZE] of real; procedure tabulateGrades (var lecture01 : Grades; var average : real); var i : integer; total : real; Homogeneous composite types in Pascal

Passing Arrays As Parameters: An Example (2) begin (* tabulateGrades *) total := 0; for i := 1 to CLASS_SIZE do begin write('Enter grade for student no. ', i, ': '); readln(lecture01[i]); total := total + lecture01[i]; end; average := total / CLASS_SIZE; writeln; end; (* tabulateGrades *) Homogeneous composite types in Pascal

Passing Arrays As Parameters: An Example (3) procedure displayGrades (lecture01 : Grades; average : real); var i : integer; begin writeln('Grades for the class...'); for i := 1 to CLASS_SIZE do writeln('Grade for student no. ', i, ' is ', lecture01[i]:6:2, '%'); writeln('The average grade is ', average:6:2, '%'); writeln; end; Homogeneous composite types in Pascal

Passing Arrays As Parameters: An Example (4) begin var lecture01 : Grades; var average : real; tabulateGrades (lecture01, average); displayGrades (lecture01, average); end. Homogeneous composite types in Pascal

Returning Arrays From Functions Declare a type for the array. e.g. const CLASS_SIZE = 5; type Grades = array [1..CLASS_SIZE] of real; Declare an instance of this type. e.g., var lecture01 : Grades; Return the instance of the array as you would any other return value. (Function call) lecture01 := fun (lecture01); (Function definition) function fun (lecture01 : Grades ): Grades; Homogeneous composite types in Pascal

Segmentation Faults And The Array Bounds (2) RAM a.out [1] [2] [3] [4] list OK OK OK OK ??? Homogeneous composite types in Pascal

Segmentation Faults And The Array Bounds (2) RAM a.out CORE [1] [2] [3] [4] list OK OK (Big file) Wav file from “The SImpsons” OK OK ??? Homogeneous composite types in Pascal

Segmentation Faults And The Array Bounds (3) When using an array take care not to exceed the bounds. Ways of reducing the likelihood of exceeding the bounds of the array: Use a constant in conjunction with arrays e.g., const MAX = 5; Refer to the constant when declaring an array: var aList : array [1..MAX] of integer; Refer to the constant when declaring the type for the array: type List = array [1..MAX] of integer; Refer to the constant when iterating/traversing through the array: for i := 1 to MAX do writeln('Grade for student no. ', i, ' is ', lecture01[i]:6:2, '%'); Homogeneous composite types in Pascal

Segmentation Faults And The Array Bounds (4) Make sure that array indices are properly initialized. You may need to verify this assumption with debugging statements. Incorrect : What is the current value of index ‘i’? program array1 (output); begin var i : integer; var list : array [1..2] of integer; list [i] := i; writeln (list[i]); end. Correct : Always initialize your variables before using them: in this case the index ‘i’ is set to a value within the bounds of the array before it’s used. program array2 (output); begin var i : integer; var list : array [1..2] of integer; i := 2; list [i] := i; writeln (list[i]); end. Homogeneous composite types in Pascal

The String Type It is a special type of character array. Format for declaration: var name : string [No of elements]; Example declaration: var firstName : string [MAX]; Homogeneous composite types in Pascal

Benefits Of The String Type The end of array is marked. Many operations have already been implemented. Homogeneous composite types in Pascal

Marking The End Of The Array The full example can be found in Unix under the path: /home/231/examples/arrays/stringExample.p program stringExample (output); const MAX = 8; begin var list1 : array [1..MAX] of char; var list2 : string[MAX]; list1 := 'abcdefg'; list2 := 'abcdefg'; writeln('-', list1, '-'); writeln('-', list2, '-'); end. Homogeneous composite types in Pascal

The Contents Of The String “List2” [1] [2] [3] [4] [5] [6] [7] [8] ‘a’ ‘b’ ‘c’ ‘d’ ‘e’ ‘f’ ‘g’ END Homogeneous composite types in Pascal

Strings Are A Built-In Type1 This means that they can be passed as parameter in the same fashion as other built in types, no type needs to be defined beforehand. Format: procedure procedureName (stringName : string); OR procedure procedureName (var stringName : string); Examples: procedure proc1 (list : string); procedure proc2 (var list : string); 1 For many programming languages and some versions of Pascal Homogeneous composite types in Pascal

When To Use Arrays Of Different Dimensions Determined by the data – the number of categories of information determines the number of dimensions to use. Examples: (1D array) Tracking grades for a class Each cell contains the grade for a student i.e., grades[i] There is one dimension that specifies which student’s grades are being accessed (2D array) Expanded grades program Again there is one dimension that specifies which student’s grades are being accessed The other dimension can be used to specify the lecture section One dimension (which student) Homogeneous composite types in Pascal

When To Use Arrays Of Different Dimensions (2) (2D array continued) Student Lecture section First student Second Third … L01 L02 L03 L04 L05 : L0N Homogeneous composite types in Pascal

When To Use Arrays Of Different Dimensions (3) (2D array continued) Notice that each row is merely a 1D array (A 2D array is an array containing rows of 1D arrays) Columns [1] [2] [3] [4] [1] [2] [3] [4] [5] [6] [7] L01 Rows L02 L03 L04 L05 L06 L07 Homogeneous composite types in Pascal

When To Use Arrays Of Different Dimensions (4) (3D array – take the 2D array but allow for multiple courses). The third dimension specifies which course grades are being tracked. Course (Z) Student (X = column) Lecture (Y = row) Note: The standard approach for specifying the dimensions is to specify the row coordinate (Y) and then the column coordinate (X). The size of a dimension must be the same for all elements along that dimension e.g., all rows must be of the same size Homogeneous composite types in Pascal

When To Use Arrays Of Different Dimensions (5) L06 L01 L02 L03 L07 L05 L04 Student 3 … Student 2 Student 1 CPSC 231 CPSC 233 CPSC 235 Homogeneous composite types in Pascal

Declaring Multi-Dimensional Arrays Format: (Two dimensional arrays) Name : array [min..max, min..max] of type; (Three dimensional arrays) Name : array [min..max, min..max, min..max] of type; Examples: var johnFinances : array [1..3, 1..7] of real; var cube : array[1..6, 1..6, 1..6] of char; Rows Columns Homogeneous composite types in Pascal

Declaring Multi-Dimensional Arrays As A Type Format: Type declaration Type name = array [min..max, min..max] of element type; Type name = array [min..max, min..max, min..max] of element type; Variable declaration array name : Type name; Homogeneous composite types in Pascal

Declaring Multi-Dimensional Arrays As A Type (2) Example: Type declaration Finances = array [1..3, 1..7] of real; Cube = array [1..6, 1..6, 1..6] of char; Variable declaration var johnFinances : Finances; var aCube : Cube; Homogeneous composite types in Pascal

Accessing / Assigning Values To Elements Format: name [row][column] := name [row][column]; Example: finances [1][1] := 4500; writeln (finances[1][1]); Homogeneous composite types in Pascal

Multi-Dimensional Arrays And Input/Output Arrays of more than one dimension (including multi-dimensional character arrays) cannot be passed as parameters to: read, readln, write, writeln. Only one-dimensional character arrays can be passed as parameters to these procedures. Homogeneous composite types in Pascal

Example 2D Array Program: A Character-Based Grid You can find the full program in Unix under: /home/231/examples/arrays/grid.p Homogeneous composite types in Pascal

A Character-Based Grid program gridExample (input, output); const MAX_ROWS = 4; MAX_COLUMNS = 4; NO_COMBINATIONS = 10; type Grid = array[1..MAX_ROWS, 1..MAX_COLUMNS] of char; Homogeneous composite types in Pascal

A Character-Based Grid (2) function generateElement (temp : integer) : char; var anElement : char; begin case (temp) of 1, 2, 3, 4, 5, 6 : anElement := ' '; 7, 8, 9: anElement := '*'; 10: anElement := '.'; Homogeneous composite types in Pascal

A Character-Based Grid (3) else begin writeln('<< Error with the random no. generator.>>'); writeln('<< Value should be 1-10 but random value is ', temp); anElement := '!'; end; generateElement := anElement; Homogeneous composite types in Pascal

A Character-Based Grid (4) procedure initialize (var aGrid : Grid); var r : integer; c : integer; temp : integer; begin for r := 1 to MAX_ROWS do for c := 1 to MAX_COLUMNS do temp := random(NO_COMBINATIONS) + 1; aGrid[r][c] := generateElement(temp); end; Homogeneous composite types in Pascal

A Character-Based Grid (5) procedure display (aGrid : Grid); var r : integer; c : integer; begin for r := 1 to MAX_ROWS do for c := 1 to MAX_COLUMNS do write(aGrid[r][c]); end; writeln; Homogeneous composite types in Pascal

A Character-Based Grid (6) procedure displayLines (aGrid : Grid); var r : integer; c : integer; begin for r := 1 to MAX_ROWS do writeln(' - - - -'); for c := 1 to MAX_COLUMNS do write('|', aGrid[r][c]); end; writeln('|'); Homogeneous composite types in Pascal

A Character-Based Grid (7) begin var aGrid : Grid; initialize(aGrid); writeln('Displaying grid'); writeln('==============='); display(aGrid); writeln; writeln('Displaying grid with bounding lines'); writeln('=========================='); displayLines(aGrid); end. Homogeneous composite types in Pascal

Valid Operators: 1D Character Arrays And Strings The relational operators will work with the String type and 1-dimensional character arrays. They will not work with other type of arrays. Homogeneous composite types in Pascal

You Should Now Know What is the difference between simple types (atomic) and composite types (aggregate). What is the benefit of using homogeneous composite types (arrays) How to declare arrays. How to access or assign values to array elements. How to work with an entire array (e.g., access or assign values to different parts). How to pass instances of arrays into functions and procedures and how to return an array from a function. What is a segmentation fault and what is a core dump file. How to declare and to use instances of a string type. The number of dimensions to declare for an array. How to declare and traverse arrays of multiple dimensions. How to display “bounding lines” around array elements as a formatting technique. Homogeneous composite types in Pascal