1) Introduction to higher level types 2) Arrays and their declaration 3) Assigning values to array elements 4) Operations on arrays 5) Ada array attributes.

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

1) Scope a] Ada scope rules b] C scope rules 2) Parameter passing a] Ada parameter modes b) Parameter passing mechanisms COMP205 IMPERATIVE LANGUAGES 13.
One Dimensional Arrays
Programming Languages and Paradigms
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
1) Constrained (static) and unconstrained (dynamic) arrays. 2) Flexible arrays 3) Multi-dimensional arrays 4) Arrays of arrays 5) Lists, sets, bags,Etc.
Kernighan/Ritchie: Kelley/Pohl:
Chapter 9 Imperative and object-oriented languages 1.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Enumerated Types 4 Besides the built-in types, ANSI C allows the definition of user-defined enumerated types –To define a user-define type, you must give.
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.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Programming with Collections Collections in Java Using Arrays Week 9.
ISBN Chapter 6 Data Types: Structured types.
1) Pointers 2) Type declarations 3) Access/reference values 4) Type renaming/aliasing 5) Subtypes 6) Macro substitution COMP205 IMPERATIVE LANGUAGES 4.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
1 ) Data Attributes 2) Declarations, Assignment and Instantiation 3) Global and Local Data 4) Variables 5) Constants 6) Example programs COMP205 IMPERATIVE.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
Arrays. 2 The array data structure An array is an indexed sequence of components Typically, the array occupies sequential storage locations The length.
1) More on parameter passing: a) Parameter association b) Default parameters c) Procedures as parameters 2) Modules: Ada packages, C modules, C header.
1) Data Review 2) Anonymous Data Items 3) Renaming 4) Overloading 5) Introduction to Data Types 6) Basic types 7) Range and precision 8) Ada/Pascal “attributes”
1) Enumerated types 2) Records and structures 3) Accessing fields in records/structures 4) Operations on records/structures 5) Variant records 6) Dynamic.
Chapter 9: Arrays and Strings
1) Causes of errors 2) Classification of errors 3) Signals and exceptions 1) Program hierarchy 2) Blocks 3) Routines 4) Procedures and functions COMP205.
Arrays. 2 The array data structure An array is an indexed sequence of components Typically, the array occupies sequential storage locations The length.
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
Programming Languages
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
Names Variables Type Checking Strong Typing Type Compatibility 1.
DCT1063 Programming 2 CHAPTER 5 ADVANCED DATA TYPE (part 1) Mohd Nazri Bin Ibrahim Faculty of Computer Media and Technology TATi University College
Chapter 8 Arrays and Strings
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
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.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Lecture 3 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout from Lecture 1 Background.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
CHAPTER 7: Arrays CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (Feb 2012)
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Data Handling in Algorithms. Activity 1 Starter Task: Quickly complete the sheet 5mins!
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
structured statically typed imperative wide-spectrum object-oriented high-level computer programming language extended from Pascal and other languages.
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.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Computer Programming BCT 1113
COMP205 IMPERATIVE LANGUAGES
Visual Basic .NET BASICS
EKT150 : Computer Programming
Lecture 18 Arrays and Pointer Arithmetic
Presentation transcript:

1) Introduction to higher level types 2) Arrays and their declaration 3) Assigning values to array elements 4) Operations on arrays 5) Ada array attributes COMP205 IMPERATIVE LANGUAGES 5. COMPOUND (HIGHER LEVEL) DATA TYPES I --- ARRAYS

INTRODUCTION TO HIGHER LEVEL TYPES The available basic types can be extended by adding higher level types. Higher level types are (usually user-defined) data types made up of basic types and other user-defined higher level types. There are some standard higher level types available in most imperative languages. These include: 1) Arrays 2) Strings 3) Enumerated types 4) Records/structures 5) Unions

