Skip to content

Commit

Permalink
[grace] Two array manipulating programs
Browse files Browse the repository at this point in the history
* Fix and extend `arrays.grc`
* Add `arraysum.grc` program from PR #25, slightly extended
* Add expected results for these two programs

Closes #24
Closes #25
  • Loading branch information
kostis committed Mar 26, 2024
1 parent da80200 commit a1e9b3c
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 19 deletions.
55 changes: 36 additions & 19 deletions grace/programs/arrays.grc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
$$
This grace program tests the utilization of array structures with multiple
dimensions as arguments, parameters, variables, l-values and r-values. It also
includes mildly complex arguments given to function calls and nested while
loops without blocks.
This program tests the use of array structures with multiple dimensions as
arguments, parameters, variables, l-values and r-values. It also includes
mildly complex arguments given to function calls and nested while loops
without blocks.
$$

fun main () : nothing
Expand Down Expand Up @@ -30,11 +30,16 @@ fun main () : nothing
i <- 0;
j <- 0;
k <- 0;
while i < 2 do
while j < 10 do
while k < 13 do
while i < 2 do {
while j < 10 do {
while k < 13 do {
b[i][j][k] <- chr((i + j + k) mod 26 + ascii('A'));

k <- k + 1;
}
j <- j + 1;
}
i <- i + 1;
}
return 0;
}

Expand All @@ -49,7 +54,6 @@ fun main () : nothing
s <- 11;
while i < s do {
x[i] <- a[i];

i <- i + 1;
}

Expand All @@ -58,30 +62,43 @@ fun main () : nothing
z[i][9] <- 0;
j <- 0;
while j < 9 do {
writeInteger(j);
z[i][j] <- 1;
j <- j + 1;
}
i <- i + 1;
}
writeString("\n");

i <- 0;
j <- 0;
s <- 10;
while i < 10 do {
while (z[j][i] # 0) do {
z[j][i] <- a[i];
j <- j + 1;
j <- 1;
while i < j and z[j][i] # 0 do {
writeInteger(j);
z[j][i] <- a[i];
j <- j + 1;
if j >= 5 then {
j <- 0;
i <- i + 1;
}
i <- i + 1;
}
writeString("\n");

i <- 0;
j <- 0;
k <- 0;
while i < 2 do
while j < 4 do
while k < 13 do
while i < 2 do {
while j < 4 do {
while k < 13 do {
if ascii(b[i][j][k]) >= 65 then
y[i][j][k] <- b[i][j][k];
writeInteger(j-k);
k <- k + 1;
}
j <- j + 1;
}
i <- i + 1;
}
writeString("\n");
}

{ $ main
Expand Down
4 changes: 4 additions & 0 deletions grace/programs/arrays.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
012345678012345678012345678012345678012345678
1234
0-1-2-3-4-5-6-7-8-9-10-11-12
Everything went OK!
108 changes: 108 additions & 0 deletions grace/programs/arraysum.grc
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
$$
This program tests passing of multi-dimensional arrays as parameters.

In the main program, the sum of a three dimensional array is calculated with
two midlly different ways, using the functions 'array1DSum' and 'array2DSum'.
$$

fun main() : nothing
var arr : int [2][3][4];
var i, j: int;
var sum : int [2];
var msg : char[128];

fun incr(ref x : int) : nothing {
x <- x + 1;
}

fun incrByStep(ref x : int; step : int) : nothing {
x <- x + step;
}

fun initializeArray(ref a : int[2][3][4]) : nothing
var n, i, j, k : int;
{
n <- 0;
i <- 0;
while i < 2 do {
j <- 0;
while j < 3 do {
k <- 0;
while k < 4 do {
a[i][j][k] <- n;
incrByStep(n, 8);
incr(k);
}
incr(j);
}
incr(i);
}
}

fun array1DSum(ref a : int[]; size : int) : int
var sum, i : int;
{ $array1DSum
i <- 0;
sum <- 0;
while i < size do {
sum <- a[i] + sum;
i <- i + 1;
}
return sum;
}

fun array2DSum(ref a : int[3][4]) : int
var sum, i, j : int;
{ $array2DSum
i <- 0;
sum <- 0;
while i < 3 do {
j <- 0;
while j < 4 do {
sum <- sum + a[i][j];
j <- j + 1;
}
i <- i + 1;
}

return sum;
}

{ $main

writeString("Initializing array... ");
initializeArray(arr);
writeString("done!\n");

writeString("Sum using the \'array1DSum\' function: ");
i <- 0;
sum[0] <- 0;
while i < 2 do {
j <- 0;
while j < 3 do {
sum[0] <- sum[0] + array1DSum(arr[i][j], 4);
j <- j + 1;
}
i <- i + 1;
}
writeInteger(sum[0]);
writeString("\n");

writeString("Sum using the \'array2DSum\' function: ");
i <- 0;
sum[1] <- 0;
while i < 2 do {
sum[1] <- sum[1] + array2DSum(arr[i]);
i <- i + 1;
}
writeInteger(sum[1]);
writeString("\n");

if sum[0] # sum[1] then
strcpy(msg, "Results differ!\n");
else
strcpy(msg, "Results are the same!\n");

writeString(msg);

}
4 changes: 4 additions & 0 deletions grace/programs/arraysum.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Initializing array... done!
Sum using the 'array1DSum' function: 2208
Sum using the 'array2DSum' function: 2208
Results are the same!

0 comments on commit a1e9b3c

Please sign in to comment.