COS 333: Chapter 6, Part 1

Willem van Heerden
23 Aug 202183:18

Summary

TLDRThis lecture delves into data types, focusing on their importance in programming. It begins with an overview of data types, moving on to discuss primitive data types like integers, floating points, and characters. The lecture then covers user-defined types, including ordinal types with enumerations, and concludes with an exploration of array data types. It touches on the design issues related to data types, such as operations defined for a type and syntactic mechanisms for specification, providing a comprehensive foundation for understanding data structures in programming.

Takeaways

  • πŸ“š The lecture delves into various data types, starting with an introduction and covering primitive data types, character strings, user-defined ordinal data types, and array data types.
  • πŸ”’ Data types define a range of values, memory storage methods, and a set of predefined operations for data objects, emphasizing the importance of type in programming languages.
  • πŸ’Ύ A data type's memory representation can vary, such as using two's complement for integers or IEEE standards for floating points, highlighting the binary encoding of data objects.
  • πŸ”‘ Character strings are sequences of characters and can be implemented as primitive types or as arrays of characters, with operations like concatenation and substring referencing.
  • πŸ”‘ User-defined ordinal types, such as enumerations, map a set of named constants to integer values, providing readability and type safety in programming.
  • πŸ“Š Arrays are aggregate data types that can be static, fixed stack dynamic, stack dynamic, fixed heap dynamic, or heap dynamic, each with different allocation and binding properties.
  • πŸ”„ The dynamic nature of data types, especially arrays, allows for flexibility in programming, but also introduces complexity in memory management and type safety.
  • πŸ‘ The advantages of certain data types, like boolean for readability and integer for direct hardware mapping, are discussed, along with their impact on programming language design.
  • πŸ‘Ž The disadvantages, such as memory inefficiency in decimal types or limited range in fixed-length arrays, are also considered in the context of programming language evaluation.
  • πŸ” The lecture examines different programming languages' support for data types, such as Java's support for primitive data types and Python's support for complex numbers.
  • πŸ”— The importance of design issues in data types, like operation definitions and syntactic mechanisms for specifying data types, is emphasized for programming language development.

Q & A

  • What are the three important aspects defined by a data type in relation to data objects?

    -A data type defines a collection of data objects with a range of values, how the data objects are stored in memory, and a set of predefined operations on these data objects.

  • How does a data type's range of values differ between integers and floating-point values?

    -For integers, the range of values is defined by a minimum and maximum whole number that can be represented without a fractional portion. For floating-point values, the range is defined by the minimum and maximum values allowable for the type, along with the precision associated with the type.

  • What is the significance of the IEEE standard in floating-point representation?

    -The IEEE standard floating-point representation is significant as it provides a consistent and widely used method for encoding floating-point values on a binary level, ensuring compatibility and accuracy across different systems and platforms.

  • Why might a programming language provide support for unsigned integer types?

    -Unsigned integer types are provided to allow for a greater range of positive integer values, effectively doubling the range of numbers that can be represented with the same amount of memory, compared to signed integers.

  • What is the primary advantage of using a decimal type over a floating-point type for representing monetary values?

    -The primary advantage of using a decimal type is accuracy, as it stores data using a fixed number of decimal digits, providing an exact representation without the approximations inherent in floating-point types.

  • What is the main drawback of using a character encoding scheme like ASCII?

    -The main drawback of using ASCII is that it only represents a limited set of characters, primarily English letters and some symbols, and does not support the wide range of characters needed for other languages.

  • Why is the support for character strings considered important in programming languages?

    -Support for character strings is important because it enhances readability and writability, allowing programmers to work with sequences of characters in a more intuitive and efficient manner, which is especially useful in applications that involve text manipulation.

  • What are the two main design issues to consider when implementing support for character strings in a programming language?

    -The two main design issues are whether character strings should be a primitive type or a special kind of array, and whether the length of the string should be static (fixed) or dynamic (able to grow and shrink during program execution).

  • What is an enumeration type and how does it differ from a primitive ordinal type?

    -An enumeration type is a user-defined data type with a set of named constants as its possible values. It differs from a primitive ordinal type, which is built into the programming language and has values that can be mapped to a set of positive integers.

  • Why are heap dynamic arrays considered more flexible than other types of arrays?

    -Heap dynamic arrays are considered more flexible because they allow the array to grow and shrink during program execution, with the memory allocated from the heap rather than the stack, which means the array's size can change as needed without being constrained by the scope of the variable.

Outlines

00:00

πŸ“š Data Types and Their Fundamentals

This paragraph introduces the concept of data types, delving into the specifics of what they define and how they are represented in memory. It explains the three key aspects of data types: the range of values they encompass, their bit-based memory representation, and the predefined operations applicable to them. The paragraph also touches on the importance of data types in programming and the distinction between data objects and objects in object-oriented programming.

05:01

πŸ”’ Delving into Primitive Data Types

The focus shifts to primitive data types, which are foundational to most programming languages and often directlyζ˜ ε°„ to hardware-level representations. The paragraph discusses various primitive data types, including integer types with signed and unsigned variants, floating-point types that approximate real numbers, and the IEEE floating-point standard 754. It also covers the representation of complex numbers and the unique aspects of decimal and boolean data types, emphasizing their storage and operation efficiencies or inefficiencies.

10:04

πŸ†Ž Character Data Types and Encoding Schemes

This paragraph explores character data types, the necessity of numeric coding schemes for character representation, and the evolution of encoding standards from ASCII to Unicode. It discusses the limitations of ASCII and how Unicode, with its various standards like UCS2, UCS4, and UTF, addresses the need for representing a broader range of characters from different languages. The paragraph also examines character strings, their potential as primitive or array-based data types, and the implications of static versus dynamic string lengths.

15:05

πŸ” Operations and Design Considerations for Character Strings

The paragraph delves into the operations that can be performed on character strings, such as assignment, copying, comparison, concatenation, and pattern matching, often facilitated by regular expressions. It also discusses design considerations for character string types in programming languages, including whether strings should be primitive, the dynamics of string length, and the decision-making process for supported operations.

20:07

πŸ“ User-Defined Ordinal Types and Enumerations

The paragraph introduces user-defined ordinal types, contrasting them with built-in primitive ordinal types like int, char, and boolean in Java. It provides an in-depth look at enumeration types, explaining how they are defined, their use of named constants, and the design issues surrounding their implementation in programming languages, such as coercion to and from integer values and the allowance of enumeration constants across multiple types.

25:08

🏷️ Advantages and Evaluation of Enumeration Types

This paragraph evaluates the benefits of enumeration types, such as increased readability and writeability, and their role in enhancing the reliability of programming languages by preventing invalid operations and values. It also discusses the varying levels of support for enumeration types across different programming languages, highlighting those that prevent coercion to integers, thus avoiding potential errors.

30:09

πŸ” An In-Depth Look at Array Data Types

The paragraph provides a comprehensive overview of array data types, discussing the design considerations for their implementation in programming languages. It covers the legality of different types for subscripts, the importance of range checking for subscript expressions, and the timing of subscript range and storage binding. The paragraph also touches on the maximum number of subscripts allowed, the initialization of arrays, and support for array slices.

35:10

πŸ“Š Array Subscripting and Storage Binding Categories

This paragraph categorizes arrays based on their subscript binding and storage binding characteristics, explaining the differences between static arrays, fixed stack dynamic arrays, stack dynamic arrays, fixed heap dynamic arrays, and heap dynamic arrays. It discusses the advantages and disadvantages of each category, such as efficiency, space utilization, flexibility, and the potential for dynamic resizing.

40:10

πŸ› οΈ Modern Programming Languages and Array Support

The paragraph examines how modern programming languages, including C, C++, Java, C#, and scripting languages like Perl, JavaScript, Python, and Ruby, support different array categories. It highlights the specific features and limitations of each language in terms of array management, such as the use of 'new' and 'delete' in C++, garbage collection in Java, and the ArrayList class in C#.

45:13

πŸ“š Array Initialization Techniques

The final paragraph discusses array initialization, showcasing how different programming languages, such as C, C++, Java, C#, and Ada, provide structures to initialize arrays at the time of allocation. It illustrates various initialization techniques, including the use of braces to define initial values in C-like languages, Ada's elaborate support for array initialization with fine-grained control, and Python's list comprehensions.

Mindmap

Keywords

πŸ’‘Data Type

A data type defines a set of values and a set of operations on those values. In the context of the video, data types are crucial as they determine how data objects are stored in memory, the range of values they can take, and the operations that can be performed on them. For instance, the video discusses integer, floating point, and character data types, each with specific storage mechanisms and permissible operations.

πŸ’‘Variable

A variable is a storage location in a program that can hold a value of a specific data type. The video emphasizes that variables have attributes, one of which is their data type, determining what kind of values the variable can store, such as integers, characters, or floating-point numbers.

πŸ’‘Primitive Data Types

Primitive data types are the most basic data types provided by a programming language and are often a direct reflection of the hardware level. The script mentions that they include integers, floating points, and characters, which are used to represent whole numbers, real numbers, and textual data, respectively.

πŸ’‘Character Encoding

Character encoding is the way characters are represented in memory using a numeric code. The video discusses ASCII and Unicode as examples of encoding schemes. ASCII is limited to 256 characters and is used mainly for English, while Unicode, with its UTF standards, can represent a vast array of characters from different languages.

πŸ’‘Array Data Types

Array data types are used to store multiple items of the same type in a single variable. The video script explains that arrays can be primitive or user-defined and discusses various aspects of array design, such as whether the array's length is fixed or dynamic, and the operations that can be performed on arrays.

πŸ’‘IEEE Floating Point Standard

The IEEE floating point standard, or IEEE 754, is a widely used standard for representing floating point numbers in computer systems. The video explains that this standard defines how floating point numbers are encoded in memory, using specific bit patterns for the sign, exponent, and fraction parts of the number.

πŸ’‘Concatenation