TYPE CONSTRUCTORS Higher level types are declared using type constructors which "construct" new types out of zero or more existing types. Higher level types can either: a) Be used directly and anonymously in a declaration b) Be bound to a type name first using a type declaration statement (the type name can then be used later in the code to create instances of that type). Ada type declaration statements are of the form: C type declaration statements are of the form: `is' `typedef'

ARRAYS The simplest (and oldest) form of higher level data type is the array. An array is a series of items all of the same type. Each item is assigned a location in memory. At each location a value may be held. F 100 R 104 A 108 N 112 S C 124 O 128 E 132 N 138 E 140 N 144

Features : 1) Items in arrays are called elements. 2) Specific elements in an array can be identified through the use of an index. 3) Indices can be: a) Discrete – for example integers, characters or enumerated values (also referred to as linear indexing). b) Non-discrete - indexing using non-linear types (e.g. floats) (also referred to as associative indexing). C and Ada only support linear indexing. 4) The first index in an array is called the lower bound and the last the upper bound.

ARRAY DECLARATIONS When declaring arrays we are doing two things: 1) Declaring the type of the array 2) Declaring the nature of the index

APPROACHES TO DEFINING INDEXES 1) Assume index is an integer type and define either: –Only the upper bound (assumes the lower bound is fixed), or –both the lower and upper bound. 2) Specify the data type of the index and provide lower and upper bounds as appropriate. 3) Define the index only in terms of a data type, in which case the index range for the array is assumed to be equivalent to the range of the data type in question. C only supports the first, Ada supports all three.

THERE NOW FOLLOWS A SERIES OF EXAMPLES DEFINING VARIOUS ARRAY DATA ITEMS!

DECLARING ARRAYS IN ADA In Ada, to declare arrays, we use an array data declaration statement of the form: NAME ‘: array' ‘of' TYPE ';' or an array type declaration: ‘type' TYPENAME ‘is' ‘of' TYPE ';'

EXAMPLE 1 (Ada) Assume index is an integer and define lower and upper bounds. Format: ‘(‘ LOWER_BOUND ‘..’ UPPER_BOUND ‘)’ Example declarations: ARRAY1 : array (1..9) of integer; N: integer; ARRAY2 : array (N..N+4) of integer;

EXAMPLE 2 (Ada) Approach: Index type specified by a lower and upper bound. Format: ‘(‘ INDEX_TYPE ‘range’ LOWER_BOUND ‘..’ UPPER_BOUND ‘)’ Example declaration: ARRAY3 : array (CHARACTER range a..z) of integer;

EXAMPLE 3 (Ada) Approach: Particular data type. Format: ‘(‘ INDEX_TYPE ‘)’ Example declaration: type ITEMS_T is range ; ARRAY4 : array (ITEMS_T) of integer;

EXAMPLE 4 (C) Assumes: –Index is an integer type. –Lower bound is 0. Declaration format: TYPE NAME ‘[‘ NUM_ELEMENTS ‘]’ ‘;’ Example declaration: Declares an array of 10 elements with an index range of 0 to 9 inclusive. C array declaration is more succinct but harder to read (then the Ada version). int list1[10];

MANIPULATION OF ARRAYS Assigning values Example programs Operations on complete arrays Slices Conformant array parameters Ada array attributes

ASSIGNMENT OF VALUES TO ARRAY ELEMENTS Given knowledge of the index for an array element we can assign a value to that element (or change its value) using an "assignment" operation. Examples (Ada and C): LIST1(1):= 1; list1[0] = 1;

COMPOUND VALUES Some imperative languages support the concept of compound values which allow simultaneous assignment to instances of higher level data types. Array compound values allow all the elements of an array to be assigned to simultaneously. Both C and Ada support compound values (in Ada they are called aggregates). However, in C their usage is limited to initialisation, while in Ada array aggregates can be used in any assignment statement

