Tuesday, December 6, 2016

SOLVED QBASIC SET G AND H

1a) Define loop and nested loop.
b) Write down the output of the following program.

CLS
FOR B = 1 TO 5
READ N$
M$ = M$ + MID$(N$, B, 1)
NEXT B
PRINT M$
DATA NATURE, TECHNO, HAPPY, GOPAL, PUPILS
END

c) Re-write the following program correcting the bugs:
            REM to check if a word is palindrome
INPUT N$
FOR K = LEN(N$) TO 1
W$ = S$ + MID$(N$, K, 1)
NEXT K
IF N$ = W$ THEN
PRINT "Palindrome"
OTHERWISE
PRINT "Not Palindrome"
END


d) Re-write the following program using DO……..LOOP UNTIL
            CLS
FOR J = 20 TO 2 STEP -2
S = S + J
NEXT J
PRINT "Sum of even numbers"; S
END



e) Write a program using FUNCTION….END FUNCTION statement to display the longest string among three input strings.

f) A sequential data file “EXAM.DAT” contains name, address, phone number and age of some people. Write a program to display only those records which contains age above 18 and address is ‘Lumbini’.


Answers SET G
1a) Repeating statements or statement blocks for certain number of times depending upon the condition is called loop.
            A loop that lies within another loop is called nested loop.

b) Output:
            NEPAL


c) Debugged Program
           
REM to check if a word is palindeome
INPUT N$
FOR K = LEN(N$) TO 1 STEP -1
W$ = W$ + MID$(N$, K, 1)
NEXT K
IF N$ = W$ THEN
PRINT "Palindrome"
ELSE
PRINT "Not Palindrome"
END IF
END


d) program using DO……..LOOP UNTIL
           
CLS
J = 20
DO
S = S + J
J = J - 2
LOOP UNTIL J < 2
PRINT "Sum of even numbers"; S
END


e) Write a program using FUNCTION….END FUNCTION statement to display the longest string among three input strings.
            DECLARE FUNCTION LON$( A$, B$, C$)
CLS
INPUT "ENTER FIRST STRING"; A$
INPUT "ENTER SECOND STRING"; B$
INPUT "ENTER THIRD STRING"; C$
PRINT "LONGEST STRING="; LON$( A$, B$, C$)
END
FUNCTION LON$( A$, B$, C$)
IF LEN(A$) > LEN(B$) AND LEN(A$) > LEN(C$) THEN
G$ = A$
IF LEN(B$) > LEN(A$) AND LEN(B$) > LEN(C$) THEN
G$ = B$
ELSE
G$ = C$
END IF
LON$ = S$
END FUNCTION



f) A sequential data file “EXAM.DAT” contains name, address, phone number and age of some people. Write a program to display only those records which contains age above 18 and address is ‘Lumbini’.

OPEN “EXAM.DAT” FOR INPUT AS #1
CLS
WHILE NOT EOF(1)
INPUT #1, N$, A$, P#, A
IF UCASE$(A$) = ”LUMBINI” AND A>18 THEN
PRINT N$, A$, P#, A
END IF
WEND
CLOSE#1
END





***



 DOWNLOADED FROM SUSHILUPRETI.COM.NP


1a) What is variable? List the types of numeric data.

b) Study the following program and answer the following questions:
            OPEN “detail.dat” FOR INPUT AS #1
            OPEN “temp.dat” FOR OUTPUT AS #2
            CLS
            INPUT “Name of the students “; s$
            FOR i = 1 to 10
            INPUT #1, n$, c, a
            IF s$ <> n$ THEN
            WRITE #2, n$, c, a
            END IF
            NEXT i
            CLOSE #1, #2
            KILL “detail.dat”
            NAME “temp.dat” AS “detail.dat”
            END
Questions:
i)                    What is the main objective of the above program?
ii)                  Will the program run if the statement KILL “detail.dat” is removed? Why?
iii)                If the data file “detail.dat” contains only 8 records, then the program will run? Why?
iv)                Why the file “temp.dat” is necessary in this program?
c) Write down the output of the following program.
CLS
FOR X = 2 TO 15 STEP 2
IF X >= 11 THEN EXIT FOR
PRINT X ^ 2 + 1
NEXT X
PRINT "Done"
END

d) Re-write the following program correcting the bugs:
           
CLS
REM TO PRINT THE LONGEST STRING
DATA TOURISM, TRANSPORT, SERVICE, AGRICULTURE, TRADE
READ L$
FOR C = 1 TO 5
READ X$
IF LEN(X$) >LEN(L$) THEN X$ = L$
NEXT C
PRINT "LONGEST STRING IS: L$"
END