Concatenation is the operation of joining two sequences (such as strings) end-to-end. The video script mentions that concatenation is a defined operation for string data types, allowing programmers to merge two strings into one, which is not possible with other data types like integers.

πŸ’‘Descriptor

In the context of the video, a descriptor refers to a collection of attributes associated with a variable. It includes details about the variable's data type, storage, and other properties, providing a comprehensive definition of the variable's characteristics.

πŸ’‘User-Defined Ordinal Data Types

User-defined ordinal data types are created by programmers to represent a series of values that can be ordered or ranked. The video script discusses enumeration types as an example, where named constants are used to represent a set of predefined values, such as days of the week or colors.

πŸ’‘Static and Dynamic Length Strings

Static length strings have a fixed size determined at compile time, while dynamic length strings can grow and shrink during program execution. The video script explains that the choice between static and dynamic strings depends on the programming language and the specific requirements of the application, with each having its advantages and disadvantages.

πŸ’‘Pattern Matching

Pattern matching is a technique used to determine if a string matches a specified pattern. The video script mentions regular expressions as the modern standard for pattern matching in many programming languages, allowing for complex string manipulations and searches.

Highlights

Introduction to data types and their importance in defining a collection of data objects, storage in memory, and operations on these objects.

Explanation of how data types specify a range of values, such as the minimum and maximum for integers, and precision for floating points.

Discussion on the bit-based representation of data types, including encoding schemes like ASCII and IEEE floating-point standards.

Clarification of the difference between data objects and objects in object-oriented programming, emphasizing the programmer-defined abstract data types.

Introduction to primitive data types, which are not defined in terms of other data types and often reflect hardware-level representations.

Analysis of integer data types, their various forms in different programming languages, and the concept of signed and unsigned integers.

Overview of floating point types, their use of scientific notation in binary, and the IEEE floating point standard 754.

Complex type explanation, its limited support in programming languages, and its unique representation involving two floating point values.

Decimal type discussion, its use in business applications for monetary values, and the difference from floating point types.

Boolean data type exploration, its simplicity with only two values, and its implementation using bytes instead of bits.

Character data type examination, the necessity of numeric coding schemes like ASCII and Unicode for character representation.

Character strings as sequences of characters, the design issues of whether they should be primitive or arrays, and their operations.

Enumeration types, their definition by programmers, and the use of named constants to represent a series of values.

User-defined ordinal types, the concept of mapping values to positive integers, and the use of enumerations in programming languages.

Array data types, their role as collections of homogeneous data elements, and the various design issues related to their implementation.

Array initialization techniques, the different ways programming languages allow for setting initial values of arrays.

Conclusion of the lecture with a summary of the discussed data types and a preview of upcoming topics in the next lecture.

Transcripts

play00:01

in the previous chapter we discussed

play00:03

names bindings and scopes

play00:06

part of this discussion dealt with

play00:07

variables where we looked at a number of

play00:11

attributes that are bound to each

play00:13

variable and one of these attributes is

play00:16

the type of the variable

play00:18

in this chapter we will be looking at

play00:21

these data types in more detail

play00:26

these are the topics that we'll be

play00:27

discussing in today's lecture we'll

play00:30

begin with a quick introduction into

play00:32

data types after which we'll move on to

play00:35

primitive data types we'll then take a

play00:38

look at character string types

play00:41

user-defined ordinal data types and then

play00:44

finally array data types

play00:49

now we've already discussed the concept

play00:51

of a data type in the previous chapter

play00:55

to reiterate what we spoke about there

play00:57

we said that a data type defines three

play01:00

important things in relation to data

play01:04

objects

play01:05

so firstly a data type defines a

play01:07

collection of data objects with a range

play01:10

of values

play01:12

so for example if we are talking about

play01:14

an integer data type and then we specify

play01:17

that integral whole values can be

play01:19

represented without a fractional portion

play01:22

and we also have then a particular

play01:25

minimum and maximum value that defines

play01:28

the range of values within which those

play01:31

integer values then can fall

play01:34

in a similar fashion if we have floating

play01:36

point values we also have a minimum and

play01:39

maximum allowable value for that

play01:41

floating point data type and we also

play01:44

have a precision associated with the

play01:47

type

play01:49

then secondly a data type also defines

play01:52

how the data objects are stored in

play01:55

memory so what we are talking about here

play01:57

is a bit based representation

play02:00

in other words

play02:01

what kind of representational scheme are

play02:04

we using to encode these values on a

play02:07

binary level

play02:09

so for example we may have unsigned

play02:12

integer values or we might for example

play02:15

use a two's complement representation or

play02:17

a sign and magnitude representation if

play02:20

we are representing floating point

play02:22

values we would be using the ieee

play02:25

standard floating point representation

play02:28

if we are representing characters we may

play02:31

be using an ascii encoding or a unicode

play02:34

encoding all of these are mechanisms for

play02:38

representing these data objects within

play02:41

memory on a binary level

play02:44

and then in the third place a data type

play02:47

specifies a set of predefined operations

play02:50

on these data objects so for example

play02:53

integer values can be added to one

play02:55

another or subtracted from one another

play02:58

they can also be multiplied or divided

play03:02

however for example a concatenation

play03:04

operation is not defined for an integer

play03:08

value

play03:09

on the other hand if we're talking about

play03:10

strings concatenation or substring

play03:13

operations may be defined but for

play03:15

example it isn't a defined operation to

play03:19

allow two strings to be multiplied by

play03:21

one another for instance

play03:25

now in this context it's important to

play03:26

understand that when we're talking about

play03:28

a data object we're not talking about an

play03:31

object in the sense of object-oriented

play03:34

programming

play03:35

instead we're talking about objects

play03:38

which represent integers of a programmer

play03:41

defined abstract data type so in other

play03:44

words what we are specifying then here

play03:48

is a representation of a data type but

play03:52

which is very importantly specified by a

play03:56

programmer

play03:57

now in addition to this we also have the

play03:59

concept of a descriptor and this is

play04:02

simply the collection of the attributes

play04:05

associated with a variable as i

play04:08

previously mentioned we discussed all of

play04:10

these attributes in the previous chapter

play04:13

and i won't go through them in any

play04:15

further detail at this point

play04:18

so there are two main design issues then

play04:20

that arise for all data types

play04:23

firstly which operations are defined for

play04:26

this particular data type that we are

play04:28

currently considering

play04:30

and secondly how are data types

play04:33

specified what kind of syntactic

play04:35

mechanism do we use to specify

play04:39

a data type

play04:43

so now we'll move on to primitive data

play04:45

types

play04:46

now almost all programming languages

play04:48

provide a set of what we refer to as

play04:51

primitive data types and the primitive

play04:54

data type is simply a data type that is

play04:56

not defined in terms of other data types

play05:00

now a large number of these primitive

play05:03

data types are simply reflections of

play05:06

what is actually happening on a hardware

play05:08

level as we'll see in a moment

play05:10

but some other primitive data types are

play05:13

not direct representations of how these

play05:16

values are represented on a hardware

play05:18

level however they require only a little

play05:21

non-hardware support for their actual

play05:24

implementation

play05:28

the first primitive data type that we'll

play05:30

look at is the integer data type which

play05:32

you should be very very familiar with by

play05:35

now

play05:36

so integer data types almost always an

play05:39

exact reflection of what's happening on

play05:41

a hardware level and therefore the

play05:43

mapping between the type in the

play05:45

high-level programming language and what

play05:47

is actually being represented is fairly

play05:50

trivial

play05:51

now there may be as many as eight

play05:53

different integer types so for example

play05:55

if we look at java then java has only

play05:59

signed representations of its integer

play06:02

types

play06:03

and there are four integer types namely

play06:06

byte which is the smallest

play06:08

then short

play06:09

int and long which is a long integer

play06:12

representation which is the largest on a

play06:15

bit level

play06:17

so this of course then means that the

play06:19

range of values that we can represent in

play06:21

a byte is much smaller than the range of

play06:24

values that we can represent in a short

play06:27

int or long

play06:29

now some languages also then have

play06:32

unsigned integer values so for example

play06:35

if we look at c and c plus plus then

play06:38

there are unsigned versions of bytes

play06:41

short integers integers and long

play06:44

integers so what i would like you to do

play06:46

at this point is pause the video and try

play06:49

to answer what the advantage is of

play06:53

providing support for unsigned integer

play06:56

types

play07:00

next we have floating point types which

play07:03

are also primitive data types and these

play07:06

types model real numbers but only as

play07:09

approximations

play07:11

and so they use a kind of a scientific

play07:14

notation but on a binary level in order

play07:17

to perform this kind of representation

play07:22

now

play07:22

languages that are intended for

play07:25

scientific use and this also then

play07:28

extends to multi-purpose programming

play07:30

languages that can also be used for

play07:32

scientific purposes such as cnc plus

play07:35

plus

play07:36

will provide at least two floating point

play07:39

data types

play07:41

so for example in the c based languages

play07:44

we have the float and double types

play07:47

float represents a single precision

play07:50

floating point value whereas double

play07:52

represents a double precision floating

play07:55

point type

play07:57

now there are sometimes more floating

play07:59

point data types that are supported in

play08:02

certain programming languages

play08:04

so for example there might be a long

play08:07

double type that may be supported in a

play08:10

particular programming language

play08:12

some implementations of cnc plus plus do

play08:16

provide support for a long double type

play08:18

however this is exactly the same as a

play08:21

regular double type so it all depends on

play08:24

the specific programming language and

play08:26

the platform that the programming

play08:28

language is intended to compile code for

play08:32

now usually floating point data types

play08:35

are exact representations of the

play08:38

hardware counterparts on a binary level

play08:41

but this isn't always necessarily the

play08:43

case

play08:44

now there are different ways of

play08:46

representing floating point values but a

play08:48

very commonly used approach

play08:51

today is the ieee floating point

play08:53

standard 754

play08:56

which is very commonly used

play08:58

