James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.

Slides:



Advertisements
Similar presentations
James Tam Linked Lists in Pascal Linked Lists In this section of notes you will learn how to create and manage a dynamic list.
Advertisements

An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Introduction to Programming CSCE 110 Drawn from James Tam’s material.
Program CheckPass; var TestScore, ExamScore : integer; FinalScore : real; Status : string; begin write(‘Enter the Test score:’); readln(Testscore); write(‘Enter.
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 Repetition In Pascal In this section of notes you will learn how to have a section of code repeated without duplicating the code.
James Tam Breaking Problems Down This section of notes shows you how to break down a large programming problem into smaller modules that are easier to.
James Tam Getting Started With Pascal Programming How are computer programs created What is the basic structure of a Pascal Program Variables in Pascal.
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 Introduction To Files In Pascal In this section of notes you will learn how to read from and write to files in Pascal.
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 Repetition In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
James Tam Introduction To Files In Pascal In this section of notes you will learn how to read from and write to files in your Pascal programs.
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.
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 Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
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 Getting Started With Pascal Programming How are computer programs created What is the basic structure of a Pascal Program Variables and constants.
James Tam Getting Started With Pascal Programming What is the basic structure of a Pascal Program Variables in Pascal Performing input and output with.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
James Tam Breaking Problems Down This section of notes shows you how to break down a large programming problem into smaller modules that are easier to.
J. Michael Moore Input and Output (IO) CSCE 110 Drawn from James Tam's material.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
J. Michael Moore From James Tam’s material Multi-Dimensional Arrays CSCE 110.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate your code.
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.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
Getting Started With Pascal Programming
J. Michael Moore From James Tam's material Records CSCE 110.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
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 Breaking Problems Down This section of notes shows you how to break down a large programming problem into smaller modules that are easier to.
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 Recursion You will learn what is recursion as well as how and when to use it in your programs.
Link Lists In this section of notes you will learn how to create and manage a dynamic list. Link Lists in Pascal.
James Tam Introduction To Files In Pascal You will learn how to read from and write to text files in Pascal.
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 Records You will learn in this section of notes what is a record and how to use them in Pascal.
James Tam Pointers In this section of notes you will learn about another type of variable that stores addresses rather than data.
Input and Output (IO) CSCE 110 Drawn from James Tam's material.
James Tam Pointers In this section of notes you will learn about a third type of variable that stores addresses rather than data.
Simple Arrays Programming COMP104 Lecture 12 / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g., a collection.
James Tam Introduction To Files In Pascal In this section of notes you will learn how to read from and write to files in Pascal.
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.
James Tam Introduction To Files In Pascal In this section of notes you will learn how to read from and write to text files in Pascal.
ARRAYS 1.Basic Ideas 2.The Array Type 3.Processing Arrays 4.Parallel Arrays 5.Two-dimensional Array 6.Arrays as Parameters.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Introduction to Pascal The Basics of Program writing.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday)
Foundation Studies Course M.Montebello Arrays Foundation Studies Course.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
James Tam Multi-Dimensional Arrays In Pascal In this section of notes you will learn about how and when to use multi- dimensional arrays.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
Array : 1-dimension อนันต์ ผลเพิ่ม Anan Phonphoem
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
FOR LOOP STRUCTURE For := to do eg. for I := 1 to 100 do begin writeln(‘This is a loop’); end;
kbkjlj/m/lkiubljj'pl;
Arrays In this section of notes you will be introduced to a homogeneous composite type, one-dimensional arrays One-dimensional arrays in Pascal.
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.
Presentation transcript:

James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays

James Tam Simple Types (Atomic) 1)Integer 2)Real 3)Char 4)Boolean

James Tam Composite Types (Aggregate) 1)Homogeneous arrays 2)Heterogeneous records

James Tam Why Bother With Composite Types? For a compilable example look in Unix under: /home/231/examples/arrays/classList1.p const CLASSSIZE = 5; var stu1, stu2, stu3, stu4, stu5: real; total, average : real; begin write('Enter grade for student number 1: '); readln(stu1);

James Tam Why Bother With Composite Types (2) ? 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 / CLASSSIZE; writeln('The average grade is ', average:6:2, '%');

