cs 330 programming languages 11 / 04 / 2008 election day instructor: michael eckmann

53
CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Upload: kerry-arnold

Post on 05-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

CS 330Programming Languages

11 / 04 / 2008

Election Day

Instructor: Michael Eckmann

Page 2: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

Today’s Topics• Questions / comments?• Midterm exam grading• Data Types

– Enumerations– Subrange types– unions– Arrays– Records

Page 3: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

Midterm Exam grading• Mean = 73.55

• StdDev = 14.45

• Your Raw Score is out of 100

• To convert to a curved 0-4.0 scale, use the following

formula:

• Minimum of 4.33 and

• 2.85 + (RawScore – Mean) / StdDev + 4*EC/100

• Example: if your raw score is 78 with 2 points EC

• Min(4.33, 2.85 + (78-73.55)/14.45 + 4*2/100) =

• Min(4.33, 3.24) = 3.24

Page 4: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 3330 - Fall 2008

Midterm Exam grading• Notice that a raw score of exactly the mean will get the

mean grade of 2.85 (ignoring EC)

• A raw score below the mean (of 73.55) will get a grade

below 2.85 (ignoring EC)

• The reason I use the Min(4.33,...) is because I define 4.33

to be the maximum score per assignment/exam.

Page 5: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

Grading• At the end of the semester I compute your letter grade

from your final average on a 0-4.0 scale like so:

>4.17 = A+

>3.83 = A

>3.5 = A-

>3.17 = B+

>2.83 = B

>2.5 = B-

>2.17 = C+

>1.83 = C

>1.5 = C-

>1.17 = D+

>0.83 = D

>0.5 = D-

Page 6: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Ordinal / Enumeration

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Ordinal types

– Range of values are associated with positive integers

• Many languages provide support for user defined ordinal types like Enumerations.

• Enumeration types

– Named constants represent positive integers

– Design issues• Are enumeration type values coerced to integer?

– Same operations and range of values of integers• Are any types coerced to enumeration type values?

– Could break the allowable values of the enum type

– Range of values intentionally limited.

Page 7: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Enumeration

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• e.g. In C#

enum months {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};

• e.g. In C++

enum months {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};

months m; // declare a variable of type months

• Represented as 0 to 11 typically but could be given programmer specified values in the declaration.

• They look the same, but C# enum types not coerced to integer whereas C++'s are --- so, C# doesn't allow operations that don't make sense, while C++ does. e.g. In C++, we can add 1 to an enum

Page 8: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Enumeration

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• e.g. In C++

enum months {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};

// declares currentMonth of type months and assigns

// an initial value

months currentMonth = Jun;

• In C++, we can add 1 to an enum– e.g. currentMonth++;

• Any danger here?

Page 9: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Enumeration

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• As of Java 1.5 (aka 5.0), Java does support enumerations. When used, they create full classes for the enum. See:

http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html

• What are advantages to enumeration types?

Page 10: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Enumeration

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• What are advantages to enumeration types?

– All forms of implementation of them offer better readability

– Reliability• No arithmetic operations on the enum types in Ada

and C# --- this prevents adding/subtracting which might not make sense like in adding months together

• Ada and C# also restrict values within the range for the type.

• C treats them like integers so we don't get those advantages

Page 11: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Enumeration

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

– Reliability• C++ allows numeric values to be assigned to enum

types if they are explicitly cast.• C++ also allows values to be assigned to enum

types but the range is checked for validity.– This just checks that the integer is between the

lowest and highest enum value. Enum values need not be consecutive integers. Any problem here?

Page 12: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Subrange types

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Subrange types

• e.g. In Ada:

• type Days is (Mon, Tue, Wed, Thu, Fri, Sat, Sun);

• subtype Weekdays is Days range Mon..Fri;

• Day1 : Days;

• Day2 : Weekdays;

• Day2 := Day1;

• Only allowed if Day 1 isn't Sat or Sun.

Page 13: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Subrange types

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Subrange types

– When do you think they're type checked?

– When do you think they're range checked?

Page 14: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Subrange types

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Subrange types

– When do you think they're type checked?• Ada type checks at compile-time to make sure you are

assigning “compatible types” where in our example, Days and Weekdays are compatible types.

– When do you think they're range checked?• Ada range checks at run-time to make sure you are

assigning a valid value to a subrange type.

Page 15: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Subrange types

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Only current language that supports is Ada 95.

• Subrange types are implemented as their parent types but with additional range checks.

• Advantages / Disadvantages?

Page 16: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Subrange types

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Advantages / Disadvantages?

– Increased code size (range checking code is added for each assignment to a subrange)

– Increased execution time due to range checking

– Readability increases and reliability increases

Page 17: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

Unions• A Union is a type that can store one of different type values

during the execution of a program. Why?

• Design issues

– Type checking• Free

– No type checking• Discriminated

– Has type checking

– Are unions allowed in records?

Page 18: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

Unions• A Union is a type that can store one of different type values