so on the bottom right of the slide we

play09:01

see a representation of a single

play09:03

precision floating point value at the

play09:05

top and at the bottom a double precision

play09:07

floating point value we can see each of

play09:10

these representations consists of a

play09:13

number of bits where these bits are

play09:15

subdivided into a single sine bit in

play09:18

each case

play09:19

and then a number of bits for the

play09:21

exponent in the case of our single

play09:24

precision floating point value we have

play09:25

eight bits for the exponent for double

play09:28

precision representation we have 11 bits

play09:31

for our exponent and then finally the

play09:34

fractional portion which consists of 23

play09:37

bits in the case of the single precision

play09:40

floating point value and 52 bits in the

play09:43

case of the double precision floating

play09:45

point value

play09:48

so

play09:49

we see then that there are two

play09:52

attributes associated with floating

play09:54

point values and these relate to the bit

play09:57

based representation of these values

play10:00

firstly we have precision and this is

play10:04

then the accuracy of the numbers

play10:07

fractional part so in other words

play10:10

to how many digits after the

play10:13

decimal point um can we accurately

play10:16

represent a floating point value

play10:20

and this then directly

play10:22

relates to the number of bits in the

play10:25

fraction portion of the representation

play10:29

then we also have the range which

play10:30

defines the minimum and the maximum

play10:33

values that can be represented using a

play10:36

particular number of bits within a

play10:39

floating point representation

play10:41

now this is defined by both the number

play10:44

of bits in the exponent part of the

play10:46

representation as well as the number of

play10:49

bits in the fraction part of the

play10:51

representation but most important of

play10:54

these is the number of bits in the

play10:56

exponent part of the representation so

play10:59

the more bits we have available for the

play11:01

exponent the larger the range is that

play11:04

can be represented

play11:09

the next primitive data type that we'll

play11:11

look at is the complex type which is

play11:14

used to represent complex numbers

play11:17

now the complex type is not very widely

play11:20

supported there are only a small handful

play11:22

of programming languages that support

play11:24

complex types the most notable of which

play11:27

is fortran however python also supports

play11:30

the complex type

play11:32

so what i would like you to do at this

play11:34

point is to pause the video and try to

play11:36

explain why it makes sense for the

play11:39

fortran programming language to support

play11:42

the complex type

play11:46

now complex types are a little bit more

play11:49

complicated in terms of their

play11:50

representation than integers and floats

play11:54

they consist of two

play11:56

floating point values however they are

play11:58

treated as a single unit and this is why

play12:00

complex types are still considered

play12:03

primitive data types and not a compound

play12:06

type such as the user-defined record

play12:09

data type which we'll speak about later

play12:11

on in this chapter

play12:14

so the first floating point part of a

play12:16

complex type is the real part and the

play12:20

second part is the imaginary part now

play12:24

the literal form for a complex value

play12:28

needs to represent both the real and the

play12:30

imaginary part

play12:32

so in python we would use the notation

play12:35

seven plus three j in parentheses and

play12:39

this then represents a complex value in

play12:43

its literal form the literal form being

play12:46

the actual representation of a value

play12:49

using this type so in the same way that

play12:52

7 is a literal representation of an

play12:55

integer and 3.48 is a literal

play12:58

representation of a floating point value

play13:01

7 plus 3j is the literal representation

play13:05

of a complex value

play13:07

so in this case the 7 then is the real

play13:10

part and the 3 is the imaginary part

play13:16

the next primitive data type we'll look

play13:19

at is the decimal type now it's very

play13:21

important that you don't confuse decimal

play13:24

types and floating point types which we

play13:27

discussed previously both are used to

play13:30

represent real values however they do so

play13:34

using a different kind of storage format

play13:38

now decimal types are used in the

play13:41

context of business applications

play13:43

particularly when they are required to

play13:46

model monetary values

play13:49

so they are essential and very much at

play13:51

the core of the cobol programming

play13:53

language but c sharp also offers support

play13:56

for a decimal data type

play13:59

so what i would like you to do at this

play14:00

point is to pause the video and try to

play14:03

explain why cobol provides support for a

play14:06

decimal data type and why the concept of

play14:09

a decimal type is so central to cobol

play14:13

also related to this try to answer why c

play14:16

sharp

play14:18

would provide support for a decimal data

play14:20

type and why this makes sense in the

play14:22

context of c sharp and what c sharp is

play14:25

intended for as a programming language

play14:29

all right so instead of using an

play14:31

approximate representation the way that

play14:34

floating point values do

play14:36

a decimal primitive data type stores

play14:39

data using a fixed number of decimal

play14:43

digits

play14:44

so each digit is then represented

play14:47

separately using a coded representation

play14:50

which is referred to as a binary coded

play14:52

decimal representation or alternatively

play14:55

a bcd representation what this means

play14:58

then is that each digit of a decimal

play15:02

number is represented separately in

play15:05

other words each digit is encoded as a

play15:08

separate number

play15:10

unto itself what this means then is we

play15:13

have a very accurate representation

play15:15

because each individual digit is

play15:18

represented explicitly we're not working

play15:21

with an approximate representation the

play15:23

way that we were with floating point

play15:25

values and this is the primary advantage

play15:28

associated with decimal types

play15:31

so what i would like you to do at this

play15:32

point is to pause the video and try to

play15:35

answer why this accuracy is important in

play15:39

the context of business applications

play15:42

particularly when we are working with

play15:44

monetary values

play15:48

so the primary advantage then associated

play15:51

with decimal values is that they are

play15:53

very accurate however there are two

play15:55

major disadvantages associated with

play15:57

decimal types

play15:59

the first is that we waste a lot of

play16:02

memory and the reason for this is that

play16:06

the bcd representation that we use is

play16:08

not very compact so we see

play16:11

that with floating point values we have

play16:13

a very compact notation we use a

play16:16

scientific notation like representation

play16:19

of course represented on a binary level

play16:23

and this is a very efficient

play16:25

representation scheme we have a compact

play16:28

representation in terms of the number of

play16:30

bits that are used in memory to

play16:32

represent our floating point values but

play16:34

we also have

play16:36

operations that are defined for these

play16:39

floating point values which have been

play16:41

optimized and can be performed very

play16:43

efficiently

play16:45

so we also then as a disadvantage

play16:47

associated with decimal values have a

play16:50

fairly limited range and the reason for

play16:52

this

play16:53

is that we have a limited number of

play16:56

decimal digits that are represented now

play16:59

of course we can increase the range by

play17:01

adding more decimal digits however this

play17:04

will then require a larger bit based

play17:07

representation within memory so we are

play17:10

always going to run into a problem where

play17:13

we want to represent very large values

play17:16

either negative or the positive

play17:17

direction then we are going to require a

play17:20

lot more memory and therefore our range

play17:23

is limited in one respect or another

play17:27

another disadvantage associated with

play17:30

decimal types which is not explicitly

play17:32

mentioned in the textbook but

play17:34

is also a concern to keep in mind

play17:37

is that operations for decimal values

play17:41

are not as efficient as operations for

play17:44

floating point values and the reason for

play17:47

this is that these operations typically

play17:49

need to be simulated in software because

play17:52

they are not directly supported on a

play17:55

hardware level typically with floating

play17:57

point values operations such as addition

play18:00

subtraction multiplication and division

play18:03

are actually implemented on a chip level

play18:06

and therefore are very efficient but

play18:08

this is not the case when we are talking

play18:10

about decimal representations and

play18:13

therefore these calculations are a lot

play18:15

less efficient when it comes to decimal

play18:17

values

play18:20

the next primitive data type we'll look

play18:23

at is the boolean data type which you

play18:25

should also be very familiar with the

play18:28

boolean data type is the simplest data

play18:30

type of all

play18:32

and it has a range of values that

play18:34

consists of only two elements one

play18:36

element representing a true value and

play18:39

the other representing a false value

play18:43

now it would be possible to implement

play18:46

billion data types using single bits

play18:49

however in most modern computers these

play18:53

days addressing limitations don't allow

play18:55

individual bits to be addressed and

play18:58

therefore we cannot retrieve individual

play19:01

bits one at a time so as a result of

play19:04

this usually a full byte consisting of

play19:07

eight bits is used in order to represent

play19:10

a boolean value

play19:11

so what i would like you to do at this

play19:13

point is to pause the video and try to

play19:15

think of a disadvantage of this kind of

play19:19

representation

play19:22

so then if we look at the primary

play19:26

advantage of a boolean primitive data

play19:29

type

play19:30

this is readability so what i would like

play19:33

you to do at this point again is to

play19:35

pause the video and try to explain why

play19:37

boolean primitive data types contribute

play19:40

to the readability of a programming

play19:43

language that supports them

play19:48

the last of the primitive data types

play19:50

we'll look at is the character data type

play19:53

now of course characters can't be

play19:55

directly represented in memory

play19:57

and therefore a numeric coding scheme is

play20:00

required to represent any character data

play20:04

what this means is that on a binary

play20:06

level a sequence of bits will represent

play20:09

each individual character each sequence

play20:11

of bits translates to a number and this

play20:15

number is a numeric code that maps to a

play20:17

specific character

play20:19

now there are a wide variety of

play20:21

different encoding schemes that can be

play20:23

used in order to represent characters

play20:26

in general the more bits that are used

play20:28

for an encoding the larger the range of

play20:32

numeric encoding values there are which

play20:35

means we have then a larger set of

play20:38

characters that we can represent

play20:41

now the most commonly used encoding

play20:43

scheme is the ascii encoding scheme

play20:46

which you should be familiar with ascii

play20:48

stands for american standard code for

play20:51

information interchange and here a

play20:53

single byte is used for each of the

play20:57

numeric codings what this means is we

play21:00

can then have 256

play21:03

separate numeric codings which means we

play21:06

can represent

play21:07

256 secret characters now the major

play21:11

drawback associated with the asking

play21:13

coding scheme is that it only represents

play21:17

