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

Slides:



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

James Tam Linked Lists in Pascal Linked Lists In this section of notes you will learn how to create and manage a dynamic list.
Program CheckPass; var TestScore, ExamScore : integer; FinalScore : real; Status : string; begin write(‘Enter the Test score:’); readln(Testscore); write(‘Enter.
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 Advanced Composite Types You will learn in this section of notes how to create single and generic instances of non-homogeneous composite types.
James Tam Recursion You will learn what is recursion as well as how simple recursive programs work.
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 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 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 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.
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.
© The McGraw-Hill Companies, 2006 Chapter 5 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 Multi-Dimensional Arrays In Pascal In this section of notes you will learn about how and when to use multi- dimensional arrays.
James Tam Recursion You will learn what recursion is as well as how simple recursive programs work.
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 Recursion You will learn what is recursion as well as how simple recursive programs work.
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.
James Tam Recursion You will learn what is recursion as well as how simple recursive programs work.
J. Michael Moore Modules – the Basics CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
Link Lists In this section of notes you will learn how to create and manage a dynamic list. Link Lists 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 Advanced Programming Concepts You will learn how advanced programming constructs and concepts are implemented in ‘C’
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 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.
J. Michael Moore Modules – the Basics CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
J. Michael Moore Recursion CSCE 110 From James Tam’s material.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
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.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
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.
ARRAYS 1.Basic Ideas 2.The Array Type 3.Processing Arrays 4.Parallel Arrays 5.Two-dimensional Array 6.Arrays as Parameters.
Introduction to Pascal The Basics of Program writing.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday)
Pascal Programming Today Chapter 11 1 Chapter 11.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
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.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
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,
Recursion You will learn what recursion is as well as how simple recursive programs work Recursion in Pascal.
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 Types Of Variables Pascal Variables 1.Simple (atomic) integer char boolean real Homogenous (arrays) Heterogeneous (records) 2. Aggregate (composite)

James Tam Types Of Variables Pascal Variables 1.Simple (atomic) integer char boolean real Homogenous (arrays) Heterogeneous (records) 2. Aggregate (composite)

James Tam 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; 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 / CLASS_SIZE; writeln('The average grade is ', average:6:2, '%');

James Tam 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.

James Tam 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!

James Tam 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!

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

James Tam First you need to 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]” Accessing Data In The Array classGrades [1] [2] [3] [4] [5] classGrades [1] [2] [3] [4] [5] Using only the name of the array refers to the whole array Use the array name and the subscript refers to a single element

James Tam Assigning Data To The Array Format: (Whole array) (One element) name of array name of array [index] Examples (assignment via the assignment operator): (Whole array) (One element) firstArray := secondArray; classGrades [1] := 100;

James Tam 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;

James Tam Assigning Data To The Array (3) (Whole array – all elements: Character arrays only) var charArray : array [1..5] of char; readln(charArray);

James Tam Accessing 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);

James Tam Accessing Data In The Array (2) (Whole array – all elements: Character arrays only) var charArray : array [1..5] of char; write(charArray);

James Tam 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;

James Tam 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, '%'); for i := 1 to CLASS_SIZE do writeln('Grade for student no. ', i, ' is ', classGrades[i]:6:2, '%');

James Tam Passing Arrays As Parameters 1.Declare a type for the array. e.g. 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.

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

James Tam 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;

James Tam 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 *)

James Tam 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;

James Tam Passing Arrays As Parameters: An Example (4) begin var lecture01 : Grades; var average : real; tabulateGrades (lecture01, average); displayGrades (lecture01, average); end.

James Tam Returning Arrays From Functions 1.Declare a type for the array. e.g. type Grades = array [1..CLASS_SIZE] of real; 2.Declare an instance of this type. e.g., var lecture01 : Grades; 3.Return the instance of the array as you would any other return value. (Function/procedure call) lecture01 := fun (L01); (Function/procedure definition) function fun (lecture01 : Grades ): Grades;

James Tam Segmentation Faults And Arrays RAM a.out [1] [2] [3] [4] list OK ???

James Tam Segmentation Faults And Arrays RAM a.out [1] [2] [3] [4] list OK ??? CORE (Big) Wav file from “The SImpsons”

James Tam 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 How to pass instances of arrays into methods and how to return an array from a function. What is a segmentation fault and core dump file.