Files
TDT4205/ps6/vsl_programs/ps6-codegen2/find.vsl
T
2026-04-28 15:27:55 +02:00

63 lines
1.4 KiB
Plaintext

var pos
var array[100]
func main(value) {
initialize()
find(value)
}
// Fills the array with initial values in a pseudo-random way
// Works by incrementing array elements until one of the elements
func initialize() {
var i = 0, j = 0
var index
println("Filling up array with \"random\" values")
while (1) {
i = i * 21 + i / 31 + 7
j = j * 16 + j / 13 + 18
index = i + j
index = index < 0 ? 1 - index : index
// Turn the numbers into an index in the range [0,99]
index = index - index / 100 * 100
// Increment the array at the given index, stop once a 10 is written
array[index] = array[index] + 1
if (array[index] >= 10)
break
}
}
// Starts looking for an array element with the given value, starting at "pos"
// If a match is found, 1 is returned, and pos is the index of the match
func nextPos(value) {
while (pos < 100) {
if (array[pos] == value)
return 1
pos = pos + 1
}
return 0
}
func find(value) {
println("Looking for all occurances of ", value)
pos = 0
print("Found at:")
while (nextPos(value)) {
print(" ", pos)
pos = pos + 1
}
println("")
println("Done!")
}
//TESTCASE: 3
//Filling up array with "random" values
//Looking for all occurances of 3
//Found at: 0 2 9 23 26 36 42 50 64 71 72 78 80 87 95
//Done!