roman characters so it's only really

play21:19

useful for representing english

play21:23

what this then has led to is other

play21:26

encoding schemes which use a larger

play21:28

number of bits to represent each of the

play21:31

individual characters allowing for a

play21:34

much larger set of characters which can

play21:36

then be used to represent non-english

play21:39

alphabetical characters

play21:43

so alternatives then generally fall into

play21:46

the unicode category

play21:49

and here we have a 16 bit or two byte

play21:53

encoding scheme referred to as

play21:55

ucs2

play21:57

so this allows for the characters from

play21:59

most of the natural languages that exist

play22:01

in the world to be represented and this

play22:04

was originally supported in the java

play22:07

programming language c-sharp and

play22:09

javascript however also support unicode

play22:12

and most modern programming languages

play22:15

have been extended or designed from the

play22:18

ground up to support the unicode

play22:21

now there is a larger unicode standard

play22:24

which uses 32 bits to represent each of

play22:27

the individual character encodings in

play22:30

other words four bytes and this is

play22:32

referred to as

play22:34

ucs4 so this was originally supported by

play22:37

fortran starting with the 2003

play22:41

update

play22:42

now the ucs standards have largely been

play22:46

superseded by the utf standard ucs

play22:50

stands for universal coded character set

play22:53

and utf stands for unicode

play22:55

transformation format

play22:58

so these days utf is more commonly used

play23:01

and the equivalent of ucs2 in utf format

play23:05

is

play23:06

utf-16 whereas the equivalent of ucs4 is

play23:11

utf-32

play23:14

so

play23:15

java and c-sharp use the newer utf-32

play23:19

standard in order to represent their

play23:21

characters

play23:25

so this brings us rather neatly to the

play23:27

next data type we'll look at namely

play23:30

character strings

play23:32

so character strings are data types

play23:34

where the values are simple sequences of

play23:36

characters where one character follows

play23:39

on from another in a linear sequence

play23:43

now it is possible for a character

play23:45

string to be primitive in nature in

play23:47

which case each character string is

play23:50

handled as a single self-contained

play23:52

entity however it's also possible for a

play23:55

character string to not be primitive in

play23:58

nature in which case the character

play24:00

string is usually represented as an

play24:03

array of characters where each character

play24:06

is then a primitive type

play24:08

now there are two main design issues

play24:10

that need to be decided on when

play24:13

designing a character string type in a

play24:16

programming language firstly will

play24:19

character strings be a primitive type or

play24:22

just a special kind of array that stores

play24:25

characters

play24:26

and secondly should the length of the

play24:29

string be static in other words fixed

play24:32

once after compile time or should it be

play24:36

dynamic in nature in other words the

play24:38

length of the string can grow and shrink

play24:41

over the course of program execution

play24:44

time

play24:46

now of course as we've seen before a

play24:50

type also specifies the operations that

play24:53

are valid for objects of a particular

play24:55

type

play24:56

so when we are talking about character

play24:58

strings there are usually a variety of

play25:00

operations that might be valid for

play25:02

character strings and during the design

play25:05

of a character string type one must

play25:08

decide on the operations that will be

play25:11

supported by the programming language

play25:13

for these character strings

play25:16

so typical operations are firstly

play25:18

assignment and copying these are two

play25:21

different operations so let's assume for

play25:24

example that we have two strings a and b

play25:28

and we then perform an assignment a

play25:31

equals b

play25:33

now if we are talking about an

play25:35

assignment then very often what happens

play25:37

is we are simply performing a reference

play25:40

assignment so in other words if we have

play25:43

a equals b this means that a will then

play25:46

refer to the string b

play25:48

and if we change b then a will also

play25:52

refer to the changed string and vice

play25:55

versa

play25:56

a copying operation is actually then a

play25:59

literal copying of the content of one

play26:01

string into another in which case then

play26:04

each of the two copies are completely

play26:07

independent from each other so if we

play26:09

have two strings a and b and we perform

play26:11

the assignment a equals b then if we

play26:14

perform a copying operation then the

play26:16

content of b will be copied into the

play26:19

string a if we modify b then a will not

play26:23

be modified and vice versa

play26:26

we also typically have comparison

play26:28

operations so we have an equality

play26:31

operation usually which tests to see

play26:34

where the two character strings are

play26:36

equivalent to each other in other words

play26:38

they contain exactly the same characters

play26:41

we may also have inequality operations

play26:44

such as greater than and of course here

play26:46

we need to decide on the semantics of

play26:49

this kind of operation

play26:51

usually this involves a comparison of

play26:54

characters on a numeric level

play26:58

where we then actually compare the

play27:00

encoded numeric values of each of the

play27:03

individual characters to each other but

play27:05

again this is something that needs to be

play27:07

decided during language design time

play27:10

the incarceration or concatenation is an

play27:13

operation where two character strings

play27:15

are added to one another where one

play27:18

string is then added to the end of the

play27:21

other string

play27:23

of course here the semantics must also

play27:25

be decided upon so is one of the strings

play27:28

modified by means of the concatenation

play27:30

operation or do we generate a new string

play27:34

which is the result of the concatenation

play27:37

then we very often also provide

play27:40

support for substring referencing in

play27:43

high-level programming languages

play27:45

so here we are referring to a

play27:47

sub-portion of an existing larger

play27:50

character string again we need to decide

play27:53

how this referencing takes place are we

play27:56

simply referring to a character within

play27:59

an existing string or is there a

play28:02

mechanism for referring to a specific

play28:04

sub-portion of the string

play28:07

and then finally pattern matching is

play28:08

supported by a number of modern

play28:11

programming languages particularly

play28:13

scripting languages and here what we are

play28:15

talking about is providing a mechanism

play28:18

whereby a particular characteristic of a

play28:22

string can be expressed

play28:24

and we then want to determine whether a

play28:27

string matches the

play28:28

specification now normally

play28:31

in

play28:32

the modern day high level programming

play28:35

languages that we usually encounter

play28:38

this specification

play28:40

for a

play28:42

pattern matching sequence would be

play28:45

performed by means of what's referred to

play28:47

as a regular expression there are

play28:49

competing standards that were

play28:52

alternatives to regular expressions in

play28:54

the past but most of those have fallen

play28:57

away and these days

play28:59

in almost all situations where we

play29:01

perform pattern matching we will be

play29:03

using regular expressions of some sort

play29:09

now of course different programming

play29:11

languages provide different kinds of

play29:13

support for character strings

play29:15

so we'll look at a few examples of

play29:17

programming languages and how they

play29:19

support character strings on this slide

play29:22

and the next

play29:23

cnc plus do not provide primitive string

play29:27

data types however they do provide

play29:30

support for character strings by means

play29:32

of arrays that contain character types

play29:36

they also provide operations such as

play29:39

concatenation and the extraction of

play29:42

substrings from existing strings by

play29:44

means of a set of library functions that

play29:48

perform operations on these character

play29:51

arrays

play29:53

fortran and python both provide a

play29:56

primitive character string type and they

play29:59

also then provide built-in support for

play30:02

assignment as well as several other

play30:04

operations that can be performed on

play30:06

character strings

play30:08

java provides a primitive character

play30:11

string type by means of the string class

play30:15

however it does also provide something

play30:17

similar to c and c plus plus's character

play30:20

arrays by means of the string buffer

play30:23

class the string buffer class is in a

play30:26

lot of ways much more efficient to work

play30:29

with than the string class is and the

play30:32

reason for this is that operations on

play30:35

the string class such as concatenations

play30:38

for example

play30:39

will result in a new string being

play30:42

created

play30:44

this then results in a lot of additional

play30:46

objects being created in memory and then

play30:49

potentially being disposed of as well

play30:52

which means that the garbage collector

play30:54

needs to work overtime the string buffer

play30:57

class on the other hand provides direct

play30:59

access to characters within it so

play31:01

therefore one can perform character

play31:04

manipulation operations on string buffer

play31:07

objects which are then much more

play31:09

efficient because new objects are not

play31:11

being created we are instead simply

play31:14

modifying the existing string buffer

play31:16

object that we are working with

play31:21

the snowball 4 programming language

play31:23

which we spoke about briefly in chapter

play31:26

2

play31:27

is as we've seen a string manipulation

play31:30

language it was designed for the

play31:32

implementation of text editors

play31:36

so because of this the manipulation of

play31:39

character string data is very central to

play31:41

the language and therefore the string

play31:44

type is primitive in snowball 4.

play31:47

there are also a wide variety of

play31:49

operations that can perform

play31:51

manipulations on these strings

play31:54

including very elaborate pattern

play31:56

matching

play31:57

what's interesting to note here is that

play31:59

the pattern matching matching uses a

play32:01

very different mechanism to modern day

play32:04

regular expressions

play32:07

then perl javascript ruby and php all of

play32:10

which are scripting languages all

play32:12

provide built-in support for pattern

play32:14

matching and they all use regular

play32:17

expressions which as i've previously

play32:19

mentioned has become the standard today

play32:24

of course strings have a length

play32:26

associated with them which is equivalent

play32:29

to the number of characters stored in

play32:32

the stream

play32:33

now there are three different ways that

play32:34

the length of strings can be managed by

play32:38

a programming language and we'll look at

play32:40

each of these in turn now and also look

play32:43

at some practical examples of

play32:45

programming languages that use these

play32:48

different string length options

play32:51

so first of all we have static length

play32:53

strings and as the word static implies

play32:56

here the length of the string is

play32:59

determined before runtime and doesn't

play33:01

change through the course of runtime

play33:05

now cobol is an example of a programming

play33:08

language that supports static length

play33:10

strings

play33:11

and interestingly enough java's string

play33:13

class is also an example of a static

play33:16

length string

play33:18

so what this then implies is that when

play33:21

we're working with static length strings

play33:24

we can then not grow or shrink the

play33:26

strings through the course of run time

play33:28

so what this means is any operation that

play33:31

is performed that would modify a string