James Tam With Bother With Composite Types (3) (* Printing the grades for the class. *) Writeln('Student: ', 1, stu1); Writeln('Student: ', 2, stu2); Writeln('Student: ', 3, stu3); Writeln('Student: ', 4, stu4); Writeln('Student: ', 5, stu5);

James Tam With Bother With Composite Types (3) (* Printing the grades for the class. *) Writeln('Student: ', 1, stu1); Writeln('Student: ', 2, stu2); Writeln('Student: ', 3, stu3); Writeln('Student: ', 4, stu4); Writeln('Student: ', 5, stu5); NO!

James Tam Revised Version Using An Array For compilable example look in Unix under: /home/231/examples/arrays/classList2.p const CLASSSIZE = 5; var classGrades : array [1..CLASSSIZE] of real; i : integer; total, average : real; begin total := 0;

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

James Tam Declaring Arrays Syntax: Name: array [low index..high index] of element type; Example: classGrades : array [1..CLASSSIZE] of real; classGrades [1] [2] [3] [4] [5]

James Tam Accessing Data In The Array First need to indicate which array is being accessed Done via the name of the array If are accessing a single element, you need to indicate which element that you wish to access. Done via the array index Syntax: (Whole array) (One element) name name [index] Examples (assignment via the assignment operator): (Whole array) firstArray := secondArray; (One element) classGrades [1] := 100;

James Tam Accessing Data In The Array (2) Examples (assignment via read or readln): (Single element) readln(classGrades[1]) (Whole array – all elements) for i: = 1 to CLASSIZE do begin write('Input grade for student No. ', i, ': '); readln(classGrades[i]); end;

James Tam Accessing Data In The Array (3) Examples (displaying information): (Single element) writeln(classGrades[1]); (Whole array – all elements) for i := 1 to CLASSSIZE do writeln('Grade for student No. ', i, ' ', classGrades[i]);

James Tam Common 1 Array Operations Declaration Done previously in this set of notes (slide No. 8, line of code No. 4) Initialization / Assignment of all elements Done previously in this set of notes (slide No. 9, lines of code No. 1 – 4). Extracting Elements Single element – done previously in this set of notes (slides No. 11 & 13) All elements – done previously in this set of notes (slide No. 9, line of code No. 11, slides No. 11 & 13) In order copy between two arrays Using the assignment operator – done previously in this set of notes (slide No. 11) Manual copy – coming up Reverse order copy between two arrays Manual copy – coming up 1) Common but by no means complete

James Tam In-Order Copy Between Arrays Method 1: Using the assignment operator e.g., array1 := array2; Method 2: Manual copy Use loops and copy from one array to another element-by-element Example of manual copy (full example can be found in Unix under /home/231/examples/inorderArrayCopy.p) const SIZE = 5; MAXVALUE = 11; var array1 : array [1..SIZE] of integer; array2 : array [1..SIZE] of integer; i : integer;

James Tam In-Order Copy Between Arrays (2) begin for i:= 1 to SIZE do array1[i] := trunc (RANDOM * MAXVALUE); for i:= 1 to SIZE do array2[i] := array1[i]; writeln; for i:= 1 to SIZE do writeln('array1: ', array1[i]:2, ' array2: ', array2[i]:2);

James Tam Reverse Order Copy Between Arrays const SIZE = 5; MAXVALUE = 11; var array1 : array [1..SIZE] of integer; array2 : array [1..SIZE] of integer; i : integer; begin for i:= 1 to SIZE do array1[i] := trunc (RANDOM * MAXVALUE);

James Tam Reverse Order Copy Between Arrays (2) for i:= 1 to SIZE do begin array2[SIZE-i+1] := array1[i]; end; writeln; for i:= 1 to SIZE do writeln('array1: ', array1[i]:2, ' array2: ', array2[i]:2); writeln;

James Tam Summary What is the difference between simple types (atomic) and composite types (aggregate)? Why is the benefit of using homogeneous composite types (arrays)? How are some common operations performed with arrays in Pascal? Declaration Initialization and assignment Extracting elements In order copy of elements Reverse order copy of elements