(IX) Debug - Program Correction


1. Re-write the given program after correcting the bugs.
REM QBASIC program to find the sum of 7 and 10 START A=7 10=B SUM=A-B PRINT "Sum of two numbers is"; SUM THE END
Answer:
Error (bug) Correction
START CLS
10=B B=10
SUM=A-B SUM=A+B
THE END END

After correction the correct program is:
REM QBASIC program to find the sum of 7 and 10 CLS A = 7 B = 10 SUM = A + B PRINT "Sum of two numbers is"; SUM END
2. Re-write the given program after correcting the bugs.
REM to display the reverse of the number INPUT "Enter any number" ; N$ DO WHILE N$<>0 B=10 MOD N$ R=B*10+R N$=N$/10 LOOP PRINT "Reverse number is" ; R END
Answer:
Error (bug) Correction
INPUT ... N$ N
DO WHILE N$<>0 DO WHILE N<>0
B= 10 MOD N$ B=N MOD 10
R=B*10+R R=R*10+B
N$=N$/10 N=N\10

After correction the correct program is:
REM to display the reverse of the number INPUT "Enter any number" ; N DO WHILE N<>0 B=N MOD 10 R=R*10+B N=N\10 PRINT "Reverse number is" ; R END
3. Re-write the given program after correcting the bugs.
REM to print string in reverse order CLS INPUT " ENTER STRING " , W FOR I = LEN$(W$) TO 1 STEP -1 C$ = MID (W$, I, 1) R$=R$+C$ NEXT P PRINT " REVERSE ORDER ", C$ END
Answer:
Error(bug) Correction
INPUT ....W W$
LEN$ LEN
MID MID$
NEXT P NEXT I
PRINT C$ R$

After correction the correct program is:
REM to print string in reverse order CLS INPUT "ENTER STRING " , W$ FOR I = LEN(W$) TO 1 STEP -1 C$ = MID$ (W$, I, 1) R$=R$+C$ NEXT I PRINT " REVERSE ORDER", R$ END
4. Re-write the given program after correcting the bugs.
REM to print commission on the basis of sales amount INPUT "Enter the sales amount"; AMT SELECT CASE AMT CASE 2,000 TO 5,000 C=(AMT*10%) CASE 5001 TO 10,000 C=(AMT*15)/100 CASE IS >=10001 C=(AMT*100)/20 ELSE CASE PRINT "No commission" SELECT END END
Answer:
Error (bug) Correction
CASE 2,000 TO 5,000 CASE 2000 TO 5000
C=(AMT*10%) C=(AMT*10)/100
CASE 5001 TO 10,000 CASE 5001 TO 10000
C=(AMT*100)/20 C=(AMT*20)/100
ELSE CASE CASE ELSE
SELECT END END SELECT
After correction the correct program is:
REM to print commission on the basis of sales amount INPUT “Enter the sales amount”; AMT SELECT CASE AMT CASE 2000 TO 5000 C=(AMT*10)/100 PRINT C CASE 5001 TO 10000 C=(AMT*15)/100 PRINT C CASE IS >=10001 C=(AMT*20)/100 PRINT C CASE ELSE PRINT "No commission" END SELECT END
5. Re-write the given program after correcting the bugs.
REM to test whether the entered number is positive, negative or Zero INPUT “Enter a number” ; N$ S=SIGN(N$) SELECT CASE S CASE S PRINT "Number is positive" CASE -1 PRINT "Number is Negative" CASE 0 PRINT "Number is zero" SELECT END END
Answer:
Error (bug) Correction
INPUT .... N$ N
S=SIGN(N$) S=SGN(N)
CASE S CASE 1
CASE 0 CASE ELSE
SELECT END END SELECT

After correction the correct program is: INPUT “Enter a number” ; N S=SGN(N) SELECT CASE S CASE 1 PRINT "Number is positive" CASE -1 PRINT "Number is Negative" E ELSE PRINT "Number is zero" END SELECT END
  1. Re-write the given program after correcting the bugs.

REM to count vowels in a sentence

INPUT “Enter string ::::” ; S

L=LEN$(S)

FOR K = 1 TO L

            C$=MID$(S$,1,K)

            SELECT CASE C$

                        CASE A,E,I,O,U

                        COUNT=COUNT+1

            SELECT END

NEXT

PRINT “Supplied Sentence”; S$

PRINT “Total Vowels :::::;C

END

Answer:

Error (bug)

Correction

INPUT …S

S$

LEN$(S)

LEN(S$)

C$=MID$(S$,1,K)

MID$(S$,K,1)

CASE A,E,I,O,U

CASE “A”, “E”, “I”, “O” , “U”

SELECT END

END SELECT

NEXT

NEXT K

PRINT …C

COUNT

               

After correction the correct program is:

 

REM to count vowels in a sentence

INPUT “Enter string ::::” ; S$

L=LEN(S$)

FOR K = 1 TO L

            C$=MID$(S$,K,1)

            SELECT CASE C$

                        CASE “A”, “E” , “I” , “O” , “U”

                        COUNT=COUNT+1

            END SELECT

NEXT K

PRINT “Supplied Sentence”; S$

PRINT “Total Vowels :::::;COUNT

END