play33:34

so for example removing characters from

play33:36

the string or doing something like

play33:39

concatenating two strings together

play33:42

actually they need to produce a new

play33:46

static length string so this is the case

play33:48

with java string class

play33:50

if we concatenate two java string class

play33:53

objects together then what actually

play33:56

happens behind the scenes is a new

play33:58

string object is created and this then

play34:01

stores the characters from the two

play34:03

strings that are concatenated with one

play34:06

another

play34:08

the next option is limited dynamic

play34:10

length strings so here the word dynamic

play34:13

implies that the length of a string can

play34:15

change through the course of execution

play34:17

however this is limited

play34:20

so

play34:21

two good examples of programming

play34:23

languages that support limited dynamic

play34:25

length strings are c and c plus plus

play34:28

which you should be relatively familiar

play34:30

with

play34:31

so what happens here then is that a

play34:34

fixed length structure is used to house

play34:37

the string and in the case of cnc plus

play34:39

plus this structure would be an array

play34:43

so the length of the array is then fixed

play34:46

it might be determined prior to runtime

play34:49

if this is just a static

play34:51

array

play34:53

but it may also be determined at runtime

play34:56

if we for example dynamically allocate

play34:59

the array that the string will be

play35:01

contained in

play35:02

either way the length of the string is

play35:05

fixed once it has been set and it cannot

play35:07

change through the course of runtime

play35:09

so now our array stores the characters

play35:12

in the string and then a special

play35:15

character is used to indicate the end of

play35:17

the string and in the case of c and c

play35:19

plus plus this is a null character

play35:23

the length of the string is also not

play35:25

maintained explicitly

play35:27

so what this thing means is we can then

play35:31

store

play35:32

a string that can be contained within

play35:35

the array

play35:36

and we can store as many characters as

play35:39

the array allows us to store as long as

play35:42

we leave a space for the special

play35:43

character but we can also have strings

play35:46

that are shorter than this length and in

play35:48

this case we are then essentially

play35:51

terminating the string early

play35:53

and before we reach the balance of the

play35:56

array by means of the use of this

play35:58

special character

play36:00

now because the length isn't maintained

play36:02

explicitly we can't generally look this

play36:05

length up however we can determine the

play36:08

length at runtime by iterating through

play36:11

the characters in our string and

play36:13

counting them one by one until

play36:16

eventually we reach our special

play36:18

terminating character

play36:21

then in the third place we have dynamic

play36:23

length strings

play36:24

and as the word dynamic implies here

play36:27

these are strings where the length can

play36:30

change through the course of run time

play36:33

so these strings in other words then are

play36:35

variable in length they can grow and

play36:38

shrink and there typically isn't a

play36:40

maximum length

play36:42

examples of programming languages that

play36:44

support dynamic length strings are

play36:47

snowball for

play36:48

perl and javascript so we see that these

play36:52

programming languages or scripting

play36:54

languages

play36:55

and this of course makes sense because a

play36:58

dynamically growing shrinking string is

play37:00

something that you would typically want

play37:02

in the context of a scripting language

play37:04

where you want to very quickly knock

play37:06

together a program without having to

play37:08

worry about memory allocation and

play37:11

de-allocation

play37:13

so dynamic link strings are different to

play37:16

limited dynamic length strings in that

play37:18

we are no longer constrained by the

play37:22

containing structure in the case of cnc

play37:24

plus the array structure

play37:26

so behind the scenes a dynamic length

play37:29

string may be implemented in a variety

play37:31

of different ways

play37:33

we may use for example some sort of

play37:35

linked structure like a linked list

play37:37

where each node would then store a

play37:39

character or alternatively we may be

play37:42

using arrays behind the scenes and then

play37:44

we have automatic growing operations

play37:47

that take place where a new array would

play37:49

be allocated that would be large enough

play37:51

to contain the resultant string after a

play37:54

string manipulation operation

play37:57

now ada gives us a lot of flexibility

play37:59

ada in fact supports all three of these

play38:03

string length options so it supports

play38:05

static length strings limited dynamic

play38:08

length strings and also

play38:10

fully dynamic length strings

play38:15

so let's evaluate character strings in

play38:18

terms of whether they are useful to be

play38:21

included in a programming language or

play38:24

not so in order to do this we need to

play38:26

look at the advantages

play38:29

and potentially disadvantages in

play38:32

relation to our programming language

play38:34

evaluation criteria which we've been

play38:36

using throughout this course

play38:39

well character strings provide increased

play38:42

readability

play38:44

so what i would like you to do at this

play38:46

point is pause the video and try to

play38:49

explain why character strings contribute

play38:52

positively to write ability

play38:54

and in order to do this you'll need to

play38:56

think about what the alternative would

play38:59

be if you wanted to support sequences of

play39:02

characters in some way

play39:05

assuming that

play39:07

character strings are not directly

play39:09

supported by the programming language

play39:11

you're considering

play39:14

so if we implement character strings as

play39:18

a primitive data type with static length

play39:22

then this is fairly inexpensive for us

play39:24

to provide

play39:25

and this is of course because all of the

play39:28

memory allocation

play39:29

is happening prior to run time

play39:33

so this thing makes these kinds of

play39:36

strings where the length is static and

play39:38

we implement them as a primitive type

play39:40

very efficient in terms of runtime

play39:43

execution

play39:44

so why not then support them in a

play39:47

programming language

play39:49

so if we then move from our primitive

play39:53

static link strings through to dynamic

play39:55

length strings then of course this is

play39:58

very nice if our programming language

play40:00

supports these kinds of strings we have

play40:03

very flexible string strings that can

play40:05

grow and shrink and this thing of course

play40:08

improves the writability of our

play40:10

programming language

play40:11

however is it worth any additional cost

play40:15

that might be associated with the

play40:16

support

play40:18

so what i would like you to do at this

play40:20

point is pause the video

play40:22

and try to explain why there is a

play40:25

negative cost impact in other words why

play40:28

does the cost of the programming

play40:30

language increase if we provide support

play40:33

for dynamic length streams

play40:35

think about how the memory allocation

play40:38

and the allocation needs to work for

play40:41

these kinds of strings

play40:45

we'll now take a look at the

play40:47

user-defined ordinal types so in order

play40:50

to understand what a user-defined

play40:52

ordinal type is we first need to know

play40:54

what an ordinal type is

play40:57

and this is quite simply a type that has

play40:59

a series of possible values but each

play41:03

possible value can be easily associated

play41:06

with the set of positive integer values

play41:11

now it is of course possible for ordinal

play41:13

types to be provided by a programming

play41:15

language and this is the case in java

play41:18

but the same also holds in c and c plus

play41:21

plus as well as a wide variety of other

play41:24

programming languages

play41:25

so we see in java that the built-in

play41:28

primitive types int char and boolean are

play41:32

all three ordinal types now why is this

play41:35

the case well a variable of type int has

play41:39

a value that is an integer value and of

play41:42

course it also allows for negative

play41:44

integer values

play41:46

but it is possible by simply adding a an

play41:50

offset to each of the integer values

play41:54

that we can transpose then every value

play41:57

that is legal for an integer variable

play42:00

into the positive range so what we now

play42:03

have is a situation where each of the

play42:05

values that an int variable can take on

play42:08

will then correspond to a positive

play42:12

integer value

play42:15

so next if we look at

play42:18

the chart type in java

play42:21

and we see that we have a series of

play42:22

characters that can be represented by a

play42:25

variable of type chart

play42:28

however what we saw previously in this

play42:30

lecture is that every character maps to

play42:34

a positive integer code

play42:36

because we have to use an encoding

play42:39

scheme in order to represent our

play42:41

characters in memory so what this then

play42:44

means again is that every value that

play42:46

char variable can take on in other words

play42:49

each character value that a child

play42:50

variable can take on then maps to a

play42:54

positive integer value

play42:57

and then in the third place we have

play42:59

boolean variables

play43:01

so here we then of course have a

play43:04

variable which is of type boolean and

play43:07

can take on one of two values a true

play43:10

value or a false value now of course we

play43:14

can map true and false values to zero

play43:18

and one values and this then again

play43:21

allows us then to create a

play43:22

correspondence between every value that

play43:24

a brilliant variable can take on and a

play43:27

positive integer value

play43:30

so these are then all examples of

play43:32

primitive ordinal types because they are

play43:34

built into the java programming language

play43:37

but we also have user-defined ordinal

play43:40

types which are then ordinal types that

play43:42

have been defined by a programmer

play43:46

now there are several user-defined

play43:49

ordinal types that are possible but we

play43:51

are only going to be looking at one of

play43:53

these namely the enumeration type which

play43:57

is quite commonly used in practice

play44:01

so let's spend some time looking at

play44:04

enumeration types in a bit more detail

play44:07

so an enumeration type is a type that

play44:10

can be defined by a programmer and it

play44:13

has a series of possible values that it

play44:15

can take on where each of these values

play44:19

will be then provided in the definition

play44:21

of the enumeration type and also these

play44:24

values are named constants

play44:27

so let's look at an example of this in

play44:30

the c-sharp programming language but

play44:32

enumerations are supported in a variety

play44:35

of other programming languages including

play44:38

c and c

play44:40

so here we have then a definition of an

play44:44

enumeration type this is indicated by

play44:46

the special word enum

play44:49

and we provide then a name for our

play44:52

enumeration type which in this case is

play44:54

day

play44:55

now this doesn't mean that we are

play44:57

defining a variable named day we are now

play45:00

creating a new type called day so

play45:04

a variable then of type day can be

play45:06

defined we could for example have a

play45:09

statement that declares a variable

play45:12

where we would specify day my day and in

play45:16

this case my day would then be the name

play45:19

of the variable and the type would then

play45:21

be day

play45:23

now following the name that we have

play45:26

provided for our enumeration type we

play45:28

then have a pair of braces and in the

play45:31

braces we then have a list of named

play45:35

constants and they are separated by

play45:37

