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.

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

Program CheckPass; var TestScore, ExamScore : integer; FinalScore : real; Status : string; begin write(‘Enter the Test score:’); readln(Testscore); write(‘Enter.
J. Michael Moore Text Files CSCE 110 From James Tam’s material.
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.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to 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 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.
Chapter 10.
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 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.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 2, Lecture 2.
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.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
J. Michael Moore From James Tam's material Records 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 Modules – the Basics CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
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.
Guide To UNIX Using Linux Third Edition
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,
Review of C++ Programming Part II Sheng-Fang Huang.
Homework Reading Programming Assignments
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 A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real.
Foundation Studies Course M.Montebello Records Foundation Studies Course.
Pascal Programming Strings, Arithmetic operators and output formatting National Certificate – Unit 4 Carl Smith.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
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.
Copyright 1999 by Larry Fuhrer. Pascal Programming Getting Started...
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
Introduction to Pascal The Basics of Program writing.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Sha Tin Methodist College F.4 Computer Studies Pascal Programming.
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.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
Pascal Programming Today Chapter 11 1 Chapter 11.
1 STRINGS String data type Basic operations on strings String functions String procedures.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
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.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Variables reference, coding, visibility. Rules for making names  permitted character set  maximum length, significant length  case sensitivity  special.
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.
1 More on Readln:numerical values Note: ENTER key counts sends a carriage return and a line feed to the computer definition: “white space”: space, tab,
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
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:

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 grades are saved as percentages. 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.

J. Michael Moore Why Bother With Composite Types? 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;

J. Michael Moore 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, '%');

J. Michael Moore 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!

J. Michael Moore 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. An array!

J. Michael Moore Data Types simple structured pointer ordinalreal predefined booleancharinteger programmer-defined enumeratedsubrange arrayrecord setfile predefinedprogrammer-defined text Homogenous

J. Michael Moore 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]

J. Michael Moore 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] ” 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 index refers to a single element

J. Michael Moore Assigning Data To The Array Format: (Whole array)(One element) nameOfArray := value; nameOfArray[index] := value; Examples (assignment via the assignment operator): (Whole array)(One element) firstArray := secondArray;classGrades[1] := 100;

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

J. Michael Moore 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 (exception: one-dimensional character arrays can be passed)

J. Michael Moore Accessing The Data In The Array Examples (displaying information): (Single element) writeln('Grade for student 1: ',classGrades[1]:6:2); (Whole array – all elements) for i := 1 to CLASS_SIZE do writeln('Grade for student # ', i:2, ': ', classGrades[i]:6:2);

J. Michael Moore 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 (exception: one-dimensional character arrays can be passed)

J. Michael Moore Revised Version Using An Array const CLASS_SIZE = 5; begin var classGrades: array [1..CLASS_SIZE] of real; var i: integer; var total: real; var average: real;

J. Michael Moore Class Example Using An Array (2) total := 0; for i := 1 to CLASS_SIZE do begin write('Enter grade for student # ', 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 # ', i, ' is ', classGrades[i]:6:2, '%');

J. Michael Moore Passing Arrays As Parameters 1.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 (e.g. A blueprint). -No memory is allocated.

J. Michael Moore 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 (lecture01, average); (Function/procedure definition) procedure displayGrades (lecture01 : Grades; average : real);

J. Michael Moore Passing Arrays As Parameters: An Example 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;

J. Michael Moore Passing Arrays As Parameters: An Example (2) begin(* tabulateGrades *) total := 0; for i := 1 to CLASS_SIZE do begin write('Enter grade for student # ', i, ': '); readln(lecture01[i]); total := total + lecture01[i]; end; average := total / CLASS_SIZE; writeln; end;(* tabulateGrades *)

J. Michael Moore 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 # ', i, ' is ', lecture01[i]:6:2, '%'); writeln('The average grade is ', average:6:2, '%'); writeln; end;

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

J. Michael Moore Returning Arrays From Functions 1.Declare a type for the array. e.g. const CLASS_SIZE = 5; 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 call) lecture01 := fun (lecture01); (Function definition) function fun (lecture01: Grades ): Grades;

J. Michael Moore Segmentation Faults And The Array Bounds (2) RAM a.out [1] [2] [3] [4] list OK ???

J. Michael Moore Segmentation Faults And The Array Bounds (2) RAM a.out OK CORE (Big file) [1] [2] [3] [4] list ???

J. Michael Moore 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: 1.Use a constant in conjunction with arrays e.g., const MAX = 5; 2.Refer to the constant when declaring an array: var aList : array [1..MAX] of integer; 3.Refer to the constant when declaring the type for the array: type List = array [1..MAX] of integer; 4.Refer to the constant when iterating/traversing through the array: for i := 1 to MAX do writeln('Grade for student # ', i, ' is ', lecture01[i]:6:2, '%');

J. Michael Moore Segmentation Faults And The Array Bounds Make sure that array indices are properly initialized -You may need to verify this assumption with debugging statements. What is the current value of index ‘i’? 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 array1 (output); begin var i : integer; var list : array [1..2] of integer; list [i] := i; writeln (list[i]); end. program array2 (output); begin var i : integer; var list : array [1..2] of integer; i := 2; list [i] := i; writeln (list[i]); end.

J. Michael Moore The String Type It is a special type of character array. Format for declaration: var name : string [NumberOfElements]; Example declaration: var firstName : string[MAX];

J. Michael Moore Benefits Of The String Type 1.The end of the array is marked. 2.Many operations have already been implemented.

J. Michael Moore Marking The End Of The Array program stringExample (output); const MAX = 7; begin var list1 : array [1..MAX] of char; var list2 : string[MAX]; list1 := 'abcd'; list2 := 'abcd'; writeln('-', list1, '-'); writeln('-', list2, '-'); end. Output:

J. Michael Moore The Contents Of The Lists [1][2][3][4][5][6][7] ‘a’‘b’‘c’‘d’END [1][2][3][4][5][6][7] ‘a’‘b’‘c’‘d’ List2 (String) List1 (Array of characters)

J. Michael Moore Strings Are A Built-In Type 1 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); OR procedure proc2 (var list : string); 1 For many programming languages and some versions of Pascal