e) Re-write the following program using FOR…NEXT
            CLS
N = 1
X = 1
BACK:
PRINT N, N ^ 2
N = N + 2
X = X + 1
IF X <= 8 THEN GOTO BACK
END



f) Write a program to print alternate characters (starting from the first character of the user given string.
g) Write a program to ask student’s name, class and marks secured in three subjects. Store the data in a sequential data file ‘SCHOOL.DAT’ along with total marks. Make a provision to ask the user to enter another record.


Answers SET H
1a) A variable is an identifier or reference or name for the memory address that holds data and can change its value during the execution of a program. The types of numeric data are:
i)                    Integer data
ii)                  Long integer data
iii)                Single precision data
iv)                Double precision data

b) Questions:
i)                    What is the main objective of the above program?
Ans: The main objective of the above program is to delete the records of students according to the supplied name of the students.
ii)                  Will the program run if the statement KILL “detail.dat” is removed? Why?
Ans: No, the program will not run because if detail.dat file is not removed then the next statement while renaming the file of temp.dat to detail.dat, it displays the error message as “File already exists” because detail.dat file is already existed and it cannot rename temp.dat file to detail.dat.
iii)                If the data file “detail.dat” contains only 8 records, then the program will run? Why?
Ans: No, the program will not run and it displays the error message as “Input past end of file”
iv)                Why the file “temp.dat” is necessary in this program?
Ans: The file temp.dat is necessary in this program to store the non deleted records.


c) Output:
               5
               17    
               37
               65
               101
            Done
d) Debugged Program
           
CLS
REM TO PRINT THE LONGEST STRING
DATA TOURISM, TRANSPORT, SERVICE, AGRICULTURE, TRADE
READ L$
FOR C = 2 TO 5
READ X$
IF LEN(X$) >LEN(L$) THEN L$ = X$
NEXT C
PRINT "LONGEST STRING IS:"; L$
END


e) program using FOR…NEXT
            CLS
N = 1
FOR X = 1 TO 8
PRINT N, N ^ 2
N = N + 2
NEXT X
END


f) Write a program to print alternate characters (starting from the first character) of the user given string.
            DECLARE SUB ALT(S$)
CLS
INPUT "ENTER ANY STRING"; S$
CALL ALT(S$)
END

SUB ALT(S$)
FOR I = 1 TO LEN(S$) STEP 2
PRINT MID$(S$, I, 1);
NEXT I
END SUB

g) Write a program to ask student’s name, class and marks secured in three subjects. Store the data in a sequential data file ‘SCHOOL.DAT’ along with total marks. Make a provision to ask the user to enter another record.

OPEN “STUDENT.DATA” FOR OUTPUT AS #1
DO
CLS
INPUT “ENTER  STUDENT’S NAME”; N$
INPUT “ENTER CLASS”; C
INPUT “ENTER MARKS SECURED IN THREE SUBJECTS”; M1. M2, M3
INPUT “ENTER ROLL NUMBER”; R
T = M1 + M2 + M3
WRITE #1, N$, C, M1, M2, M3, T
INPUT “DO YOU WANT TO CONTINUE”; CH$
LOOP WHILE UCASE$(CH$)=”Y”
CLOSE #1
END


SOLVED QBASIC SET E AND F

Set E
1.      Define counter with example.
2.      Read the following program and answer the following questions:
DECLARE SUB take(x,y)
CLS
FOR I= 1 TO 5
READ c, d
CALL take(c,d)
NEXT I
DATA 13, 15, 16, 12, 31, 12, 16, 17, 21, 22
END

SUB take(a,b)
STATIC s
IF a>b THEN
s=s+a
ELSE
s=s+b
END IF
PRINT s
END SUB
a.      What is the output of the above program?
b.      If the statement STATIC s is removed, then what will be the change in its output?
c.       If the statement PRINT s is places in main module just below the statement CALL take(a,b), then what will be the output?
d.      List out the formal arguments and actual arguments from the above program.
3.      Write down the output of the following program:
CLS
FOR I = 1 TO 5
READ A$,A
PRINT MID$(A$,A,1)
NEXT I
DATA COMPUTER, 4, ORACLE, 1, WINDOW, 6
DATA KEYBOARD, 2, FORTRAN, 5
END
4.      Re-write the following program correcting the bugs:
REM to find product of the even numbers from 10 to supplied nos.
P=0
FOR C=1 TO TEN
INPUT N
IF N MOD 2>0 THEN
P=P*N
END IF
NEXT
DISPLAY P
END
5.      Write a program to find the greatest number among the 10 numbers given by the users.
6.      A sequential data file “STUDENT.DAT” has fields name, roll and address. Write a program to display those records.
Answers:
1.      Counter is a variable that counts the iterations of a loop. Example: I=1 to 10, I is a counter variable.