means of commas

play45:39

so we can see for example that man is a

play45:43

named constant

play45:45

2 is also named constant and this holds

play45:47

for all of the remaining values all the

play45:50

way up to sun

play45:52

so these are then basically just names

play45:54

that are used to refer to values

play45:57

and what then happens in c sharp as well

play46:01

as in a variety of other programming

play46:03

languages is that the first named

play46:06

constant in this case man will then map

play46:09

to an integer value of zero

play46:12

two would then map to an integer value

play46:14

of one and so on and so forth

play46:19

so there are three main design issues

play46:21

that need to be decided on if we are

play46:24

going to provide support

play46:26

for enumeration types in a programming

play46:28

language

play46:30

so firstly can enumeration constants

play46:33

appear in more than one type definition

play46:37

for example in more than one enumeration

play46:41

so let's take this back to the example

play46:44

that we just discussed let's assume that

play46:46

we have this enumeration type called day

play46:49

defined and then we define a second

play46:52

enumeration type that we call week end

play46:56

day and weekend day then has two values

play47:00

that it can take on namely sat and sun

play47:04

now in this case we are then reusing the

play47:07

named constants sat and sun they appear

play47:11

once in the enumeration type day but

play47:14

they also appear in the enumeration type

play47:18

week end day so the question is then

play47:21

does the programming language allow for

play47:23

this or not now some programming

play47:26

languages will just simply disallow this

play47:28

and this removes any chance of any

play47:31

ambiguity

play47:32

however it does reduce flexibility

play47:34

somewhat

play47:36

but if we do allow then these named

play47:39

constants to appear in multiple

play47:41

enumeration types then how do we

play47:44

differentiate between them so we need

play47:47

some sort of syntactic construct that

play47:49

specifies whether we are referring to

play47:52

sat or sun

play47:54

in the enumeration type day or weekend

play47:58

day and this is necessary so that we can

play48:01

perform a type and check for each of

play48:05

these constants we need to know which

play48:08

specific type we are referring to

play48:12

then secondly our enumeration values

play48:15

closed in other words automatically

play48:17

converted to integer values now we saw

play48:20

in our example that each of the named

play48:23

constants maps to an integer value so

play48:26

this kind of coercion does make sense

play48:28

however there is an extended implication

play48:32

to this so if we allow enumeration

play48:35

values to be automatically converted to

play48:37

integers then all of the

play48:40

operations that are valid for integers

play48:43

will also then be valid for

play48:46

instances of the enumeration type

play48:51

so for example addition and subtraction

play48:54

would be valid now if we take this back

play48:56

to our previous example what this thing

play48:58

will mean is that if we allow variables

play49:01

of type data to be automatically

play49:03

converted into integers then this means

play49:06

that we can add variables of type day

play49:09

and we can subtract them from one

play49:11

another does it make sense for us to add

play49:14

2 to man probably not the same also

play49:18

holds for subtraction

play49:20

so some programming languages will

play49:22

completely disallow this kind of

play49:25

coercion

play49:27

however other programming languages will

play49:29

treat enumeration types as integers

play49:32

just simply because the mapping

play49:35

trivially makes sense

play49:37

then in the third place we have a fairly

play49:41

similar design issue to

play49:43

the second one that i just discussed

play49:45

are other types coerced to enumeration

play49:49

types so for example if we have integer

play49:51

values

play49:52

then can those be automatically

play49:55

converted into an enumeration type this

play49:59

will for example then allow us to create

play50:02

a variable of type day so let's call it

play50:05

my day and then we could assign to that

play50:08

an integer value of one for example and

play50:11

that assignment would then be equivalent

play50:14

to assigning two

play50:16

to our variable

play50:18

so is this allowed by a programming

play50:20

language or not well of course it does

play50:23

make logical sense that this kind of

play50:25

coercion should be allowed because again

play50:28

there's a trivial mapping between these

play50:30

named constants and integer values

play50:34

however

play50:36

a question then arises what happens if

play50:38

we attempt to assign an integer value

play50:41

that is outside of the balance of the

play50:43

enumeration so for example if we have my

play50:47

day which is of type day we would then

play50:50

be able to assign a value of 15 to that

play50:53

how does that assignment then get

play50:55

handled is it automatically converted

play50:57

into the appropriate range

play51:00

for our day enumeration is that

play51:02

assignment not allowed does it generate

play51:04

some kind of runtime error

play51:06

the and these are all questions that

play51:09

need to be answered in terms of how the

play51:11

programming language will go about

play51:12

dealing with these kinds of coercions

play51:16

the same of course holds for assigning a

play51:18

negative integer value to an enumeration

play51:22

type

play51:25

so let's evaluate enumeration types

play51:28

well the main advantage introduced by

play51:30

support for enumeration types is that

play51:33

they increase readability

play51:36

so let's assume that we have a program

play51:38

and we want to represent a number of

play51:41

colors now if we don't have support for

play51:44

enumeration types in our programming

play51:46

language we would have to create a

play51:48

separate integer code for each one of

play51:51

the colors that we want to represent

play51:53

so this is of course not very

play51:57

readable because we are referring to

play51:59

integer codes we need to memorize these

play52:01

integer codes and of course it is quite

play52:04

error-prone in practice to do this

play52:08

so instead of then having separate

play52:10

integer codes for our individual colors

play52:12

we can in our program then simply define

play52:15

an enumeration type

play52:17

lists the colors that we want to

play52:19

represent and then the encoding is

play52:21

handled for us behind the scenes we

play52:24

don't need to worry about that

play52:26

so that then increases readability it

play52:29

could also be argued that it improves

play52:31

writeability

play52:33

now enumeration types also aid the

play52:37

reliability of the programming language

play52:39

in which they are supported and this is

play52:42

because we can build in checks that the

play52:47

compiler can perform to ensure the

play52:49

validity of variables of a particular

play52:52

enumeration type

play52:54

so we can perform then

play52:57

checks related to operations so for

play53:00

example the compiler can disallow

play53:04

instances of an enumeration type from

play53:07

being added to each other so we can then

play53:09

block the possibility of two colors

play53:12

being added to one another

play53:14

the compiler can also check to ensure

play53:18

that values that are assigned to an

play53:21

enumeration variable are all within a

play53:24

valid range so for example if we have a

play53:27

set of 10 colors and we try to assign an

play53:31

integer value of 50

play53:34

to that enumeration type variable

play53:38

then the compiler can disallow that and

play53:41

can generate an error

play53:44

which will alert the user to the fact

play53:46

that a value outside of the legal range

play53:49

is being assigned to an enumeration

play53:51

variable

play53:53

now ada c-sharp java 5.

play53:57

these all have better support for

play53:59

enumeration types than c plus does and

play54:02

the reason for this is that enumerations

play54:05

are not coerced into integer types in

play54:08

these programming languages so this then

play54:12

this allows a situation where we for

play54:15

example would add two variables of a

play54:18

particular enumeration type to one

play54:20

another that kind of operation is not

play54:24

allowed by ada c-sharp and java 5.

play54:29

the last type that we'll look at in this

play54:33

lecture is the array type

play54:36

so you should be familiar with arrays

play54:37

arrays are basically an aggregate or a

play54:40

collection of homogeneous data elements

play54:44

so in general we assume that all of the

play54:47

data elements contained within an array

play54:50

have exactly the same type so for

play54:53

instance we can define an array of

play54:55

integers or an array of float values

play54:59

now individual elements are then

play55:01

identified by means of the position

play55:03

within the array and this position is

play55:06

relative to the first element and we

play55:09

usually refer to this then as indexing

play55:12

or subscripting

play55:14

so in general we will find that the

play55:17

first index or subscript is zero the

play55:20

next one along is one next one along is

play55:23

two and so on and so forth however some

play55:26

programming languages don't use a base

play55:29

zero indexing approach

play55:31

but instead will

play55:34

start at one or may even allow the

play55:37

programmer to define where they would

play55:39

like the indices to start

play55:43

now there are a number of design issues

play55:45

that need to be considered if we are to

play55:47

provide support for arrays in a

play55:50

programming language

play55:52

first of all what types are legal for

play55:55

the subscripts so the subscripts are the

play55:57

indices into the array are we only

play56:00

allowed to use integers or will the

play56:02

programming language allow us to use

play56:04

some other ordinal type something like

play56:07

an enumeration type for example

play56:10

then secondly our subscripting

play56:13

expressions in element references range

play56:16

checked so this relates to an expression

play56:19

being used for the index into a

play56:22

particular array

play56:24

does the runtime system of the

play56:27

programming language then perform some

play56:29

kind of checking to ensure that the

play56:32

element that is being accessed actually

play56:35

does lie within the array in other words

play56:37

does it prevent the programmer from from

play56:40

indexing past the end of the array

play56:44

then in the third place

play56:46

when are subscript ranges bound

play56:49

so this relates then to the size of an

play56:52

array at what point is the size

play56:54

determined does it happen before runtime

play56:57

or can it happen during the course of

play56:59

run time

play57:01

then somewhat related to the third point

play57:06

when does allocation take place so here

play57:09

we're talking about memory allocation

play57:11

for the array

play57:12

in other words at what point then will

play57:15

the spaces for the various data elements

play57:18

be reserved in memory again does this

play57:21

occur before runtime or does it occur

play57:24

during runtime and can it change through

play57:26

the course of execution

play57:29

then what is the maximum number of

play57:32

subscripts so as you know we can create

play57:35

multi-dimensional structures using

play57:37

arrays so for instance a matrix can be

play57:40

represented using a two-dimensional

play57:42

array a cubic structure can be

play57:45

represented using a three-dimensional

play57:47

array so is there some kind of limit to

play57:50

how many subscripts we can have which

play57:52

would then limit the dimensionality of

play57:55

an array structure

play57:56

or is there no limit

play57:59

this is a design issue that needs to be

play58:02

decided up front for the programming

play58:04

language in question

play58:06

