Skip to content

Commit

Permalink
Some more correct programs for grace
Browse files Browse the repository at this point in the history
  • Loading branch information
kostis committed May 19, 2024
1 parent 111dabb commit 217d986
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 1 deletion.
1 change: 0 additions & 1 deletion grace/programs/arraysum.grc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ fun main() : nothing
}
i <- i + 1;
}

return sum;
}

Expand Down
24 changes: 24 additions & 0 deletions grace/programs/fibonacci.grc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
fun main() : nothing

fun fibo(x : int) : int
{
if (x = 0) then return 0;
if (x = 1) then return 1;
return fibo(x-1) + fibo(x-2);
}

var N,limit : int;

{
limit <- 21;
N <- 0;
while (N <= limit)
do {
writeString("fibo(");
writeInteger(N);
writeString(") = ");
writeInteger(fibo(N));
writeString("\n");
N <- N+1;
}
}
22 changes: 22 additions & 0 deletions grace/programs/fibonacci.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
fibo(0) = 0
fibo(1) = 1
fibo(2) = 1
fibo(3) = 2
fibo(4) = 3
fibo(5) = 5
fibo(6) = 8
fibo(7) = 13
fibo(8) = 21
fibo(9) = 34
fibo(10) = 55
fibo(11) = 89
fibo(12) = 144
fibo(13) = 233
fibo(14) = 377
fibo(15) = 610
fibo(16) = 987
fibo(17) = 1597
fibo(18) = 2584
fibo(19) = 4181
fibo(20) = 6765
fibo(21) = 10946
78 changes: 78 additions & 0 deletions grace/programs/mergesort.grc
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
fun main () : nothing
var storage : int[16];

fun mergeSort(ref arr : int[]; l : int; r : int) : nothing
fun merge(ref arr : int[]; l : int; m : int; r : int) : nothing
var i, j, tmp, counter : int;
{
counter <- l;
while (counter <= r) do {
storage[counter] <- arr[counter];
counter <- counter+1;
}
counter <- l;
i <- l;
j <- m+1;
while (i <= m and j <= r) do {
if (storage[i] < storage[j]) then {
arr[counter] <- storage[i];
i <- i+1;
} else {
arr[counter] <- storage[j];
j <- j+1;
}
counter <- counter+1;
}
while (i <= m) do {
arr[counter] <- storage[i];
i <- i+1;
counter <- counter+1;
}
while (j <= r) do {
arr[counter] <- storage[j];
j <- j+1;
counter <- counter+1;
}
}
var m : int;
{
if (l < r) then {
m <- l + ((r - l) div 2);

$ Sort first and second halves
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);

merge(arr, l, m, r);
}
}

fun writeArray (ref msg : char[]; n : int; ref x : int[]) : nothing
var i : int;
{
writeString(msg);
i <- 0;
while i < n do {
if i > 0 then writeString(", ");
writeInteger(x[i]);
i <- i+1;
}
writeString("\n");
}

var seed, i, N : int;
var x : int[421742];

{ $ main
N <- 16;
seed <- 65;
i <- 0;
while i < 16 do {
seed <- (seed * 137 + 221 + i) mod 101;
x[i] <- seed;
i <- i+1;
}
writeArray("Initial array: ", N, x);
mergeSort(x, 0, N-1);
writeArray("Sorted array: ", N, x);
} $ main
2 changes: 2 additions & 0 deletions grace/programs/mergesort.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Initial array: 36, 3, 28, 20, 36, 7, 75, 100, 92, 7, 79, 46, 71, 63, 79, 50
Sorted array: 3, 7, 7, 20, 28, 36, 36, 46, 50, 63, 71, 75, 79, 79, 92, 100
41 changes: 41 additions & 0 deletions grace/programs/swap.grc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
fun main() : nothing

fun swap(ref x, y : int) : nothing
var temp : int;
{
temp <- x;
x <- y;
y <- temp;
}

fun swap_2(ref x : int[]) : nothing
{
swap(x[0], x[1]);
}

var x : int[2];

{
x[0] <- 17;
x[1] <- 42;
writeString("x[0] = ");
writeInteger(x[0]);
writeString("\n");
writeString("x[1] = ");
writeInteger(x[1]);
writeString("\n\nSWAPPP\n");
swap(x[0],x[1]);
writeString("\nx[0] = ");
writeInteger(x[0]);
writeString("\n");
writeString("x[1] = ");
writeInteger(x[1]);
writeString("\n\nSWAPPP\n");
swap_2(x);
writeString("\nx[0] = ");
writeInteger(x[0]);
writeString("\n");
writeString("x[1] = ");
writeInteger(x[1]);
writeString("\n");
}
12 changes: 12 additions & 0 deletions grace/programs/swap.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
x[0] = 17
x[1] = 42

SWAPPP

x[0] = 42
x[1] = 17

SWAPPP

x[0] = 17
x[1] = 42

0 comments on commit 217d986

Please sign in to comment.