Skip to content

Commit 0cfae80

Browse files
AntonisAkkostis
andauthored
[Dana] Add one correct and one erroneous program (#69)
* A correct IntXor program with input/output files * An erroneous program with accessing a variable out of scope --------- Co-authored-by: Kostis Sagonas <kostis@cs.ntua.gr>
1 parent 5ca72c5 commit 0cfae80

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# x is declared in the loop but called outside
2+
# the loop -> out of scope error
3+
4+
def main
5+
var i is int
6+
i := 10
7+
loop:
8+
var x is byte
9+
x := 'A'
10+
i := i - 1
11+
if i=0 : break
12+
x := 42

dana/programs/IntXor.dana

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# A (not so efficient) program that returns the XOR of 2 ints
2+
3+
def main:
4+
5+
# A binary number is represented with an array
6+
# where every bit is stored as an int
7+
8+
# Function definitions
9+
def DecToBin: n as int, N as int[15]
10+
var max i is int
11+
max := 16384
12+
i := 14
13+
14+
loop:
15+
N[i] := n / max
16+
n := n % max
17+
max := max / 2
18+
i = i - 1
19+
if i < 0: break
20+
21+
def BinToDec is int: N as int[15]
22+
var max i n is int
23+
max := 16384
24+
i := 14
25+
n := 0
26+
27+
loop:
28+
n := n + N[i] * max
29+
max := max / 2
30+
i = i - 1
31+
if i < 0: break
32+
return n
33+
34+
def XOR: A B X is int[15]
35+
var i is int
36+
i := 14
37+
38+
loop:
39+
X[i] := (A[i] + B[i]) % 2
40+
i = i - 1
41+
if i < 0: break
42+
43+
44+
# Main program
45+
var a b x is int
46+
var A B X is int[15]
47+
48+
# Read input
49+
writeString: "Input 2 non negative integers"
50+
a := readInteger()
51+
b := readInteger()
52+
53+
# Convert to binary
54+
DecToBin: a, A
55+
DecToBin: b, B
56+
57+
# Calculate XOR
58+
XOR: A, B, X
59+
60+
# Convert result to int
61+
x := BinToDec(X)
62+
63+
# Print result
64+
writeInteger: x

dana/programs/IntXor.input

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
10472 31738

dana/programs/IntXor.output

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
21266

0 commit comments

Comments
 (0)