then are ragged

play58:08

or rectangular arrays allowed or are

play58:11

both allowed we'll get to what ragged

play58:14

and rectangular arrays are in a moment

play58:18

then can array objects be initialized so

play58:21

what we are talking about here is a sign

play58:24

assigning an initial value to a variable

play58:28

that is an array variable

play58:32

so is there a way in other words to

play58:34

populate the array with initial values

play58:37

before the programmer can then actually

play58:40

use the array

play58:42

and then finally are any kind of array

play58:45

slices supported and again we'll get

play58:47

into what array slices are in a moment

play58:53

array indexing or subscripting is a

play58:56

simple mapping from indices to elements

play59:00

contained within an array

play59:03

so over here we have an example

play59:06

we have an array referred to by array

play59:09

name

play59:10

and this is in the identifier of the

play59:14

array that we are going to be indexing

play59:16

or subscripting we then also have an

play59:20

index value which is

play59:23

then specified for our array and this

play59:26

indicates then the position

play59:28

within the array that we want to access

play59:31

element from

play59:32

relative to the first position in the

play59:36

array

play59:37

we then have a mapping that is performed

play59:40

and this mapping then maps our index

play59:42

value to an element that is actually

play59:45

contained within our array

play59:49

now of course different kinds of

play59:51

syntactic notations can be used to

play59:54

represent indexing and the majority of

play59:57

programming languages use the notation

play59:59

that you will be familiar with namely

play60:01

square brackets that contain the index

play60:05

that we want to access within our array

play60:08

however fortran pl1 and edo all use

play60:12

parentheses in other words round

play60:14

brackets in order to

play60:17

contain the index value that

play60:21

we wish to access within our array

play60:24

now in ada the reasoning behind this is

play60:28

that it provides uniformity between

play60:31

array references and function calls the

play60:34

reason for this is that ada considers

play60:36

both of these to be mappings so in the

play60:40

case of an array reference we are

play60:42

performing a mapping from an index to a

play60:46

specific array element if we are talking

play60:49

about function calls we are mapping

play60:51

parameter values into the function so

play60:54

because ada considers these two

play60:55

operations to be similar because they

play60:58

both involve mappings it then uses the

play61:01

parenthesis notation

play61:03

in order to indicate this similarity

play61:08

next let's look at what data types are

play61:12

allowable to serve as array indices or

play61:16

subscripts

play61:17

so what we are looking at here is what

play61:20

type can a particular index or subscript

play61:23

value take on

play61:25

the core question here is will the

play61:27

programming language only allow for

play61:30

integer values to be used as array

play61:32

indices

play61:34

or will it allow other ordinal types to

play61:37

be used

play61:39

so fortran c c plus plus and java all

play61:43

only allow integer values to be used as

play61:46

subscripts

play61:48

however in the case of c and c plus plus

play61:50

this does include then any particular

play61:53

value that will be automatically

play61:55

converted into an integer value by means

play61:58

of coercion and i've provided some

play62:01

additional details in the notes for this

play62:04

slide now the ada programming language

play62:07

is different any ordinal type can be

play62:10

used as a subscript into an array so

play62:14

this includes the integers of course but

play62:17

also user-defined enumeration types

play62:20

boolean values as well as character

play62:23

values because all of these allow for a

play62:26

mapping between the variable's value

play62:30

and an integer value that is positive in

play62:33

nature all of these then are defined as

play62:36

ordinal types and can be used as

play62:38

subscripts

play62:42

next let's look at support for index

play62:45

range checking for arrays

play62:48

so there are two main approaches that

play62:51

can be used when it comes to index range

play62:53

checking

play62:54

firstly every access into an array could

play62:57

be range checked and then the

play63:00

alternative to that is to perform no

play63:03

range checking

play63:05

it is also possible for a programming

play63:07

language to support both of these

play63:09

approaches

play63:11

using some kind of mechanism to

play63:13

differentiate between them

play63:15

so if we look at this in terms of real

play63:18

world programming languages c c plus

play63:20

plus pearl and fortran all don't perform

play63:24

any index range checking at all

play63:27

so what i would like you to do at this

play63:28

point is to pause the video and try to

play63:31

think of an advantage and a drawback

play63:34

associated with this lack of index range

play63:38

checking specifically in terms of our

play63:41

programming language evaluation criteria

play63:44

that we've been using in this course

play63:47

then java ml and c-sharp all

play63:51

do support index range checking and in

play63:54

fact require that every indexing or

play63:58

subscripting into an array must be range

play64:01

checked so again i would like you to

play64:03

pause the video at this point and try to

play64:06

answer

play64:08

what an advantage and a drawback would

play64:11

be associated with this approach to

play64:14

range checking

play64:16

once again in relation to the

play64:18

programming language evaluation criteria

play64:21

we've been using so far

play64:25

then ada

play64:26

again follows a slightly different route

play64:28

to the other programming languages

play64:30

ada does have array index range checking

play64:34

by default however it can be turned off

play64:37

by the programmer so what i would like

play64:40

you to do at this point is again pause

play64:42

the video and try to think of an

play64:44

advantage associated with ada's approach

play64:51

we'll now delve into subscript binding

play64:54

and storage binding

play64:56

so subscript binding is a binding of a

play64:59

size to an array variable where the size

play65:02

specifies how many values are contained

play65:05

within this array

play65:07

storage binding on the other hand is the

play65:09

binding of the actual memory space that

play65:12

is allocated for each of the values

play65:15

contained within the array

play65:17

now there are different categories of

play65:20

arrays

play65:21

that can be defined based on how they

play65:24

deal with subscript binding and storage

play65:26

binding

play65:28

so first of all we have the simplest

play65:30

kind of array which is a static array

play65:34

so this is then simply a static local

play65:37

variable but the type of this variable

play65:39

is an array so you can think of this in

play65:42

the context of c or c plus plus where we

play65:46

have a variable that is defined inside a

play65:48

function but this variable is defined as

play65:50

a static variable and then the type of

play65:52

the variable is for instance an integer

play65:56

array

play65:57

so in this case then the subscript range

play66:01

is statically bound in other words it

play66:03

takes place before run time so the size

play66:06

of the array is then specified before

play66:10

program execution begins

play66:12

what this means is that the length of

play66:14

the array must then be a constant

play66:17

because this length is not allowed to

play66:19

change

play66:20

during run time it's only sick once

play66:23

prior to run time

play66:26

where the

play66:27

subscript range is then bound to the

play66:30

array

play66:31

now storage allocation then is also

play66:34

static in nature so the memory allocated

play66:36

for the array also then

play66:39

is

play66:41

allocated prior to runtime

play66:45

so we see then that this is the case

play66:47

with a static local variable which has a

play66:50

type of an array

play66:52

and this is because this variable is

play66:55

allocated prior to runtime and then

play66:58

remains allocated for the entire

play67:00

execution of the program

play67:03

now the main advantage associated with

play67:06

purely static arrays is that they are

play67:09

very efficient in terms of execution

play67:11

time and this is because all of the

play67:13

allocation that takes place is static in

play67:16

nature it does not occur dynamically at

play67:19

run time in general any dynamic

play67:22

allocation is

play67:24

usually slower than a static allocation

play67:28

now what i would like you to do at this

play67:29

point is to pause the video once more

play67:31

and try to think of a disadvantage

play67:34

associated with static arrays

play67:38

now the next category of arrays that

play67:41

we'll look at is the fixed stack dynamic

play67:45

array

play67:46

so here we have the subscript range

play67:50

which is statically bound again this

play67:52

means that the length must be a constant

play67:55

however a location for the array occurs

play68:00

at declaration elaboration time and

play68:02

de-allocation usually occurs at the end

play68:05

of the arrays scope

play68:08

so an example of this kind of array

play68:10

structure is in coc plus where we have a

play68:14

local non-static variable that has the

play68:17

type of an array

play68:19

now in that case a variable cannot be

play68:22

used to define the length of the array

play68:25

the length of the array must be

play68:27

specified by means of a constant so a

play68:30

literal integer or a named constant for

play68:34

instance

play68:35

and so this then

play68:37

allows

play68:38

the

play68:39

size of the array in other words the

play68:41

subscript range to be defined then

play68:44

statically prior to runtime because this

play68:46

is defined by a constant value

play68:49

however we will only allocate memory for

play68:52

the array once execution reaches the

play68:55

declaration of that array and then

play68:58

similarly once the scope of the array

play69:00

variable then ends then that memory

play69:03

space will be deallocated automatically

play69:06

so in other words we have an array that

play69:08

behaves in the same way in terms of

play69:11

storage allocation

play69:13

as a regular

play69:15

local variable which is non-static in

play69:18

nature would have

play69:20

so the main advantage of fixed stack

play69:23

dynamic arrays is that they are very

play69:25

space efficient what i would like you to

play69:27

do at this point is to pause the video

play69:29

and try to explain why fixed stack

play69:32

dynamic arrays are efficient in terms of

play69:36

storage space in memory

play69:42

next we have stack dynamic arrays so in

play69:45

stack dynamic arrays the subscript range

play69:47

is dynamically bound

play69:50

this means that the length then is

play69:52

allowed to be a variable obviously a

play69:54

constant could also be used

play69:57

the storage allocation for resdac

play69:59

dynamic array is also dynamically bound

play70:03

so in other words both the subscript

play70:05

range and the storage

play70:08

binding will then be determined at

play70:12

runtime

play70:13

now both the subscript range and the

play70:16

storage binding will be then fixed after

play70:18

the initial binding takes place

play70:21

now there is unfortunately no analog for

play70:25

this in the cnc plus programming

play70:27

languages but if we could define an

play70:31

array variable locally inside a function

play70:34

and we could use for the size of that

play70:37

array a variable this then means that

play70:40

the size of the array could be different

play70:43

in different executions of the program

play70:46

depending upon what value that variable

play70:49

then receives for the size of the array

play70:52

however that size cannot change once it

