Slide: 1 Copyright © AdaCore Arrays Presented Quentin Ochem university.adacore.com.

Slides:



Advertisements
Similar presentations
Introduction to arrays
Advertisements

Slide: 1 Copyright © AdaCore Record Types Presented by Quentin Ochem university.adacore.com.
Slide: 1 Copyright © AdaCore Packages Presented by Quentin Ochem university.adacore.com.
Programming Languages and Paradigms
Arrays, A1 COMP 401, Fall 2014 Lecture 4 8/28/2014.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Chapter 7:: Data Types Programming Language Pragmatics
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Javascript Essentials How do I write it??  Start Homesite  Between the start and end BODY tags type: 
Chapter 10.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
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.
© Alan Burns and Andy Wellings, 2001 Programming in the Small n Aim: to remind you how to read/write programs in Ada 95, Java and ANSI C n Practicals will.
ISBN Chapter 6 Data Types: Structured types.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
Java Syntax Primitive data types Operators Control statements.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Chapter Eight: Arrays 1.Terms and what they mean 2.Types of arrays -One Dimensional arrays -Two Dimensional arrays -ragged arrays -Parallel arrays 3. Methods.
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏
Structured Data Types and Encapsulation Mechanisms to create new data types: –Structured data Homogeneous: arrays, lists, sets, Non-homogeneous: records.
JavaScript, Fourth Edition
JavaScript, Third Edition
© Alan Burns and Andy Wellings, 2001 Programming in the Small Aim: to remind you how to read/write programs in Ada 95, Java and ANSI C.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Structured Data Types. Date Types We have seen various data types –Integer, Real, Character, Logical All these types define data values of different kinds.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Slide: 1 Copyright © AdaCore Basic Types Presented by Quentin Ochem university.adacore.com.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Slide: 1 Copyright © AdaCore Subprograms Presented by Quentin Ochem university.adacore.com.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Programming Languages and Paradigms Imperative Programming.
ICAPRG301A Week 2 Strings and things Charles Babbage Developed the design for the first computer which he called a difference engine, though.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
Datatypes, Variables, Constants Flash Class. What Does ActionScript Do? Automates Examples: – Tells animation what to do button is clicked – Turn off.
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
Data Types Declarations Expressions Data storage C++ Basics.
Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com.
ISBN Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.
Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
REEM ALMOTIRI Information Technology Department Majmaah University.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
ITEC 320 Lecture 6 More on arrays / Strings. Arrays Review Scope Exceptions Arrays –Dynamic arrays How? Typed and anonymous.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Multiple variables can be created in one declaration
Arrays .
Topics Basic String Operations String Slicing
Topics Basic String Operations String Slicing
Topics Basic String Operations String Slicing
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Slide: 1 Copyright © AdaCore Arrays Presented Quentin Ochem university.adacore.com

Slide: 2 Copyright © AdaCore Ada Type Model Type Elementary Type AccessScalar Discrete EnumerationInteger Signed Modular Real Floating PointFixed Point Ordinary Decimal Composite Type Array Record Protected Task

Slide: 3 Copyright © AdaCore Arrays are First Class Citizens All arrays are (doubly) typed Arrays types properties are –The index type (can be any discrete type, with optional specific boundaries) –The component type (can be any definite type) Arrays objects properties are –The array type –Specific boundaries –Specific values type T is array (Integer range <>) of Integer; A : T (0.. 14); int * A = new int [15]; Ada C++

Slide: 4 Copyright © AdaCore Definite vs. Indefinite Types Definite types are types that can be used to create objects without additional information –Their size is known –Their constraints are known Indefinite types need additional constraint Array types can be definite or indefinite Components of array types must be definite type Definite is array (Integer range ) of Integer; type Indefinite is array (Integer range <>) of Integer; A1 : Definite; A2 : Indefinite (1.. 20);

Slide: 5 Copyright © AdaCore Array Indices Array indexes can be of any discrete type –Integer (signed or modular) –Enumeration Array indexes can be defined on any contiguous range Array index range may be empty Array indexes are computed at the point of array declaration type A1 is array (Integer range <>) of Integer; type A2 is array (Character range 'a'.. 'z') of Integer; type A3 is array (Integer range 1.. 0) of Integer; type A4 is array (Boolean) of Integer; X : Integer := 0; type A is array (Integer range 1.. X) of Integer; -- changes to X don't change A instances after this point