C Example LIST:= (5, 4, 3, 2, 1, other => 0); Ada Example Ada also supports an alternative to the above: int list[10] = {5, 4, 3, 2, 1, 0, 0, 0, 0, 0}; type MY_ARRAY_T is array (1..10) of integer; LIST : MY_ARRAY_T := (5, 4, 3, 2, 1, 0, 0, 0, 0, 0);

ADA ARRAY EXAMPLE PROGRAM procedure EXAMPLE is START: constant := 2; IA: array (integer range START..START+4) of integer := (2,4,6,8,10); begin put("Array comprises = "); put(IA(2)); put(", "); put(IA(3)); put(", "); put(IA(4)); put(", "); put(IA(5)); put(", "); put(IA(6)); new_line; end EXAMPLE; Note that the above can be made more succinct if some form of loop construct was used to step through the array.

C ARRAY EXAMPLE PROGRAM void main(void) { int ia[5] = {2,4,6,8,10}; printf("Size of array = %d (bytes)\n",sizeof(ia)); printf("Num elements = %d\n",sizeof(ia)/sizeof(ia[0])); printf("Array comprises = %d, %d, %d, %d, %d\n", ia[0], ia[1], ia[2], ia[3], ia[4]); printf("Address ia[0] = %d\n",&ia[0]); printf("ia = %d\n",ia); } Size of array = 20 (bytes) Num elements = 5 Array comprises = 2, 4, 6, 8, 10 Address ia[0] = ia =

OPERATIONS ON COMPLETE ARRAYS Some languages, such as Pascal and PL/1 (but not Ada or C), allow operations on complete arrays. In PL/1 we can write: This will make all elements in the array IA equal to 0. Alternatively, some languages (Pascal, Ada) support assignment of one array to another array: Where LIST1 and LIST2 are two arrays of the same type (but not supported by C). IA = 0; LIST1 := LIST2

SLICES A Slice is a sub-array of an array. The process of slicing returns a sub-array (supported by Ada but not C). Thus: The statement would assign the sub array ('b', 'c', 'd') to the variable IA_SUB. IA: array (integer range START..START+4) of character := ('a', 'b', 'c', 'd', 'e'); IA_SUB = IA(START+1..START+3)

procedure EXAMPLE is START: constant := 2; type MY_ARRAY_T is array (integer range START.. START+4) of integer; LIST1: MY_ARRAY_T := (2,4,6,8,10); LIST2, LIST3: MY_ARRAY_T; procedure OUTPUT_ARRAY (LIST: MY_ARRAY_T) is begin --- Output array contents --- end OUTPUT_ARRAY; begin OUTPUT_ARRAY(LIST1); LIST2 := LIST1; OUTPUT_ARRAY(LIST2); LIST3 := LIST1(2..4) & LIST2(2..3); OUTPUT_ARRAY(LIST3); end EXAMPLE; SLICES EXAMPLE Note: all arrays are of the same type.

CONFORMANT ARRAY PARAMETERS To be compatible two arrays must be of the same size and type. This means that general purpose array procedures can only work for arrays of a pre-specified size and type. To get round this we can allow formal array parameters to be given bounds that can very depending on the nature of the actual parameter at the time the procedure is called. Such parameters are referred to as conformant array parameters. Supported by Pascal (but not Ada or C).

ADA ARRAY ATTRIBUTES T'FIRST Gives first index value for array type T T'LAST Gives last index value for array type T T'LENGTH Gives number of components in array type T Note that instead of an array type an array name may equally well be used.

procedure EXAMPLE is START: constant := 2; IA: array (integer range START..START+4) of character := ('a', 'b', 'c', 'd', 'e'); begin put("IA'first = "); put(IA'first); new_line; put("IA'last = "); put(IA'last); new_line; put("IA'length = "); put(IA'length); new_line; end EXAMPLE ;

SUMMARY 1) Introduction to higher level types 2) Arrays and their declaration 3) Assigning values to array elements 4) Operations on arrays 5) Ada array attributes