2.      a. OUTPUT
15
31
62
79
101

b. OUTPUT
15
16
31
17
22

c. OUTPUT
0
0
0
0
0

d. The formal arguments are: a and b and actual arguments are: c and d

3.      OUTPUT
P
O
W
E
R

4.      REM to find product of the even numbers from 10 to supplied nos.
P=1
FOR C=1 TO 10
INPUT N
IF N MOD 2=0 THEN
P=P*N
END IF
NEXT
PRINT P
END

5.      DECLARE SUB GREAT (N)
CLS
INPUT “Enter first number”; N
CALL GREAT(N)
END

SUB GREAT(N)
FOR I = 2 TO 10
INPUT “Enter next number”; G
IF G>N THEN N=G
NEXT I
PRINT “The greatest number is”; N
END SUB

6.      OPEN “STUDENT.DAT” FOR INPUT AS #1
CLS
WHILE NOT EOF(1)
INPUT #1, N$, R, A$
PRINT N$, R, A$
WEND
CLOSE #1
END



 DOWNLOADED FROM SUSHILUPRETI.COM.NP 

1a) What command is used to do following tasks in QBASIC?
            i) To list files from current directory.
            ii) To create a directory
b) Write down the output of the following program.
CLS
C = 1
FOR I = 2 TO 8 STEP 2
PRINT C;
SWAP B, C
C = B + 1
NEXT I
END


c) Re-write the following program correcting the bugs:
            REM To find the sum of square upto ten natural nos.
CLS
X = 1
DO WHILE X >= 10
A = X2
S = S + A
LOOP
PRINT "Sum of Square:"; SUM
END


d) Re-write the following program using WHILE……..WEND.
            CLS
FOR I = 1 TO 5
FOR J = 1 TO I
PRINT J;
NEXT J
PRINT
NEXT I
END



e) Write a program to declare a user defined function to check whether the supplied string is palindrome or not using FUNCTION….END FUNCTION statement. The program should input string and uses the above function to check whether it is palindrome or not.

f) WAP to open a data file “LIBRARY.DAT” in output mode and store information of books. Data file should store information such as book title, author price, date of purchase and publisher. The program should allow user to input records as needed


Answers SET F
1a)       i) To list files from current directory.
            Ans: FILES
            ii) To create a directory
ANs: MKDIR
b) Output:
            1   2   3   4
c) Debugged Program
           

            REM To find the sum of square upto ten natural nos.
CLS
X = 1
DO WHILE X <= 10
A = X ^ 2
S = S + A
X = X + 1
LOOP
PRINT "Sum of Square:"; S
END

d) program using WHILE….WEND
            CLS
I = 1
WHILE I <= 5
J = 1
WHILE J <= I
PRINT J;
J = J + 1
WEND
PRINT
I = I + 1
WEND
END



e) Write a program to declare a user defined function to check whether the supplied string is palindrome or not using FUNCTION….END FUNCTION statement. The program should input string and uses the above function to check whether it is palindrome or not.
           
DECLARE FUNCTION REV$ (S$)
CLS
INPUT "ENTER ANY STRING"; S$
C$ = REV$(S$)
IF S$ = C$ THEN
PRINT  S$; “IS PALINDROME”
ELSE
PRINT S$; “IS NOT PALINDROME”
END IF
END

FUNCTION REV$ (S$)
FOR I = LEN(S$) TO 1 STEP -1
B$ = MID$(S$, I, 1)
W$ = W$ + B$
NEXT I
REV$ = W$
END FUNCTION




f) WAP to open a data file “LIBRARY.DAT” in output mode and store information of books. Data file should store information such as book title, author price, date of purchase and publisher. The program should allow user to input records as needed

OPEN “LIBRARY.DAT” FOR OUTPUT AS #1
DO
CLS
INPUT “ENTER  BOOK TITLE”; B$
INPUT “ENTER PRICE”; P
INPUT “ENTER DATE OF PURCHASE”; D$
INPUT “ENTER NAME OF PUBLISHER”; P$
WRITE #1, B$, P, D$, P$
INPUT “DO YOU WANT TO CONTINUE”; CH$
LOOP WHILE UCASE$(CH$)=”Y”
CLOSE #1
END