Week 6 - Chapters 8 & 9 - Scalars, Lists, and Records

Theory

Data Types

There are a variety of different types, or arrangements of data:

In all of the above examples, these data types are well served by the principles of object-oriented programming, specifically, self-description and encapsulation of data and functions that act upon that data.

Operator Theory

Operators come in many flavors. Typically the thing that sets operators apart is that they are comprised of punctuation symbols, but this is not always the case, operators can be small alpha sequences that look a lot like functions (but really aren't).

Operator Categories

Operators are typically classified based on the number of operands that they act upon. An operand is an argument "passed to" an operator. (Think of it like a function.) There are some classifications of operators based on the number of operands they take:

Operator Notation

Let's delve into a little discussion on syntax. More specifically, let's discuss operator syntax. There are basically three positions for operators:

Trivia: LISP and it's variants (Scheme, Guile, etc.) ONLY support prefix operators. This means that if you perform an addition, you say (+ 1 2). This may seem a little awkward, but it can actually be cleaner than infix notation (see below) when you want to add together a list of items, i.e. (+ 1 2 5 7 18 20).

Ada Stuff

Chapter 8

Scalar Types: (p. 301) Goes over the basic ones. Mostly review here. The program listing and accompanying output on p. 303 will tell you how big Ada data types are and what their min / max values are.

Numeric Literals (p. 304) Ada has some cool features for doing literals. For starters, underscores can be used in place of commas in integer literals: 30_500 is valid. Also, integer literals can be written in nondecimal number system bases: the number 29 can be written as 2#11101# (binary) or 8#35# (octal). Kinda neat.

Numeric Operators (p. 304) This follows from the theory we talked about. Ada has 3 monadic operators (-, +, ABS) and 6 dyadic operators (+, -, *, /, **, REM). The remainder of the section goes into great detail explaining how expressions are evaluated in Ada. Feel free to skim this section.

Conversions / Casting: (p. 311) How to cast variables of one type to another. The code at the bottom is instructive. The white box on p. 312 entitled "Explicit Type Conversion" contains some pearls of wisdom.

Math Functions: (bottom of p. 315) Some elementary functions are conveniently found in the package Ada.Numerics.Elementary_Functions.

The USE directive (bottom of p. 316) helps you avoid the need to qualify all package references. It'ss handy so you don't have to write Ada.Numerics.Elementary_Functions (or any other package name) all over the place (see sample code p. 317). The white box at the bottom of p. 317 contains some valuable insigts on the propper use of USE. (Class exersize: show how to automatically switch between two interface-compatible packages.)

Random Numbers: (p. 326) and how to generate them. Sample code follows on p. 327-328.

[Boolean] Operators: (p. 329) Some are unary, some are dyadic. A nice truth table is found on p. 330. A table of operator precedence is found on p. 330 as well. The rest of the text in this section is more in-depth look at how Boolean expressions are evaluated. You are invited to skim this section.

Debugging with Boolean constants. (white box, p. 337) This is one of the oldest debugging tricks. You might find this handy. (In 'C' you do this with the preprocessor.)

Character variables (p. 340). A listing of the characters in the Ada Character type is found on p. 341. Also, look at p. 344 for an example of how to display non-printing control characters.

The CASE statement! (p. 347) You can use this in place of a big IF / ELSIF / ELSE construct. Note that CASE will only test equivalency. Syntax summary on p. 350. Note that you can use the list constructor operator (..) or 'or' operator (|) in the WHEN clauses, which neatly avoids the fall-through problem and need for explicit break statements (as in C, C++, Java, etc.).

Chapter 9

Records

These are "composite" types in Ada. Introduced on p. 368.

(To date, I have not found any Ada syntax that shows how records are self-describing, a la enumerations.)

Arrays

Introduced on (p. 385). This is the list data I was talking about in the theory section.

Assignment 5

Write a program which declares a record of two fields which will hold the components of a fractional number: The numerator (top part) and the denominator (bottom part). Make an array of three of these records. Initialize these three records with some values of your choosing. (You are not required to use the Ada.Numerics.Discrete_Random package, but you are welcome to experiment with it.)

Then, use a loop to print the values out to the screen in the form: "3/5". Following that, make another loop that multiplies the fractions together, then print the product. (You do not have to reduce the product to simplest terms.)

Output from the program should look like this:

fraction #1: 1/2
fraction #2: 3/5
fraction #2: 5/8
product: 15/80

I'm expecting to see you use the following keywords/constructs in your program: TYPE, SUBTYPE, ARRAY, FOR. Anything special that you use beyond that is gravy.

This assignment is due on October 31st, 2001, the night of the second exam.