Notes CS 430 – 10.12.2006 3:30 class – thanks once more to Alan Crouch

 

READ YOUR BOOK WHEN TOLD!

 

How does the machine know where the element is?

 

No matter what you do, the program has to compute the location of the element you asked for.  When you create an array, what the program knows is the base address (first element). 

 

Num [1..10]

 

 

 

 

 

 

 

 

 

 

 

BASE

ADDRESS

 

It needs to know the size of the element.

Characters are likely to be 1 byte (sometimes more for Unicode). 

Integers vary by platform but are generally 4 bytes. 

Floating Points are generally 8 bytes.

 

It needs to know how many elements to skip over to get to the element you want.

 

Where can arrays start (lower bound)?

 

Java – 0           FORTRAN – 1            Pascal – Wherever you want (for example -2)

 

How do we get our formula for a 1-dimensional array?

 

BaseAddress + (index – LowerBound) * size =  Location

 

Example:

Size of an element = 4

Total elements = 9

BaseAddress = 4200

Want element with index 4  

 

FORTRAN:  4200 + (4 – 1) * 4 = 4212

JAVA or C: 4200 + (4 – 0) * 4 = 4216

Pascal or Ada: 4200 + (4 - -2) * 4 = 4224

 

 

 

 

Time for a 2-dimensional array!!!

 

 Things to know: base address, element size, row/column major order

 

     0                                         1                      2                                  3                      4

0

 

 

 

 

 

1

 

 

 

 

 

2

 

 

 

*

 

3

 

 

 

 

 

 

Row Major Order!

 

BaseAddress = 4200

ElementSize = 2

SizeOfRow  (num of columns in a row) = UpperBoundColsLowerBoundcols + 1

Number of Rows to Skip = RowIndexLowerBoundRow

Number of Columns to Skip = ColIndexLowerBoundCols

 

BaseAddress + (UBC – LBC + 1) * 2 * (RI – LBR) + (CI – LBC) * 2 = Location

 

Fortran: 4200 + 5 * 2 * 2 + 3*2 = 4226

Ada : 4200 + 5 * 2 * 2 + 3* 2 = 4226

 

Homework:  work out the formula for column major order

 

                   Understand  THIS BECAUSE IT WILL BE ON THE EXAM!!!!     !

 

 

 

SNOBOL 4   -  a language good for String processing.

Homework:

  • Download Snobol4p.zip from the first URL under Snobol4 specified on the Useful URLs page
  • Download Vanilla.zip from the same site
  • Unzip them both
  • Copy the three Demo files from the unzipped Vanilla directory to the unzipped Snobol4p directory.
  • Run the Demo.bat file from the Snobol4p directory
  • Make sure you understand it. – it will take you longer than the 10 minutes they say.

 

Below is material from running the Demo.bat file with some additional explanations.

SNOBOL is dynamically typed (i.e. no declarations)

Will convert strings to numbers if asked to add

 

 

Example assignment statements:

note that:  = is the assignment  operator

note that: Score changed its type from integer variable to string variable and back again).

Game = 5

Score = “Love”

Score = 15

Score = Score + “30”

(Score ß 45)

 

Concatenation is done by leaving a space between elements

Text = “Troy” “Herndon” “Smiles”

(Text ßTroyHerndonSmiles”)

Text = “The score in the game number “ Game “ is “ Score

(Text ß “The score in the Game number 5 is 45”)

 

Pattern Matching – Read about it in the demo.  You’ll see it when you run the Demo.bat file

Very Important to read (much like the aforementioned, reading of your book)

 

In the line below, Colors is a PATTERN.  The vertical bar is an OR

Colors = “red” | “green” | “blue”

 

In the example below, turn at the red house is the SUBJECT, Colors is a PATTERN and Result is a variable which will hold whatever part of the PATTERN was first found in the SUBJECT.  The . between Colors and Result causes the assignment to happen.

turn at the red house” Colors . Result

(Result ß “red”)

 

Instructions is a SUBJECT string

Instructions = “turn at the red house”

 

In this example,  the first part of the PATTERN Colors found in the SUBJECT Instructions is replaced by BIG (whatever follows the = sign).

Instructions Colors = “BIG”

(Instructions ß “turn at the BIG house”)

 

Homework:  Programming problem assigned in the Lecture 13 notes is due on Tuesday, Oct 17th at the start of class.  Submit only the Pascal source as  yourname5.pp and bring printouts of your source code and screen output (showing your keyboard input data and your screen output) to class.  NOTE:  if you prefer having an input file and an output file using DOS redirection, you can bring the input file and output file printouts instead.)