Skip to content

Commit 2c09248

Browse files
authored
Fix syntax of two programs (powint and binarysearch) and add powint-erroneous (#71)
* Fix syntax of powint.dana Use"=" instead of "==" for equality and moved "var temp is int" declaration to the beginning of the block. The program still contains a semantic error so I copied it to the dana/programs-erroneous directory. * Fix syntax in binarysearch program Adjusted the bAdded a missing comma in a procedure call, changed function calls to use () instead of :, specifed a fixed size for the local array variable declaration, and corrected the assignment operator from "=" to ":=".
1 parent b2ca984 commit 2c09248

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
def main
2+
3+
def powint: base as int, exp as int, mod as int, result as ref int
4+
5+
base := base % mod
6+
var temp is int
7+
8+
if exp < 1:
9+
result := 1
10+
else:
11+
if exp % 2 = 0:
12+
powint: base, exp / 2, mod, temp # SEMANTIC ERROR: temp is not a ref variable but being passed to a ref parameter
13+
result := (temp * temp) % mod
14+
else:
15+
powint: base, exp - 1, mod, temp # SEMANTIC ERROR: temp is not a ref variable but being passed to a ref parameter
16+
result := (base * temp) % mod
17+
18+
var b is int
19+
var e is int
20+
var m is int
21+
var res is int
22+
23+
b := readInteger()
24+
e := readInteger()
25+
m := readInteger()
26+
27+
powint: b, e, m, res
28+
29+
writeInteger: res
30+
writeString: "\n"

dana/programs/binarysearch.dana

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ def main
1111
i := i + 1
1212
swap: arr[i], arr[j]
1313
j := j + 1
14-
swap: arr[i+1] arr[high]
14+
swap: arr[i+1], arr[high]
1515
return: (i+1)
1616

1717
def quicksort: arr as int [], low high as int
1818
var pi is int
1919
if low < high:
20-
pi := (partition: arr, low, high)
20+
pi := partition(arr, low, high)
2121
quicksort: arr, low, (pi - 1)
2222
quicksort: arr, (pi + 1), high
2323

@@ -40,7 +40,7 @@ def main
4040
high := mid - 1
4141
return: -1
4242

43-
var nums is int []
43+
var nums is int [100]
4444
var size k find is int
4545
k := 0
4646
writeString: "Give size of array: "
@@ -50,7 +50,7 @@ def main
5050
loop:
5151
if k <= size:
5252
nums[k] := readInteger()
53-
k = k + 1
53+
k := k + 1
5454
else:
5555
nums[k] := '\0'
5656
break
@@ -62,4 +62,4 @@ def main
6262
find := readInteger()
6363
writeString: "\n"
6464
writeString: "Value is: "
65-
writeInteger: (binarySearch: nums, 0, (size - 1), find)
65+
writeInteger: (binarySearch( nums, 0, (size - 1), find))

dana/programs/powint.dana

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ def main
33
def powint: base as int, exp as int, mod as int, result as ref int
44

55
base := base % mod
6+
var temp is int
67

78
if exp < 1:
89
result := 1
910
else:
10-
if exp % 2 == 0:
11-
var temp is int
11+
if exp % 2 = 0:
1212
powint: base, exp / 2, mod, temp
1313
result := (temp * temp) % mod
1414
else:
15-
var temp is int
1615
powint: base, exp - 1, mod, temp
1716
result := (base * temp) % mod
1817

0 commit comments

Comments
 (0)