during the execution of a program. Why?

• Design issues

– Type checking

• Free (Fortran, C and C++ have this type)

– No type checking

• Discriminated (Ada has this type)

– Has type checking

• Java and C# (the newer languages) do not have unions.

Page 19: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

Unions• An example in C++.

union Number {

int x;

float y;

}

// in some function somewhere we can:

Number num;

num.x = 100;

// then we print the contents of num.x and num.y

num.y = 25.4;

// then we print the contents of num.x and num.y

• note: this example is a modified version from Deitel & Deitel's C++ How to Program.

Page 20: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

Unions• When num.x = 100; and we print the contents of num.x and

num.y, num.x will print fine, but a reference to num.y will be a logic error and will not contain 100. num.y will interpret the bits of how 100 was stored as an int as a float.

• Same problem if we assign num.y a float value and try to access num.x

• These problems exist because C++ has Free Unions. There is no type checking done to prevent reference to the wrong name within the union at any given time.

• Discriminated Unions would have to carry what kind of information to disallow the problem described above in C++?

Page 21: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

Unions• Discriminated union example of memory storage

Page 22: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

Unions• Evaluation of Unions.

Page 23: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• We all know what arrays are.

• Design issues

– Legal types for subscripts

– Are references to subscript expressions range checked?

– When are subscript ranges bound?

– When is allocation bound?

– Are ragged and multidimensional arrays allowed?

– Is initialization allowed at allocation time?

– Slices?

Page 24: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Most languages use the name of the array followed by an index in square brackets to specify an element of an array

– e.g. array_of_names[5]

• Ada uses parentheses surrounding the index. Ada also uses parentheses for subprogram (function) calls to enclose the parameters. Any idea why they might have done that? Is it a good idea?

Page 25: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Ada chose to use parentheses in these two ways because both array references and function calls are similar in that they map the index (or parameters) to a value which is returned.

• It certainly reduces readability to some degree because the reader of the program can't tell the difference between an array reference and a function call with one parameter.

Page 26: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• There are two types associated with any array

– The type of the elements of the array

– The type of the subscript

• Most commonly integers (or subranges of integers)

• Ada allows boolean, characters and enumeration types as subscripts

• Range checking the subscripts (indices) When are we talking here?

– C, C++, Perl, and Fortran --- no range checking

– Java, ML, C# do range check

– Ada range checks by default, but can be programmer disabled

– Why not range check?

Page 27: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Lower bound of subscripts

– Usually 0 (C-based languages) Fortran95 defaults to 1.

– Why use 0?

Page 28: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Five categories of arrays

– Static – subscript ranges and storage allocation is static. Statically bound variables are bound BEFORE run-time.

– Fixed stack-dynamic – subscript ranges statically bound, but allocation done at declaration elaboration time. When is declaration elaboration time?

– Let's compare these two. What are the advantages of each?

Page 29: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Static vs. Fixed stack-dynamic

– Let's compare these two. What are the advantages of each?

– Static --- speed efficient --- no dynamic allocation or deallocation (at runtime) so they're faster.

– Fixed stack-dynamic --- space efficient --- if have block scoped arrays, they're only allocated when they need to be in memory instead of allocating all of them before run-time.

• And they can be deallocated from the stack when they're no longer needed.

Page 30: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Back to the five categories of arrays

– Besides static and fixed stack-dynamic

– Stack-dynamic• Subscript ranges dynamically bound

• Storage allocation is dynamic (on the stack)

Page 31: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Compare Stack-dynamic vs. static and fixed stack-dynamic

– Stack-dynamic• Subscript ranges dynamically bound

• Storage allocation is dynamic (on the stack)

• Both are fixed after they're bound

• What advantage does this have over static and fixed stack-dynamic?

Page 32: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Static and fixed stack-dynamic VS.

• Stack-dynamic– Size of the array doesn't need to be known until it is going to be

used.

Page 33: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Back to the five categories of arrays

– Besides static and fixed stack-dynamic and stack-dynamic

– Fixed heap-dynamic• Subscript ranges dynamically bound

• Storage allocation is dynamic (on the heap)

• Both are fixed after they're bound which is different from:

– Heap-dynamic• Subscript ranges dynamically bound

• Storage allocation is dynamic (on the heap)

• Both are changeable during execution.

Page 34: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• In functions (therefore block scope)

– C & C++ use static arrays if programmer uses the static modifier

– C & C++ use fixed stack-dynamic arrays by default (without the static modifier)

• C & C++ also use fixed heap-dynamic arrays

• Java - fixed heap-dynamic arrays

– Their subscripts and allocation are dynamically bound but fixed afterwards. What does that mean?

• What kind of arrays do you think Perl has?