Slide: 6 Copyright © AdaCore Accessing Array Components Array components can be directly accessed Array types and array objects offer ‘Length, ‘Range, ‘First and ‘Last attributes On access, bounds are dynamically checked and raise Constraint_Error if out of range type A is array (Integer range <>) of Integer; V : A (1.. 10); begin V (1) := 0; type A is array (Integer range <>) of Float; V : A (1.. 10); begin V (0) := 0.0; -- NOK

Slide: 7 Copyright © AdaCore Array Copy Array operations are first class citizens In copy operations, lengths are checked, but not actual indices type T is array (Integer range <>) of Integer; A1 : T (1.. 10); A2 : T (1.. 10); begin A1 := A2; type T is array (Integer range <>) of Integer; A1 : T (1.. 10); A2 : T ( ); A3 : T (1.. 20); begin A1 := A2; -- OK A1 := A3; -- NOK

Slide: 8 Copyright © AdaCore Array Initialization Array copy can occur at initialization time If the array type is of an indefinite type, then an object of this type can deduce bounds from initialization type T is array (Integer range <>) of Integer; A1 : T (1.. 10); A2 : T ( ) := A1; type T is array (Integer range <>) of Integer; A1 : T (1.. 10); A2 : T := A1; -- A2 bounds are

Slide: 9 Copyright © AdaCore Array Slices It’s possible to consider only a part of the array using a slice –For array with only one dimension Slices can be used where an array object is required type T is array (Integer range <>) of Integer; A1 : T (1.. 10); A2 : T (1.. 20) := …; begin A1 := A2 (1.. 10); A1 (2.. 4) := A2 (5.. 7);

Slide: 10 Copyright © AdaCore Array Literals (Aggregates) Aggregates can be used to provide values to an array as a whole They can be used wherever an array value is expected Finite aggregate can initialize variable constraints, lower bound will be equal to T'First ({[ => ],} [others => ]) (1, 2, 3) -- finite positional aggregate (1 => 1, 2 => 10, 3 => 30) -- finite named aggregate (1, others => 0) -- indefinite positional aggregate (1 => 1, others => 0) -- indefinite named aggregate type T is array (Integer range <>) of Integer; V1 : T := (1, 2, 3); V2 : T := (others => 0); -- NOK (initialization) begin V1 := (others => 0); -- OK (assignment)

Slide: 11 Copyright © AdaCore Array Concatenation Two 1-dimensional arrays can be concatenated through the “&” operator –The resulting array lower bound is the lower bound of the left operand An array can be concatenated with a value type T is array (Integer range <>) of Integer; A1 : T := (1, 2, 3); A2 : T := (4, 5, 6); A3 : T := A1 & A2; type T is array (Integer range <>) of Integer; A1 : T := (1, 2, 3); A2 : T := A1 & 4 & 5;

Slide: 12 Copyright © AdaCore Array Equality Two 1-dimensional arrays are equal if –Their size is equal –Their components are equal one by one Actual indexes do not matter in array equality type T is array (Integer range <>) of Integer; A1 : T (1.. 10); A2 : T (1.. 20); begin if A1 = A2 then -- ALWAYS FALSE

Slide: 13 Copyright © AdaCore Loops over an Array Through an index loop Through an object loop type T is array (Integer range <>) of Integer; A : T (1.. 10); for I in A'Range loop A (I) := 0; end loop; type T is array (Integer range <>) of Integer; A : T (1.. 10); for V of A loop V := 0; end loop;

Slide: 14 Copyright © AdaCore Matrices Two dimensional arrays Attributes are 'First (dimension), 'Last (dimension), 'Range (dimension) Arrays of arrays type T is array (Integer range <>, Integer range <>) of Integer; V : T (1.. 10, 0.. 2); begin V (1, 0) := 0; type T1 is array (Integer range <>) of Integer; type T2 is array (Integer range <>) of T1 (0.. 2); V : T (1.. 10); begin V (1)(0) := 0;

Slide: 15 Copyright © AdaCore Strings Strings are regular arrays There is a special String literal The package ASCII provides names for Character values type String is array (Positive range <>) of Character; V : String := "This is it"; V2 : String := "Here come quotes ("")"; V : String := "This is nul terminated" & ASCII.NUL;

Slide: 16 Copyright © AdaCore Default Component Values By default, arrays do not come with default values It’s possible to force a default value initialization through a static expression (value known at compile-time) For performance reasons, the above may not be desirable type T is array (Integer range <>) of Integer; V : T (1.. 20); begin Put_Line (Integer'Image (V (1))); -- Displays an uninitialized value type T is array (Integer range <>) of Integer with Default_Component_Value => 0; V : T (1.. 20); begin Put_Line (Integer'Image (V (1))); -- Displays 0

