Debug - Program Correction
1. REM Sub program to find average of three numbers
Declare
sub average (a, b, c)
Input
"Enter any three numbers" , a, b ,c
Call
sub average(a, b, c)
End
Sub
average(a, b, c)
av
=(a + b + c) ÷ 3
Display
"Average of three numbers is" ; av
Sub
End
Answer
|
Error
(bug) |
Correction |
|
Call
Sub average(a,b,c) |
Call
average(a,b,c) |
|
av
=(a + b + c) ÷ 3 |
av
=(a + b + c) / 3 |
|
Display
... |
Print
... |
|
Sub
End |
End
Sub |
After correction the correct program is:
REM
Sub program to find average of three numbers
Declare
sub average (a, b, c)
Input
"Enter any three numbers" , a, b ,c
Call average(a,
b, c)
End
Sub
average(a, b, c)
av =(a + b + c)
/ 3
Print "Average
of three numbers is" ; av
End Sub
2.
REM program to print 4 8 12 20 .... 20th
DECLARE
SUB Series()
CLS
EXECUTE
Series
END
SUB
Series terms
X
= 4
Y
= 4
FOR
ctr = 10 to 1
DISPLAY X;Y;
X=X+Y
Y=X+Y
Next
ctr
End
Series
Answer
|
Error
(bug) |
Correction |
|
Execute
Series |
CALL
Series |
|
For
Ctr= 10 to 1 |
For
ctr=10 to 1 step-1 |
|
Display
X;Y; |
PRINT
X;Y; |
|
End
Series |
END
SUB |
After correction the correct program is:
DECLARE
SUB Series()
CLS
CALL Series
END
SUB
Series
X
= 4
Y
= 4
FOR
ctr = 10 to 1 step-1
PRINT X;Y;
X=X+Y
Y=X+Y
Next
ctr
END SUB
3.
REM to print sum of two numbers
FUNCTION
SUM(M ,N)
A=6
B=7
DISPLAY
sum(M ,N)
END
FUNCTION
SUM(M,N)
S=
M + N
S=SUM
END
FUNCTION
Answer:
|
Error
(bug) |
Correction |
|
FUNCTION
SUM(M,N) |
DECLARE
FUNCTION SUM(M,N) |
|
Display
SUM(M,N) |
Print
SUM(A,B) |
|
S=SUM |
SUM=S |
After correction the correct program is:
DECLARE
FUNCTION SUM(M ,N)
A=6
B=7
PRINT SUM(A,B)
END
FUNCTION SUM(M,N)
S=M
+ N
SUM=S
END
FUNCTION
4.
REM to find factorial of a given number
DECLARE
FUNCTION FACTO (N$)
CLS
INPUT
"Enter a number" ; X
PRINT
“The factorial is “; FACTO(N)
END
FUNCTION
FACTO(N)
F=1
WHILE
N=0
F=F*N
N=N-1
WEND
F=FACTO
END
FUNCTION
Answer:
|
Error
(bug) |
Correction |
|
Declare
....(N$) |
Declare...(N) |
|
PRINT
...FACTO(N) |
PRINT
...FACTO(X) |
|
WHILE
N=0 |
WHILE
N<>0 |
|
F=FACTO |
FACTO=F |
After correction the correct program is:
DECLARE
FUNCTION FACTO (N)
CLS
INPUT
"Enter a number" ; X
PRINT “The
factorial is “; FACTO(X)
END
FUNCTION
FACTO(N)
F=1
WHILE N<>0
F=F*N
N=N-1
WEND
FACTO = F
END
FUNCTION
5.
REM to add record in an existing file.
CLS
Open
"Record.dat" for output as #1
AA:
Input
“Enter Name, Class and Roll No” ; Nm$, Cl , Rn
Input
#2, Nm$, Cl , Rn
Input
"more records"; y$
If
ucase$(y$)= "Y" then goto AA
Close
"Record.dat"
End
Answer:
|
Error(bug) |
Correction |
|
OUTPUT |
APPEND |
|
Input
#2, Nm$, Cl , Rn |
Input
#1, Nm$, Cl , Rn |
|
Close
"Record.dat" |
Close
#1 |
After
correction the correct program is:
CLS
Open
"Record.dat" for append
as #1
AA:
Input “Enter
Name, Class and Roll No” ; Nm$, Cl , Rn
Input
#1, Nm$, Cl , Rn Input "more records"; y$
If
ucase$(y$)= "Y" then goto AA
Close #1 ‘
End
6.
REM to store in a sequential data file "STD.DOC"
OPEN
STD.DOC FOR OUT AS #1
INPUT
"ENTER NAME" ; N
INPUT
"ENTER AGE";A
WRITE
1,N$,A
CLOSE
#1
END
Answer:
|
Error
(bug) |
Correction |
|
STD.DOC |
"STD.DOC" |
|
OUT |
OUTPUT |
|
INPUT
"ENTER NAME" ; N |
INPUT
"ENTER NAME" ; N$ |
|
WRITE
1,N$,A |
WRITE
#1,N$,A |
After
correction the correct program is:
OPEN
"STD.DOC" FOR OUTPUT AS #1
INPUT
"ENTER NAME" ; N$
INPUT
"ENTER AGE";A
WRITE #1,N$,A
CLOSE
#1
END
7.
REM to create a new data file
CLS
OPEN
"ABC.DAT" FOR INPUT AS #1
DO
INPUT "Enter Name, Roll No and
Total"; N$,R,T
INPUT #1, N$, R, T
INPUT "Supply more records
Y/N";Ch$
LOOP
WHILE UCASE(Ch$)="Y"
CLOSE
#1
END
Answer:
|
Error
(bug) |
Correction |
|
OPEN
"ABC.DAT" FOR INPUT AS #1 |
OPEN
"ABC.DAT" FOR OUTPUT AS #1 |
|
INPUT
#1, N$, R, T |
WRITE
#1, N$, R, T |
|
LOOP
WHILE UCASE(Ch$)="Y" |
LOOP
WHILE UCASE$(ch$)="Y" |
After
correction the correct program is:
CLS
OPEN
"ABC.DAT" FOR OUTPUT AS #1
DO
INPUT
"Enter Name, Roll No and Total"; N$,R,T
WRITE #1, N$, R, T
INPUT "Supply more records
Y/N";Ch$
LOOP WHILE
UCASE$(Ch$)="Y"
CLOSE
#1
END
8.
Rem To print only class 10 record from "student.dat"
CLS
OPEN
"I",#2,"student.dat"
WHILE NOT EOF (#2)
WRITE #2, N$,C,R
IF C=10 THEN
DISPLAY N$,C,R
END IF
NEXT
CLOSE
#2
END
Answer:
|
Error
(bug) |
Correction |
|
WHILE
NOT EOF (#2) |
WHILE
NOT EOF (2) |
|
WRITE
#2, N$,C,R |
INPUT
#2, N$,C,R |
|
DISPLAY
N$,C,R |
PRINT
N$,C,R |
|
NEXT |
WEND |
After
correction the correct program is:
Rem
To print only class 10 record from "student.dat"
CLS
OPEN
"I",#2,"student.dat"
WHILE NOT EOF
(2)
INPUT
#2, N$,C,R
IF C=10 THEN
PRINT N$,C,R
END IF
WEND
CLOSE
#2
END
10.
REM program to read data from the data file.
OPEN
"STD.DAT" FOR OUTPUT AS # 1
CLS
WHILE
NOT EOF (#1)
WRITE #1, N$,R, C, P
PRINT N$, R,C, P
WEND
CLOSE
"STD.DAT"
END
Answer:
|
Error
(bug) |
Correction |
|
OPEN
....OUTPUT |
INPUT |
|
WHILE....(#1) |
WHILE
NOT EOF(1) |
|
WRITE
#1,... |
INPUT
#1,.... |
|
CLOSE
"STD.DAT" |
CLOSE
#1 |
After
correction the correct program is:
OPEN
"STD.DAT" FOR INPUT
AS # 1
CLS
WHILE NOT EOF
(1)
INPUT
#1,N$, R, C, P
PRINT N$, R,C, P
WEND
CLOSE #1
END
11.
REM TO delete a record from a data file
OPEN
"STD.DAT" FRO INPUT AS #1
OPEN
"TEMP.DAT" FOR OUTPUT AS #2
INPUT
"ENTER ROLL NUMBER TO DELETE";R
DO
WHILE NOT EOF(1)
INPUT #1,R,N$,C
IF R<>RN THEN
WRITE #2,N$,C
END IF
LOOP
CLOSE
#1
KILL
"STD.DAT" NAME "TEMP.DAT" AS "SECRET.DAT"
END
Answer:
|
Error(bug) |
Correction |
|
INPUT
#1,R,N$,C |
INPUT
#1,RN,N$,C |
|
WRITE
#2,N$,C |
WRITE
#2,RN,N$,C |
|
CLOSE
#1 |
CLOSE
#1,#2 |
|
NAME
"TEMP.DAT" AS "SECRET.DAT" |
NAME
"TEMP.DAT" AS "STD.DAT" |
After
correction the correct program is:
OPEN
"STD.DAT" FRO INPUT AS #1
OPEN
"TEMP.DAT" FOR OUTPUT AS #2
INPUT
"ENTER ROLL NUMBER TO DELETE";R
DO
WHILE NOT EOF(1)
INPUT
#1,RN,N$,C
IF R<>RN THEN
WRITE #2,RN,N$,C
END IF
LOOP
CLOSE #1,#2
KILL
"STD.DAT" NAME
"TEMP.DAT" AS "STD.DAT"
END
12.
DECLARE
FUNCTION reverse$(N$)
INPUT
"Any string";N$
X$=reverse$(N$)
PRINT
N$
END
FUNCTION
reverse(N$)
L=LEN$(N$)
FOR
X=L TO 1 STEP-1
A$=MID$(N$,X,1)
B$=B$+A$
NEXT
X
B$=reverse$(N$)
END
FUNCTION
Answer:
|
Error(bug) |
Correction |
|
PRINT
N$ |
PRINT
X$ |
|
FUNCTION
reverse(N$) |
FUNCTION
reverse$(N$) |
|
L=LEN$(N$) |
L=LEN(N$) |
|
B$=reverse$(N$) |
reverse$=B$ |
After
correction the correct program is:
DECLARE
FUNCTION reverse$(N$)
INPUT
"Any string";N$
X$=reverse$(N$)
PRINT X$
END
FUNCTION
reverse$(N$)
L=LEN(N$)
FOR
X=L TO 1 STEP-1
A$=MID$(N$,X,1)
B$=B$+A$
NEXT
X
reverse$=B$
END
FUNCTION
13.
DECLARE
SUB SUM (N)
INPUT
"Any Number"; N
PRINT
SUM (N)
END
SUB
SUM (N)
S
= 0
WHILE
N = 0
R = R MOD 10
S = S+R
N = N/10
WEND
PRINT
"Sum of digits"; s
END
Answer:
|
Error(bug) |
Correction |
|
PRINT
SUM (N) |
CALL
SUM (N) |
|
WHILE
N =0 |
WHILE
N<>0 |
|
R
= R MOD 10 |
R
= N MOD 10 |
|
N=N/10 |
N=N\10 |
After
correction the correct program is:
DECLARE
SUB SUM (N)
INPUT
"Any Number"; N
CALL SUM(N)
END
SUB
SUM (N)
S
= 0
WHILE N <>
0
R = N MOD 10
S = S + R
N = N\10
WEND
PRINT
"Sum of digits"; S
END
SUB