play70:54

has been allocated so the array can't

play70:56

grow and shrink over the course of

play70:59

runtime

play71:00

also then

play71:02

storage space would be allocated for the

play71:04

array once the declaration of that array

play71:06

is reached and then generally that array

play71:09

would be deallocated once execution

play71:11

reaches the end of the arrays scope

play71:15

so the main advantage with a stack

play71:17

dynamic array is flexibility and this is

play71:20

because the array size doesn't need to

play71:23

be known until the array is actually

play71:25

used and of course then the array size

play71:29

can be allocated from a variable which

play71:31

gives us increased flexibility you could

play71:34

for example have the user of a program

play71:37

type in the size of the array which

play71:39

would then result in that much memory

play71:41

space being allocated

play71:44

next we have fixed heap dynamic arrays

play71:48

and here the subscript range and storage

play71:50

binding are both dynamic but both of

play71:53

these are fixed after allocation takes

play71:55

place

play71:57

now binding is done by means of an

play72:00

explicit request so a good example of

play72:03

this is dynamic arrays in c plus

play72:07

so we can create then a pointer

play72:10

to an array and then we would

play72:12

specifically use the new special word in

play72:17

order to create a memory allocation for

play72:21

the array also at this stage the

play72:23

subscript binding takes place because

play72:26

the size of the array can be specified

play72:28

by means of a variable

play72:30

once we're done with the array then we

play72:32

have to use a delete directive and this

play72:36

will then indicate that the memory space

play72:38

can be deallocated for our array so here

play72:42

we have an example of this in c

play72:46

we have a pointer named p which is an

play72:49

integer pointer and we've then assigned

play72:52

to that

play72:53

a new integer array with the size

play72:57

specified in the brackets over here so

play73:01

that size can then be a variable and

play73:03

what this then means is that the

play73:05

subscripts have been bound at runtime

play73:09

because we've specified the size of the

play73:11

array but also storage space has been

play73:13

allocated now what's important to

play73:15

understand here is that storage is

play73:17

allocated from the heap not the run time

play73:21

stack so what this means then is that an

play73:23

array that is allocated in this way

play73:26

can then continue to exist

play73:30

once the scope of its variable has ended

play73:37

the final array category that we have in

play73:39

terms of subscript binding and storage

play73:41

binding is the heap dynamic array so

play73:45

this array is as dynamic as we can make

play73:48

it the binding of both subscript ranges

play73:52

and storage space will occur dynamically

play73:55

at runtime and the binding can also

play73:58

change any number of times through the

play74:00

course of the program's execution

play74:03

now it's also important to understand

play74:05

that storage once again here is

play74:07

allocated from the heap

play74:09

rather than the runtime stack

play74:12

so what this then means in summary is

play74:14

that a heap dynamic array can grow and

play74:17

shrink as items are added to the array

play74:20

and removed from the array

play74:23

so the main advantage being associated

play74:26

with heap dynamic arrays is that they

play74:28

are very flexible

play74:30

arrays are exactly the size that they

play74:32

need to be no larger no smaller and they

play74:36

can continuously adapt as we change the

play74:39

number of elements contained within the

play74:41

array

play74:42

so what i would like you to do at this

play74:44

point is to pause the video and try to

play74:46

think of a disadvantage associated with

play74:50

heap dynamic arrays

play74:56

so let's look at some modern programming

play74:58

languages and what support they provide

play75:01

for these array categories that i

play75:04

discussed over the previous three slides

play75:07

now cmc plus plus we've already looked

play75:10

at largely over the course of the

play75:12

previous discussion

play75:14

if we have arrays that are declared

play75:16

locally inside a function and they use

play75:19

the static special word then they are

play75:22

static arrays however if we have arrays

play75:26

that are defined locally inside a

play75:28

function but they don't use the static

play75:31

special word then they are fixed stack

play75:35

dynamic arrays

play75:36

both c and c plus plus also provide

play75:39

support for heap dynamic arrays

play75:42

we looked at an example in c plus where

play75:45

we used a pointer and then the new

play75:48

special word

play75:49

in order to allocate memory for a heap

play75:52

dynamic array

play75:54

and then we also use delete to

play75:56

de-allocate memory space for the array

play76:01

and c uses a similar approach however it

play76:04

doesn't have special words for

play76:06

allocation and deal location instead it

play76:09

uses library functions such as malloc

play76:12

and dialog to allocate and deallocate

play76:15

memory for an array

play76:18

now c sharp has relatively similar

play76:20

support to c plus plus since of course

play76:23

makes sense because c sharp is largely

play76:26

derived from c plus plus however it does

play76:29

additionally provide an arraylist class

play76:32

which is a heap dynamic array

play76:36

and so this means that objects of type

play76:39

arraylist can then grow and shrink over

play76:42

the course of execution as values are

play76:44

added to or removed from this instance

play76:50

in java all arrays are fixed heap

play76:52

dynamic and so they use a fairly similar

play76:56

notation to c plus

play76:58

using a new special word to indicate

play77:01

that memory allocation must happen for

play77:03

the array however in the case of java

play77:05

there's no explicit delete directive

play77:08

instead java relies on the garbage

play77:11

collector to clean up memory once an

play77:14

array is no longer needed

play77:17

then the perl javascript python and ruby

play77:21

scripting languages all support heap

play77:23

dynamic arrays and this of course makes

play77:26

sense because all of these are scripting

play77:28

languages and therefore the increased

play77:31

flexibility provided by heap dynamic

play77:34

arrays will come in useful for

play77:37

programmers who want to quickly put a

play77:40

program together without too much effort

play77:45

finally for this lecture we will talk

play77:48

about array initialization

play77:51

so array initialization refers to

play77:54

structures provided by a programming

play77:57

language in order to allow for the

play77:59

initialization of arrays at the time of

play78:02

storage allocation

play78:05

so over here we have an example in c c

play78:09

plus java and c sharp we can see that we

play78:12

are defining a variable called list and

play78:15

this is an array of integer values

play78:20

now over here we can see that we have

play78:22

performed an assignment and this

play78:24

assignment is then to a range of values

play78:28

enclosed in braces

play78:30

and there we have listed then the values

play78:32

that must be initially contained within

play78:35

this array

play78:36

so at subscript to index 0 we will have

play78:39

the value 4

play78:40

at subscript 1 the value 5 at subscript

play78:43

2 the value 7 and add subscript 3 the

play78:46

value 83

play78:49

notice also that we haven't provided a

play78:52

size for this array and the reason for

play78:54

this is that the initialization

play78:57

can determine for us what the size of

play79:00

the array needs to be

play79:02

from the number of values contained in

play79:04

the braces so in this case we would have

play79:07

an array of size 4 that would be created

play79:11

now we can also do the same kind of

play79:14

thing with character strings in c and c

play79:16

plus plus

play79:18

so over here we have defined a variable

play79:20

called name and this is an array of

play79:23

characters and we can assign to that

play79:26

then the string literal fred in this

play79:29

case we know that this is a string

play79:31

literal because it contains the

play79:34

characters for the word fred in double

play79:37

quotes

play79:39

now once again here we haven't specified

play79:42

the size of this array and this is

play79:45

because once again the size can be

play79:47

determined from what is assigned to the

play79:50

array so in this case sufficient space

play79:53

would be allocated for all of the

play79:55

characters in the literal string

play79:58

so we have then space for f r e and d in

play80:02

other words four character spaces but

play80:05

because we know that arrays

play80:08

of characters are null terminated in c

play80:11

and c plus plus we would then have an

play80:13

additional space for that null character

play80:17

bringing the total length of the name

play80:19

array to 5.

play80:21

we can also do things like create arrays

play80:24

of strings in cnc plus plus

play80:27

so here we are creating an array called

play80:30

names and this array then stores

play80:33

character

play80:34

pointers

play80:36

we can then perform an assignment so

play80:39

once again we then have an assignment

play80:44

which then has a series of values which

play80:47

are contained inside braces

play80:50

and contained within these braces we

play80:52

then have string literals namely bob

play80:55

jake and joe

play80:57

java has a similar kind of approach but

play81:00

it would use of course stream objects so

play81:03

over here we are creating an array

play81:05

called names that contains strings and

play81:08

then we use our initialization list in

play81:12

the braces in order to then

play81:16

represent the string literals that will

play81:19

be contained within the names array

play81:25

now ada provides fairly elaborate

play81:27

support for array initialization and

play81:30

over here we have an example of this

play81:34

so here we are declaring a variable

play81:36

called list and we specify that list is

play81:39

an array

play81:40

where the first index or subscript of

play81:43

the array is 1 and the last index or

play81:45

subscript is 5. we also specify that

play81:49

this array contains integer values

play81:52

so now we perform an assignment to the

play81:55

second line over here

play81:57

and this line specifies that at index or

play82:00

subscript 1 we will store a value of 17

play82:04

at index or subscript 3 we store a value

play82:08

of 34 and then at all other subscripts

play82:12

or indices we will place values of zero

play82:16

so this gives us relatively fine grained

play82:18

control

play82:19

over which values are stored within an

play82:22

array and this notation also has the

play82:24

potential for being much more compact

play82:27

than an explicit initialization for each

play82:30

of the individual values contained

play82:32

within the array

play82:35

finally python uses what are referred to

play82:38

as list comprehensions

play82:41

in order to perform array initialization

play82:44

but we'll discuss this later on in this

play82:46

chapter

play82:48

all right so that then concludes this

play82:52

lecture's discussion

play82:53

on

play82:55

data types in the next lecture we will

play82:58

be continuing our discussion on arrays

play83:01

looking at some of the more exotic kinds

play83:04

of arrays and array operations that

play83:06

might be supported

play83:08

and then we will also look at

play83:13

records

play83:14

tuples and lists

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Data TypesPrimitive TypesCharacter StringsUser-Defined TypesArray DataMemory StorageVariable AttributesEncoding SchemesType CoercionProgramming Concepts