Student Originated Software 1997-1998
Fall Quarter

A Software Engineering Course at
The Evergreen State College

Smalltalk Solutions for Week 3 assignment

Exercise 10.1

    | addBlock index |
    index := 0.
    addBlock := [index:= index+1].
    5 timesRepeat: [
      addBlock value.
      Transcript cr.
      Transcript show: 'index: ',index printString.
    ]

Exercise 10.4

| a b |
a:=0.
b:=0.
    100 timesRepeat:[a:= a+1. b:= b+a].
    Transcript cr; show: b printString.
| sum |
    sum := 0.
    1 to: 100 do: [:n |
      sum := sum + n.
    ].
    Transcript cr.
    Transcript show: 'sum: ',sum printString.

Exercise 10.6

| myArray index max |
    myArray := #(-10 2 3 4 -4).
    index := 1.
    max := myArray at:1.
    [(index+1) <= myArray size]
      whileTrue:
        [ index := index+1.
        (myArray at: index) > max
          ifTrue: [max := myArray at: index]].
max

| maxBlock max |
max := nil.
maxBlock := [:each |

    max isNil ifTrue: [
      max := each
    ] ifFalse: [
      each > max ifTrue: [ max := each].
    ]
]. #(3 2 7 5 8 1) do: maxBlock.
Transcript cr.
Transcript show: 'max: ',max printString.

Exercise 10.7

| myArray i j temp |
    myArray := #(1 2 3 4 5 6).
    i:=1.
    j := myArray size.
    (j//2) timesRepeat:
    [temp := myArray at:i.
    myArray at:i put: (myArray at:j).
    myArray at:j put: temp.
    i := i+1. j := j-1].
myArray
#(6 5 4 3 2 1)

| swapBlock reverseBlock array |
swapBlock := [:anArray :i : j |

    |tmp|
    i = j ifFalse: [
      tmp := anArray at: j.
      anArray at: j put: (anArray at: i).
      anArray at: i put: tmp
    ]

    reverseBlock := [:anArray |
      |i j|
      i := 1.
      j := anArray size.
      [
        swapBlock value: anArray value: i value: j.
        i := i +1.
        j := j - 1.
        i < j
      ] whileTrue.
    ].
array := #( 3 2 1 7 5 ).
reverseBlock value: array.
Transcript cr.
Transcript show: 'array: ', array printString.

Exercise 10.10

| counter |
counter := 0.
[counter < 10] whileTrue: [
    counter := counter + 1.
    Transcript cr.
    Transcript show: counter printString, ' squared: ',
    (counter * counter) printString.
    ].


For more information contact
[ Evergreen Home Page | Academic Programs ]


Created by: SoSwEbGrOuP
E-mail: ringert@evergreen.edu