1) Constrained (static) and unconstrained (dynamic) arrays. 2) Flexible arrays 3) Multi-dimensional arrays 4) Arrays of arrays 5) Lists, sets, bags,Etc.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Dynamic memory allocation
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
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.
Programming Languages and Paradigms The C Programming Language.
Chapter 7:: Data Types Programming Language Pragmatics
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Engineering Problem Solving with C Fundamental Concepts Chapter 6 Pointers.
Prog Lang & Alg1 Programming Languages Imperative Programming Languages - Part 1 - Principles & Data.
Kernighan/Ritchie: Kelley/Pohl:
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
COMP205 IMPERATIVE LANGUAGES
Chapter 9 Imperative and object-oriented languages 1.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Chapter 10.
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.
1 ) Data Attributes 2) Declarations, Assignment and Instantiation 3) Global and Local Data 4) Variables 5) Constants 6) Example programs COMP205 IMPERATIVE.
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 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
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) Introduction to higher level types 2) Arrays and their declaration 3) Assigning values to array elements 4) Operations on arrays 5) Ada array attributes.
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
Pointers Applications
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Chapter 8 Arrays and Strings
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
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.
Pemrograman Dasar Arrays PTIIK - UB. Arrays  An array is a container object that holds a fixed number of values of a single type.  The length of an.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
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 ( 李德成 )
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
1 Unit-2 Arrays, Strings and Collections. 2 Arrays - Introduction An array is a group of contiguous or related data items that share a common name. Used.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
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.
Java Programming Language Lecture27- An Introduction.
CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.
Windows Programming Lecture 03. Pointers and Arrays.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
Data Types In Text: Chapter 6.
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
Chapter 6: Data Types Lectures # 10.
COMP205 IMPERATIVE LANGUAGES
Module 2 Arrays and strings – example programs.
C (and C++) Pointers April 4, 2019.
C Programming Lecture-8 Pointers and Memory Management
Presentation transcript:

1) Constrained (static) and unconstrained (dynamic) arrays. 2) Flexible arrays 3) Multi-dimensional arrays 4) Arrays of arrays 5) Lists, sets, bags,Etc. 6) Strings COMP205 IMPERATIVE LANGUAGES 6. COMPOUND (HIGHER LEVEL) DATA TYPES II --- MORE ON ARRAYS

CONSTRAINED AND UNCONSTRAINED ARRAYS A constrained array is an array where the index is specified (and hence the number of components is specified). We say that the bounds are static, hence constrained arrays are sometimes referred to as static arrays. Many imperative languages (including Ada but not C) also support some mechanism for declaring unconstrained arrays. In this case we say that the bounds are dynamic. Ada makes use of the symbol <> to indicate an unconstrained array: type LIST1 is array (INTEGER range <>) of FLOAT; L1 : LIST1(START..END);

DYNAMIC ARRAYS Although C does not support the concept of unconstrained arrays, however it does provide facilities to delay the declaration of an upper bound of an array till run time, i.e. the upper bound is declared dynamically hence such an array is referred to as a dynamic array. Two library functions malloc and free are used. The malloc( ) function obtains a block of memory (for the array) according to the parameter. (Note: The type of this parameter is system dependent, but is usually an int or an unsigned int ). The free function releases a block of memory.

int num = 4; void main(void) { int *numPtr = NULL; numPtr = (int *) malloc(sizeof(int)*num); /* Initialise. */ numPtr[0] = 2; numPtr[1] = 4; numPtr[2] = 6; numPtr[3] = 8; /* Output. */ printf("Array = %d, %d, %d, %d\(bsn", numPtr[0],numPtr[1], numPtr[2],numPtr[3]); /* End */ free(numPtr); } C DYNAMIC ARRAY EXAMPLE

OTHER TYPES OF ARRAY Apart from the standard array forms described earlier (static or constrained, and dynamic or unconstrained) we can identify a number of alternative forms of array which are a feature of particular imperative languages. These include: 1) Flexible arrays 2) Multi-dimensional arrays 3) Arrays of arrays 4) Lists