Page 35: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Perl and Javascript (and C# has capability)

– Heap-dynamic arrays

– Perl has push and pop functions that treat arrays like a stack therefore changing the length --- I had not mentioned these before.

– I did mention though that you could do things like:

– @arr = (1, 2, 3, 4);

– $arr[4] = 5;

– Which also obviously lengthens the array dynamically

Page 36: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Array initialization– Sometimes the initialization implicitly states the length

– In C, C++, C# and Java this happens

– Ada has an interesting feature that allows initialization of some elements of the array to certain values and all others to another value.

Page 37: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Array operations (operate on array as a whole)

– Some languages allow concatenation of arrays and can use relational operators on arrays

– Others like APL have arithmetic operations on arrays like vector multiplication and matrix transpose, etc.

Page 38: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Rectangular vs. jagged arrays– Only makes sense when talking about multidimensional arrays

– Jagged if rows can have different numbers of columns in the same array

– I made can bold above, because Java, C and C++ support jagged arrays but do not support rectangular arrays.

– This is because multidimensional arrays are implemented as arrays of arrays.

– Fortran, Ada and C# provide rectangular arrays

– When are different numbers of columns for a row useful?

Page 39: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Slices

– Not a new data type

– Instead, it is a way to reference part of an array as a separate unit.

– e.g. One row, or some subset of consecutive elements in a column, or some rectangular subset of rows etc.

– Anyone know any languages that support slices?

Page 40: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Implementation of array types

– How to access array elements• Code needs to produce element addresses

– Descriptor for arrays• Needs to have information in it to construct the access

function

• That is, when an element of an array is referenced, the index along with the information in the descriptor needs to be able to find the address in memory where that element lives.

• So if run-time range checking is done in some language, what do you think the descriptor will contain?

Page 41: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Multidimensional Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Design issue:

– How to store the multidimensional array in memory?

– A 1-D array is typically stored sequentially in memory

Page 42: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Multidimensional Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Design issue:

– How to store the multidimensional array in memory?

– Row major order

– vs.

– Column major order

– Fortran uses column, all others use row

• Would the programmer care whether the multidimensional array is stored in row or column major order?

Page 43: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Multidimensional Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• How to access 2-dimensional array elements at run-time (assuming row major order and all rows have same number of columns and index starts at 0).

– We have the subscripts to look for

– We need address of first element

– What else do we need?

Page 44: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Multidimensional Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• How to access 2-dimensional array elements at run-time (assuming row major order and all rows have same number of columns and index starts at 0).

– We have the subscripts to look for

– We need address of first element

– We need the size of one element (determined from the type of the array.)

– We need to know the number of columns per row

– Luckily all this info is stored in the descriptor for the array

Page 45: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Multidimensional Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Address of element at row i, column j is =

address_of_array +

(num_cols * i + j) * size_of_element;

Page 46: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Associative Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Unordered collection of data indexed by keys.

• Each element of an associative array is a pair:

– key, value

• Perl's associative arrays are called hashes because they are implemented using a hash table and hash function.

• Because the data is unordered, Perl can store it in memory in any order it cares to.

Page 47: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

Records

• Records are a way to hold different types of data as a group. e.g. A record might hold an employee name, salary, hire-date, etc.

• The individual elements of a record are called fields.

• Design issues include:–How are fields referenced in the language–How are fields within nested record types

referenced in the language

Page 48: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

Records

• COBOL has elaborate record structures.

• A record name is defined at a LEVEL with a low number. Fields are defined at a LEVEL with a higher number. Nested Records are created by having LEVELs in between these.

• We'll go over an example of COBOL records in the next couple of slides

• struct defines records in C, C++ and C#

• What defines records in Java?

Page 49: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

Records

• COBOL record example.01 STUDENT-RECORD.

05 STUDENT-NAME.

10 LAST-NAME PICTURE X(20).

10 FIRST-NAME PICTURE X(20).

05 BIRTH-DATE PICTURE 999999.

05 EXPECTED-GRAD PICTURE 9999.

• Note that STUDENT-NAME is also a RECORD.

Page 50: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

Records

• COBOL record example.01 EMPLOYEE-RECORD.

05 EMPLOYEE-NAME.

10 LNAME PICTURE X(20).

10 FNAME PICTURE X(20).

05 BIRTH-DATE PICTURE 999999.

05 SALARY PICTURE 99999V99.

Page 51: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

Records

• COBOL has a MOVE CORRESPONDING command to copy values of fields of one record to another. It copies values only of fields with the same name in both records.

• Do you see any benefits or drawbacks to this?

Page 52: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

Records

• Ada also allows nested levels of records.

• Do C++ or Java allow nested records?

• Referencing field names–Fully qualified reference–Elliptical reference

LAST-NAME OF STUDENT-RECORD

• How do we reference fields in Java or C++?

Page 53: CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

Records• There are some similarities and differences between records

and arrays.

– Records are used when the data is of different types

– Arrays are used when the data is all the same type

– Record elements are referenced with static field names as “subscripts.”

– Array elements are referenced with dynamic subscripts.

• Implementation

– In the descriptor, the address of the record is stored as well as the name, type and offset address of each field.