Since i am going to have to learn Smalltalk well enough to convert it to .NET ( or so i have been told )

i decided to convert some of the C++ tutorials to Smalltalk.

Namely this one:

Write a program which performs addition, subtraction, multiplication of matrices. The dimensions of both the matrices would be specified by the user (dynamic memory allocation required). Use of structure or a class to define the matrix would be a good idea. (Expert)

Here is the output,

in its current state it can only add matrices but i will manage that later on.

I would have loved to uses classes and methods like in any modern language but since the version we are using here at work is about 12 years old and adding support for classes is cryptic at best i will simply hold out for now and submit my sad version :lol:

"
 Adding matrices together
"
| matriceA matriceB matriceResult matriceHeight matriceWidth mCountH mCountW mMessage |
 Transcript clear.
 
 "setup the initial heights and widths"
 matriceHeight := 0.
 matriceWidth := 0.
 [ ( ( matriceHeight > 0 ) & ( matriceWidth > 0 ) ) ] whileFalse: [
  matriceHeight := ( Dialog request: 'Please enter the height of the matrices' initialAnswer: 2 ) asNumber.
  matriceWidth := ( Dialog request: 'Please enter the width of the matrices' initialAnswer: 2 ) asNumber.
 ].
 
 "Create a collection at MatriceA, this collection is matriceHeight in size with each item being matriceWidth in size"
 matriceA := OrderedCollection new.
 matriceB := OrderedCollection new.
 matriceResult := OrderedCollection new.
 "used to count through the array and build elements"
 mCountH := 1.
 mCountW := 1.
 [ mCountW <= matriceWidth ] whileTrue: [
  "declare the jagged array"
  matriceA add: ( OrderedCollection new ).
  matriceB add: ( OrderedCollection new ).
  matriceResult add: ( OrderedCollection new ).
  mCountW := mCountW + 1.
 ].
 "reset counters for later use"
 mCountW := 1.
 "request users to fill in matrice A"
 [mCountW <= matriceWidth ] whileTrue: [
  [mCountH <= matriceHeight ] whileTrue: [
   ( matriceA at: mCountW ) add: 
     ( (Dialog request: 'MATRICE A' cr, 'please fill in the value at ' cr, mCountW printString, ',', mCountH printString initialAnswer: 0) asNumber ).
   mCountH := mCountH + 1.
  ].
  mCountH := 1.
  mCountW := mCountW + 1.
 ].
 mCountW := 1.
 "request users to fill in matrice B"
 [mCountW <= matriceWidth ] whileTrue: [
  [mCountH <= matriceHeight ] whileTrue: [
   ( matriceB at: mCountW ) add: 
     ( (Dialog request: 'MATRICE B' cr, 'please fill in the value at ' cr, mCountW printString, ',', mCountH printString initialAnswer: 0) asNumber ).
   mCountH := mCountH + 1.
  ].
  mCountH := 1.
  mCountW := mCountW + 1.
 ].
 mCountW := 1.
 mCountH := 1.
 
 "do math to add matrices"
 [ mCountW <= matriceWidth ] whileTrue: [
  [ mCountH <= matriceHeight ] whileTrue: [
   ( matriceResult at: mCountW ) add: ( ( ( matriceA at: mCountW ) at: mCountH )  + ( ( matriceB at: mCountW ) at: mCountH ) ).
    mCountH := mCountH + 1.
  ].
  mCountH := 1.
  mCountW := mCountW + 1.
 ].
 mCountH :=1.
 mCountW :=1.
 "Build message to draw Matrice"
 mMessage := 'The Matricies to be added:'cr.
 [ mCountH <= matriceHeight ] whileTrue: [
  [ mCountW <= matriceWidth ] whileTrue: [
   ( mCountW = 1 ) ifTrue: [
    mMessage := mMessage, '| ', ( ( matriceA at: mCountW ) at: mCountH ) printString.
   ] ifFalse: [
    ( mCountW = matriceWidth ) ifTrue: [
     mMessage := mMessage , ' , ', ( ( matriceA at: mCountW ) at: mCountH ) printString, ' |' tab.
    ] ifFalse: [
     mMessage := mMessage , ' , ', ( ( matriceA at: mCountW ) at: mCountH ) printString.
    ].
   ].
   mCountW := mCountW + 1.
  ].
  mCountW := 1.
  [ mCountW <= matriceWidth ] whileTrue: [
   ( mCountW = 1 ) ifTrue: [
    mMessage := mMessage, '| ', ( ( matriceB at: mCountW ) at: mCountH ) printString.
   ] ifFalse: [
    ( mCountW = matriceWidth ) ifTrue: [
     mMessage := mMessage , ' , ', ( ( matriceB at: mCountW ) at: mCountH ) printString, ' |' cr.
    ] ifFalse: [
     mMessage := mMessage , ' , ', ( ( matriceB at: mCountW ) at: mCountH ) printString.
    ].
   ].
   mCountW := mCountW + 1.
  ].  
  mCountW := 1.
  mCountH := mCountH + 1.
 ].
 Dialog warn: mMessage.
 
 "reset the counters"
 mCountW := 1.
 mCountH := 1.
 
 mMessage := 'And the Result is:'cr.
 [ mCountH <= matriceHeight ] whileTrue: [
  [ mCountW <= matriceWidth ] whileTrue: [
   ( mCountW = 1 ) ifTrue: [
    mMessage := mMessage, '| ', ( ( matriceResult at: mCountW ) at: mCountH ) printString.
   ] ifFalse: [
    ( mCountW = matriceWidth ) ifTrue: [
     mMessage := mMessage , ' , ', ( ( matriceResult at: mCountW ) at: mCountH ) printString, ' |' cr.
    ] ifFalse: [
     mMessage := mMessage , ' , ', ( ( matriceResult at: mCountW ) at: mCountH ) printString.
    ].
   ].
   mCountW := mCountW + 1.
  ].
  mCountW := 1.
  mCountH := mCountH + 1.
 ].
 
 Dialog warn: mMessage.

The language does a good job at over complicating itself in it's current state.

To even save a file is a hassle.

One must copy what they are working on from their "workspace" open up the file browser, type in the location of the directory in which you wish to save the file, then you must paste the contents of your file into the browsers lower pane and middle+click save as and give the directory name / file name.st and save.

which smalltalk are u using ?

which smalltalk are u using ?

VisualWorks

www.cincomsmalltalk.com

At work we are using version 2.5.1 even though they are currently on version 7+

To write a method you do not need to use workspace. You do write code in lower pane of Browser itself and do accept to save it. And you do not need to save files individually. Just do Save Image.
You need to file-out the files only when you need to have a backup of a single method or a class only.

It is completely diffrent kind of IDE from the regular onces like Eclipse, Netbeans, Visual Studio.

Have you read the tutorials which comes with installation package ?
Also there are lots of free books for Smalltalk on http://www.iam.unibe.ch/~ducasse/FreeBooks.html

actually have a handfull of those books at work. the only important ones to use are from visual works or just smalltalk80 books because of the age of the distro we are using.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.