

TOPIC B: Arrays.
Contents :
ARRAY of CARDINAL, CONST.
Standards:
indentation, naming!
Exercises :
D1: Fill an array
open Modula2 compiler XDS: start -> programs -> [TW]
Programming
->
XDS
environment
copy the
to a new file (remember: same name as module!), run it and try to
understand
it...
- Define an array of 10 integers. Fill it with 1's and print the
array.
- Fill the array with its indices. Use a constant for the array
size.
Change
the value of the constant to 20.






D2: Draw Blocks
open Modula2 compiler XDS: start -> programs -> [TW]
Programming
->
XDS
environment
copy the
to a new file, run it and try to understand it...
- Dit programma toont hoe je graphics kan maken in een apart
graphics
venter.
Het window-gedeelte aan het begin en het press-key gedeelte aan het
einde
zal je steeds nodig hebben.
- Dit programma toont hoe je een figuur kunt laten 'verdwijnen', je
wacht
even en maakt het dan weer zwart (de achtergrondkleur).
The Same In English:
- This program shows you how to draw graphis in a seperate graphics
window.
Press Enter to finish the program.
- Create 'movement' by adding a delay and making the figures black
again.






D3:






S1: Array with the powers of 2.
start with the solution of D1
Part2
- Fill the array with the powers of -2, without using the power
procedure!
Print the array.
- Calculate the average of all elements of the array. Print it.
- Change the array size to 20, does your program still work?





S2:Curve vorm
Start met dit
programma.
Gegeven is een functie, waarvan de eerste 10 punten in een array staan.
Gevraagd:
- Zoek de grootste waarde van de array en print deze af.
- test uw code voor ARRAY_SIZE = 20, het moet blijven
werken!
- Wijzig in de gegeven code hetvolgende:
f := (f + (f)*4/15 + 1) MOD 40;
(* f := f + (20-f) * ABS(f) / 7; *) (* another function *)
y_values[i] := f;
Naar:
f := (f + (f)*4/15 + 1) MOD 40;
(* f := f + (20-f) * ABS(f) / 7; *) (* another function *)
b:=0-f;
y_values[i] := b;
Vergeet ook niet om b te declareren!(INTEGER)
- Print de elementen van de array af, zolang de waarden stijgen
(zolang
de
functie die y_values[i] voorstelt, monotoon stijgend is).
- test uw code voor ARRAY_SIZE = 20, het moet blijven
werken!





S3: Array Reversal
- Start with the array of the previous
exercise.
- Reverse the array: the first element becomes last, etc.
- Print the array
- Reverse the array by copying it into a new array arr2
- If possible, reverse the elements within the array, thus without
copying
it into array arr2! You may only use one extra INTEGER variable.





S4: Plot Functie
Bestudeer eerst D1.
Teken de functie mbv de waarden die we in de array y_values
gestoken hebben. Start met
deze code
(= combinatie S2 & D2).
- Teken een lijn van cirkels met straal SIZE
mbv. de array: neem als x-waarde de index van de array en als y-waarde
de waarde van de array, vermenigvuldig beiden met SIZE
om de tekening te vergroten.
- Creeer beweging met een cirkel die de vorm van de curve volgt.
Doe dit
mbv een delay en het zwart maken van de laatste cirkel zoals uitgelgd
in D2 part2.





S5: Chessboard (Schaakbord)
Use D2 as a start.
- Draw a 8 by 8 chess board with 2 colours different from black.
Vraag: kan je op een eenvoudige manier je code veranderen en er een
dambord van maken? Kan je gemakkelijk de grootte van elk vakje
aanpassen?
(gebruik van constantes)






S6: Moving figures
Start with D2 part b.
- Get a circle that moves from down left to upper right
- Add a circle that moves from upper left to down right (forming a
'cross').
- Make an 'O' (een ruit) form with 1 moving circle
- Moeilijk: zorg dat ie blijft ronddraaien, maar steeds in
kleiner wordende
ruiten...
Follow your creativity to create more figures... Let also the colour
change
from one circle to another.
Examples:





H1: Fill array with the fibonacci numbers
- Define an array of 10 cardinals. Fill it with the Fibonacci
numbers
(see
topicA#H2). They are defined as a sequence of numbers:
- N<1> = 1, N<2> = 1
- N<i> = N<i-1> +
N<i-2> for
i > 2
- Print the array.
- Ask the user for a certain element of the array and print it out.
- Ask the user for the 16th element. What happens?
- Calculate the sum and the average of the 10 first Fibonacci
Numbers.
X1: Zero
- Define three integer arrays of size 10 (use a constant!).
- Fill the first array with the powers of 3, starting with 3 ^ 0, 3
^ 1,
....
- ^ is not the power in Modula-2
- do it without a power procedure, calculate the powers yourself
- Fill the second according to the following rule:
- the first element is 1.
- the next elements is the sum of the previous elements,
multiplied by 2,
plus 1
- N<i> = (N<1> + N<2> + ... + N<i-1>) *
2 +
1
for i > 1
- The third array is the first subtracted by the second array.
Print the
resulting array.
Remark: Are the values of the resulting array correct?





T1: Output
- What is the output of the following program?
.....