FLEXIBLE ARRAYS A powerful feature associated with some imperative languages is the ability to dynamically "resize" an array after it has been declared, i.e. during run-time. Such an array is referred to as a flexible array. Neither Ada or C support flexible arrays (Algol'68 does). A similar effect can be achieved in C using the built-in functions malloc and realloc. realloc extends or contracts the memory space available for a previously declared array.

int num = 4; void main(void) { int *numPtr = NULL; /* Allocate memory and initialise. */ numPtr = (int *) malloc(sizeof(int)*num); numPtr[0] = 2; numPtr[1] = 4; numPtr[2] = 6; numPtr[3] = 8; --- Ouput code --- /* Reallocate memory and reinitialise. */ num = 3; numPtr = (int *) realloc(numPtr,sizeof(int)*num); numPtr[0] = 1; numPtr[1] = 3; numPtr[2] = 5; --- Ouput code --- free(numPtr); } C FLEXIBLE ARRAY EXAMPLE

MULTI-DIMENSIONAL ARRAYS Multi-dimensional arrays are arrays where the elements have more than one index. In the case of two-dimensional arrays these can be thought of as comprising rows and columns, where the row number represents one index and the column number a second index. Columns Rows Two-dimensional arrays are therefore useful for storing tables of information.

with CS_IO ; use CS_IO ; procedure EXAMPLE is type TWO_D_ARRAY_T is array (1..3, 1..2) of integer; IA: TWO_D_ARRAY_T := ((1, 2), (3, 4), (5, 6)); begin put(IA(1,1)); put(IA(1,2));new_line; put(IA(2,1)); put(IA(2,2));new_line; put(IA(3,1)); put(IA(3,2));new_line; end EXAMPLE ; ADA 2-D ARRAY EXAMPLE Note that the "row" index is declared first, then the "column" index.

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

ARRAYS OF ARRAYS Some imperative languages (including Ada, but not C) support arrays of arrays. The distinction between arrays of arrays and multi- dimensional is that in the second case the result need not be “rectangular”. procedure ADA_EXAMPLE is type A is array (1..2) of integer; A1: A:= (1, 2); A2: A:= (3, 4); A3: A:= (5, 6); type A_OF_A is array (1..3) of A; IA: A_OF_A:= (A1, A2, A3); begin put(IA(1)(1)); put(IA(1)(2));new_line; put(IA(2)(1)); put(IA(2)(2));new_line; put(IA(3)(1)); put(IA(3)(2));new_line; end ADA_EXAMPLE ;

LISTS Lists (or sequences) can be considered to be special types of arrays but: –with an unknown number of elements, and –without the indexing capability. Lists are popular in logic and functional languages but not in imperative languages.

SETS A set is a group of (distinct) elements, all of the same type, which are all possible values of some other type referred to as the base type. The relationship is similar to that of an Ada sub-type to its “super-type”, e.g. positive integers to integers. The distinction between an array and a set is that the elements are not ordered (indexed) in anyway. The number of elements in a set is referred to as its cardinality. The only operations that can be performed on sets are “set operations”, e.g. member, union, intersection, etc. Neither Ada or C feature sets, however Pascal and Modula-2 do.

PASCAL SET EXAMPLE program SET_EXAMPLE (output); type SOMEBASE_T = 0..10; SOMESET_T = set of SOMEBASE_T; var SET1, SET2 : SOMESET_T; begin SET1 := [1, 2, 5]; SET2 := [6, 8, 9]; --- More code in here --- end.

BAGS (MULTISETS) Bags (multisets) are similar to sets except that they can contain an element more than once and record how many times a value has been inserted. The primary operations on bags are "insert value" and "remove value" (as opposed to the union, intersection, etc. operations found in sets). Very few imperative languages feature bags.

STRINGS A further type of array is the string. A string is a sequence of characters usually enclosed in double quotes. For this reason strings are sometimes referred to as a character arrays. As such we can use standard array operations on strings: –We can access any character in a string using an index. –Where supported (Ada) we can access slices of strings. In C the last member of the array must always be the null terminator '\0' (note single quotes).

STRING DECLARATIONS In C we declare a 15 character string, name, thus: char name[15]; i.e. as an array of characters. Ada provides the basic data type string : NAME: string(1.. 15); Note the similarity with array declarations.

OPERATIONS OM STRINGS Note: The C strcpy function does not require s1 and s2 to be of the same length (this is not the case when using the Ada := operator). S1 & S2 S1 = S2 S1 := S2 Concatenation Comparison Copy OPERATIONAda strcat(s1,s2) strcmp(s1,s2) strcpy(s1,s2) C

with CS_IO ; use CS_IO ; procedure EXAMPLE is NAME1: string(1..5):= "Henri"; NAME2: string(1..15); begin NAME2(1..6):= "E. Bal"; put("Name = "); put(NAME1); put(" "); put(NAME2(1..6)); new_line; put("Initials = "); put(NAME1(1)); put(". "); put(NAME2(1..4)); put("."); new_line; end EXAMPLE ; ADA STRING EXAMPLE Name = Henri E. Bal Initials = H. E. B.

#include void main(void) { char name1[] = "Dick"; char name2[15]; strcpy(name2,"Grune"); printf("Name = %s %s\n",name1,name2); printf("Initials = %c. %c.\n",name1[0],name2[0]); printf("Address of name1[0] = %d\n",&name1[0]); printf("name1 = %d\n",name1); } C STRING EXAMPLE Name = Dick Grune Initials = D. G. Address of name1[0] = name1 =

SUMMARY 1) Constrained (static) and unconstrained (dynamic) arrays. 2) Flexible arrays 3) Multi-dimensional arrays 4) Arrays of arrays 5) Lists, sets, bags, etc. 6) Strings