Slide: 17 Copyright © AdaCore Quiz

Slide: 18 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (1/10) type My_Int is new Integer range ; type T is array (My_Int) of Integer; V : T; begin V (1) := 2;

Slide: 19 Copyright © AdaCore YES Is this correct? (1/10) type My_Int is new Integer range ; type T is array (My_Int) of Integer; V : T; begin V (1) := 2; Everything is OK

Slide: 20 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (2/10) type T is array (Integer) of Integer; V : T; begin V (1) := 2;

Slide: 21 Copyright © AdaCore NO Is this correct? (2/10) type T is array (Integer) of Integer; V : T; begin V (1) := 2; This can actually compile fine, but will fail at compilation due to the amount of data reserved on the stack

Slide: 22 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (3/10) type T1 is array (Integer range <>) of Integer; type T2 is array (Integer range <>) of Integer; V1 : T1 (1.. 3) := (others => 0); V2 : T2 := (1, 2, 3); begin V1 := V2;

Slide: 23 Copyright © AdaCore NO Is this correct? (3/10) type T1 is array (Integer range <>) of Integer; type T2 is array (Integer range <>) of Integer; V1 : T1 (1.. 3) := (others => 0); V2 : T2 := (1, 2, 3); begin V1 := V2; Compilation error, V1 and V2 are not of the same type

Slide: 24 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (4/10) type T is array (Integer range <>) of Integer; V : T := (1, 2, 3); begin V (0) := V (1) + V (2);

Slide: 25 Copyright © AdaCore NO Is this correct? (4/10) type T is array (Integer range <>) of Integer; V : T := (1, 2, 3); begin V (0) := V (1) + V (2); These three accesses are wrong, V is constrained between Integer’First.. Integer’First + 2

Slide: 26 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (5/10) type T is array (Integer range <>) of Integer; V1 : T (1.. 2); V2 : T ( ) := (others => 0); begin V1 := V2;

Slide: 27 Copyright © AdaCore YES Is this correct? (5/10) type T is array (Integer range <>) of Integer; V1 : T (1.. 2); V2 : T ( ) := (others => 0); begin V1 := V2; Everything is OK. The assignement will slide the values of V2 in the correct indexes.

Slide: 28 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (6/10) X : Integer := 10; type T is array (Integer range 1.. X) of Integer; V1 : T; begin X := 100; declare V2 : T; begin V1 := V2;

Slide: 29 Copyright © AdaCore YES Is this correct? (6/10) X : Integer := 10; type T is array (Integer range 1.. X) of Integer; V1 : T; begin X := 100; declare V2 : T; begin V1 := V2; No problem. Even if the value of X changes, the array declaration is not impacted V1 and V2 have the same boundaries

Slide: 30 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (7/10) type T is array (Integer range <>) of Integer; V1 : T (1.. 3) := (10, 20, 30); V2 : T := (10, 20, 30): begin for I in V1'Range loop V1 (I) := V1 (I) + V2 (I); end loop;

Slide: 31 Copyright © AdaCore NO Is this correct? (7/10) type T is array (Integer range <>) of Integer; V1 : T (1.. 3) := (10, 20, 30); V2 : T := (10, 20, 30): begin for I in V1'Range loop V1 (I) := V1 (I) + V2 (I); end loop; V2 (I) will raise an exception because the range is different from the one of V1

Slide: 32 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (8/10) type T is array (Integer range ) of Integer; V : T (2.. 9);

Slide: 33 Copyright © AdaCore NO Is this correct? (8/10) type T is array (Integer range ) of Integer; V : T (2.. 9); The boundaries of T are fixed, can’t be changed at object declaration

Slide: 34 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (9/10) type String_Array is array (Integer range <>) of String;

Slide: 35 Copyright © AdaCore NO Is this correct? (9/10) type String_Array is array (Integer range <>) of String; Array can only contain constrained objects

Slide: 36 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (10/10) X : Integer := 0; type T is array (Integer range <>) of Integer with Default_Component_Value => X; V : T (1.. 10);

Slide: 37 Copyright © AdaCore NO Is this correct? (10/10) X : Integer := 0; type T is array (Integer range <>) of Integer with Default_Component_Value => X; V : T (1.. 10); X is not a static expression (unknown at compile-time)

Slide: 38 Copyright © AdaCore university.adacore.com