Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore Digital Book Class-10 2nd edition

Digital Book Class-10 2nd edition

Published by Memorica Graphics, 2023-03-19 00:00:01

Description: Digital Book Class-10 2nd edition

Search

Read the Text Version

["Smart Computer Science Book-10 Multiple Sub Program can be called \u2022\t Write a program to ask two numbers and find out sum, product and difference. DECLARE SUB SUM(A,B) PRINT \\\"SUM=\\\"; S DECLARE SUB PRO(A,B) END SUB DECLARE SUB DIFF(A,B) INPUT \\\"Enter first number:\\\"; A SUB PRO (A, B) INPUT \\\"Enter second number:\\\"; B P=A*B CALL SUM(A, B) PRINT \\\"Product=\\\"; P CALL PRO(A, B) CALL DIFF(A, B) END SUB END SUB DIFF (A, B) SUB SUM (A, B) D=A-B S=A+B PRINT \\\"Difference=\\\"; D END SUB Test Your Skill-3 1. \t Write a program to ask a number and find the square and the cube value using multiple Sub Procedure. The SUB procedure name should be SQUAER(N) and CUBE(N) 2. \tWrite a program to ask Nepali currency and convert into Indian currency and US dollar using multiple Sub procedure. IC Rs. 1= NC Rs.1.6 and US dollar 1=NC Rs.110. 3.\t Write a program to ask length and breadth of a rectangle and calculate perimeter and area using multiple Sub procedure. Hint: P=2*(L+B) and A=(L*B). 4. \t Write a program to ask distance in meter and convert into kilometer and centimeter. 1 KM=1000 meter and 1 meter=100 centimeter. The SUB procedure name should be MtoKM(M) and MtoCM(M) 5.\t Write a program to ask time in minutes and convert into an hour and second using multiple Sub procedure. The SUB procedure name should be MINtoHR(M) and MINtoSEC(M). Concept of Parameter and Argument QBASIC supports two types of parameters: \t i) Formal parameters \t ii) Actual parameters: Formal Parameter : The formal parameter is the variable which is used to receive the value sent from the parent\/main module. So, it always comes in the form of variables which may be a string or numeric variable. This is also known as nominal parameter. It comes in the procedure definition section and declaration section. Eg. SUB xyz(A,B,C), Here, A, B, C are formal parameters. Actual Parameter: The actual parameter is a value which is sent to the procedure (SUB or FUNCTION) for further processing. So, the values which come in the Approved by Curriculum Development Center (CDC) 251","Modular Programming form of argument is called actual parameter or real parameter. It may be variable, constant or expression. It comes in the calling section. Eg. CALL xyz(A, B, 10, 20, a+b, 10*5). Here, A, B, 10,20, a+b, 10*5 are examples of argument. Sample Program Argument can be a constant \u2022\t Write a program to find the sum of 10 and 20. DECLARE SUB SUM(A,B) CLS CALL SUM(10, 20) END SUB SUM (A, B) \u2022\t In this example, 10 and 20 are constant arguments S=A+B or actual parameters. PRINT \\\"Sum =\\\"; S END SUB Test Your Skill-4 \u2022\t A and B are the formal parameters, A receives 10 and B receives 20. 1. \t Write a program to find whether 5 is divisible by both 2 and 3 or not. 2. \t Write a program to find the cube value of 5. 3.\t Write a program to find the simple interest. The principal amount is 500000, time is 3 years and rate is 13%. 4. \t Write a program to display multiplication table of 9. 5.\t Write a program to find the cube root of 125. Argument can be a variable PRINT \\\"It is even number\\\" ELSE Sample Program PRINT \\\"It is odd number\\\" \u2022\t Write a program to ask a number and END IF find out whether it is even or odd num- END SUB ber. DECLARE SUB EVENODD(N) \u2022\t In this example, N is the argument as a variable or actual CLS parameter. A is the formal parameter which receives the value INPUT \\\"Enter a number:\\\"; N of N. CALL EVENODD(N) END Logic Gate! SUB EVENODD (A) \u2022\t Both variable as a argument and parameter can be same. R = A MOD 2 IF R = 0 THEN 252 Approved by Curriculum Development Center (CDC)","Smart Computer Science Book-10 Test Your Skill-5 1. \t Write a program to ask sales amount and find out 12% sales commission. 2. \t Write a program to ask a number and find out whether it is prime or composite. 3.\t Write a program to ask a number and find out whether it is divisible by both 2 and 3 or not. 4. \t Write a program to ask quantity and rate of pen, copy and pencil and find out total amount. 5.\t Write a program to ask marks obtained in 5 subjects, find out total and percentage marks. Each subject holds 100 full marks. Argument can be an expression Sample Program \u2022\t Write a program to ask a number and find out its square value. DECLARE SUB SQRVALUE(S) CLS INPUT \\\"Enter a number:\\\"; N CALL SQRVALUE(N ^ 2) END SUB SQRVALUE (S) PRINT \\\"Square value=\\\"; S \u2022\t In this example, N^2 is the argument as an expression END SUB or actual parameter. You can use any type of formula. \u2022\t S is the formal parameter or just parameter which receives the value obtained by N^2. Test Your Skill-6 1. \t Write a program to ask principal amount, time and rate then find the simple interest. 2. \t Write a program to ask height and breath of a triangle and find the area. A=1\/2*(B*H) 3.\t Write a program to ask length and breath of a rectangle and find the perimeter. P=2*(L+B) 4. \t Write a program to calculate area of a circle. A=22\/7*R^2 5.\t Write a program to find the area of square. A=L^2. Argument can be an array variable Sample Program \u2022\t Write a program to ask and subscribe 10 numbers in the array variable and find their sum. DECLARE SUB ARRAY(N()) CLS \u2022\t In this example, (N()) is the argument as an array variable. DIM N(10) \u2022\t The array variable is passed always with reference. FOR I = 1 TO 10 Approved by Curriculum Development Center (CDC) 253","Modular Programming INPUT \\\"Enter number:\\\"; N(I) NEXT I CALL ARRAY(N()) END SUB ARRAY (N()) FOR I = 1 TO 10 S = S + N(I) NEXT I PRINT \\\"Sum=\\\"; S END SUB Test Your Skill-7 1. \t Write a program to ask and subscribe 10 numbers in the array variable and display in ascending order. 2. \t Write a program to ask and subscribe 10 numbers in the array variable and display in descending order. 3. \t Write a program to ask and subscribe 10 numbers in the array variable and find out your roll number is in the list or not. Concept of Local and Global variable QBASIC supports two type of scope variables in the modular programming on the basis of nature of accessing technique. i) Local Variable\t ii) Global Variable Local Variable: A variable which is declared inside any module and cannot be accessed by other modules is known as local variable. The local variable can be used within the module where it is defined. The value of local variable is destroyed when the program flows out of the procedure. Sample Program \u2022\t Write a program to ask a number and find out its square value. DECLARE SUB SQAR( ) N=5 PRINT N^2 CALL SQAR END SUB SQUAR ( ) PRINT N^2 \u2022\tIn this example, N is defined locally in the main module, so it gives the output 25 from main module and 0 from sub module, because it is not accessible in sub module. END SUB 254 Approved by Curriculum Development Center (CDC)","Smart Computer Science Book-10 Global Variable : A variable that is accessible from any procedure or module of a program is called a global variable. If a variable is needed by most or all of the procedures, it is more convenient to declare global variable than passing variable as argument. The life of the global variable is always 'on' within the program and is not destroyed during the program execution. Global variable is declared with the keyword COMMON SHARED, DIM SHARED or SHARED in the Qbasic. COMMON SHARED statement Function\t: The COMMON SHARED statement is used to declare the variable globally. It is used in main module. Syntax\t\t: COMMON SHARED list of variables. \u2022\tList of variables that represent legal strings or numeric variables. Example\t : COMMON SHARED A, B, C Sample Programs \u2022\t Write a program to ask a number and find out its cube value. DECLARE SUB CUBE() COMMON SHARED N N=5 PRINT N^3 CALL CUBE END SUB CUBE PRINT N^3 In this example, 'N' is defined as a global variable, so it is giving the output 125 in END SUB both modules because the variable 'N' is accessible in both modules. \u2022\t Write a program to ask two numbers and find out sum product and difference. DECLARE SUB SUM() S=A+B DECLARE SUB PRO() PRINT \\\"SUM=\\\"; S DECLARE SUB DIFF() END SUB COMMON SHARED A, B SUB PRO CLS P = A * B INPUT \\\"Enter first number:\\\"; A PRINT \\\"Product=\\\"; P INPUT \\\"Enter second number:\\\"; B END SUB CALL SUM SUB DIFF CALL PRO D=A-B CALL DIFF PRINT \\\"Difference=\\\"; D END END SUB SUB SUM \u2022\t In this example, 'A' and 'B' are global variables which are accessible in all three modules. Approved by Curriculum Development Center (CDC) 255","Modular Programming DIM SHARED statement Function\t: The DIM SHARED statement is used to declare the array variable globally. It is used in main module. Syntax\t\t: DIM SHARED list of variables \u2022\tList of variables that represent legal strings or numeric array variable as given in the example. Example\t : DIM SHARED N() Sample Program \u2022\t Write a program to subscribe 10 numbers in the array variable and find their sum. DECLARE SUB ARRAY() SUB ARRAY \u2022\t In this example, N(10) is defined as a DIM SHARED N(10) FOR I = 1 TO 10 global array variable, so argument is CLS S = S + N(I) not passed. FOR I = 1 TO 10 NEXT I PRINT \\\"SUM=\\\"; S \u2022\t You can see, same program is written INPUT \\\"Enter number:\\\"; N(I) previously by passing the argument. NEXT I END SUB CALL ARRAY END Test Your Skill-8 1. \t Write a program to ask and subscribe 10 students' names in the array variable and display in alphabetical order. (Using DIM SHARED statement) 2. \t Write a program to ask and subscribe 10 numbers in the array variable and dis- play all the values along with their square values. (Using DIM SHARED state- ment) SHARED statement Function\t: \tThe SHARED statement is used to declare the global variable. It is used in sub or function procedure. It shares the variable only with its main module. Syntax\t\t: SHARED list of variables \u2022\tList of variables that represent a legal string and numeric variables as given in the example. Example\t : SHARED A, B, C, N$, AD$ Sample Program \u2022\t Write a program to ask two numbers and find their sum using SHARED statement. DECLARE SUM() SUB SUM \u2022\t In this example, A and CLS SHARED A, B B variables are accessed from main module due to INPUT \\\"ENETR TWO NUMBER:\\\"; A, B S=A+B CALL SUM PRINT \\\"SUM=\\\"; S SHARED statement. END END SUB 256 Approved by Curriculum Development Center (CDC)","Smart Computer Science Book-10 FUNCTION PROCEDURE Function is a ready made program which performs a specific task. Some functions come with QBASIC system which are called library functions or built- in functions like LEFT$(), STR$(), VAL(), int(), etc. Some functions should be defined ourselves that are called user defined functions. Function procedure is a user defined function which is a small, logical and functional block of codes which performs the specific task and returns a single value to the main module or calling module. Once defined the Function procedure can be used frequently in different places in the program as our requirement. There are two types of user defined function: i) Numeric Function and ii) String Function. String Function returns the string result and Numeric Function returns the numeric result. The function's name accepts the data type and it is treated as a variable name as the data are assigned. CREATING USER-DEFINED FUNCTION As the name implies, we should create the Function Procedure ourselves. To create a user defined function, we should complete the following phases: i) Function Declaration\t ii) Function Definition\t\t iii) Calling Function Function Declaration Function declaration is the initial phase of creating the Function procedure. It is declared at the top of the main module with DECLARE statement. DECLARE FUNCTION Statement Function\t: This Statement is used to declare the user defined Function Procedure. It is non-executable statement. Syntax\t\t: DECLARE FUNCTION <name> [(parameter list)] \u2022\tname is the function procedure name \u2022\tparameter list is the list of variables, separated by comma that receive values sent from main module. Example\t : DECLARE FUNCTION XYZ(A,B) \u2022\tHere, XYZ is a name of Function procedure and A and B are the parameters. Function Definition The function definition is the process of defining function within the program. It is defined using the FUNCTION ......... END FUNCTION statement. It is the separate block of codes that is defined to return a specific value. FUNCTION.....END FUNCTION Statement Function\t: Its function is to define the user defined Function Procedure. Syntax\t: \tFUNCTION <name> [(parameter list)] \t\t............................. Approved by Curriculum Development Center (CDC) 257","Modular Programming \t\t.............................. \t\t <procedure name>= return value \t\t END FUNCTION \u2022\tname is the procedure name. \u2022\tparameter list is the list of variables that receive values sent from main module as an list of arguments. \u2022\treturn value may be variable or expression. \u2022\t PROD is a function name \u2022\t A, B are the parameters Example : FUNCTION PROD (A,B) \u2022\t PROD=P returns value \t \t P=A*B \t\t\t PROD=P \t\t END SUB Calling Function Procedure Once the Function procedure is defined, it needs to be called to use from the main module. The Function procedure can be called using PRINT statement or a variable. Syntax\t: \ti) PRINT <function name> [(argument list)] \t\tii) <Variable name>=<function name> [(argument list)] Sample Programs Function can be called with PRINT statement \u2022\t Write a program to ask any two numbers and find their sum. Using FUNCTION ...... END FUNCTION procedure. DECLARE function XYZ(A,B) Declaration section Main Module CLS INPUT \\\"Enter first number:\\\"; A INPUT \\\"Enter second number:\\\"; B Calling section PRINT \\\"Sum=\\\"; XYZ(A, B) END FUNCTION FUNCTION XYZ (A, B) Definition section Procedure S=A+B Body section XYZ = S END SUB \u2022\t This Function calling method is also called Function called by statement. 258 Approved by Curriculum Development Center (CDC)","Smart Computer Science Book-10 Function can be called with a Variable\/Expression \u2022\t Write a program to ask any two numbers and find their sum. Using FUNCTION ...... END FUNCTION procedure. DECLARE function XYZ(A,B) INPUT \\\"Enter first number:\\\"; A \u2022\t In this example, SU variable is used INPUT \\\"Enter second number:\\\"; B to call the Function XYZ(A,B). SU= XYZ(A, B) PRINT \\\"Sum=\\\"; SU \u2022\t It is also called, Function called by END the expression. FUNCTION XYZ (A, B) S=A+B XYZ = S END SUB Argument can be constant value \u2022\t Write a program to find the product of two numbers 10 and 5. Using FUNCTION ...... END FUNCTION procedure. DECLARE function xyz(A,B) Test Your Skill-9 CLS PR= xyz(10, 5) 1. \t Write a program to calculate simple interest where P=20000, T=2 PRINT \\\"Product=\\\"; PR and R=12. END 2. \t Write a program to find the area of a triangle. A=1\/2*(B*H). Where, B=5 and H=8 FUNCTION xyz(A, B) 3.\t Write a program to find the perimeter of a triangle. P=2*(L+B). P=A*B Where L=5 and B=3 XYZ = P 4. \tWrite a program to calculate the area of a circle. A=22\/7*R^2, END SUB Where R=10. \u2022\t In this example, 10 and 5 are passed as constant arguments. Argument can be expression \u2022\t Write a program to find the difference of two numbers 10 and 5. Using FUNCTION ...... END FUNC- TION procedure. DECLARE function XYZ(D) FUNCTION XYZ (D) CLS XYZ = D DF= XYZ(10-5) PRINT \\\"Difference=\\\"; DF END SUB END \u2022\t In this example, 10-5 is passed as an expression argument. Approved by Curriculum Development Center (CDC) 259","Modular Programming Test Your Skill-10 1. \t Write a program to ask any three numbers and find their sum. 2. \t Write a program to find the sum of cube value of 5 and 2. 3.\t Janak Lal Yadav sells the products of Rs. 500000 in a week. He gets the 5% commission in sales amount. Write a program to calculate commission amount of Janak Lal Yadav. 4. \t Write a program to find the remainder of the cube value of 9 divided by 5 . 5.\t Write a program to ask a number and find the square root of 25. Argument can be variable \u2022\t ABC Bank has decided to provide the 10% interest rate on fixed deposit for the depositors. Now, write a program to ask principal amount and time, then calculate simple interest. DECLARE FUNCTION SINTEREST(P, T, R) DECLARE FUNCTION SINTEREST(P, T, R) CLS CLS INPUT \\\"Enter Principal Amount:\\\"; P INPUT \\\"Enter Principal Amount:\\\"; P INPUT \\\"Enter Time:\\\"; T INPUT \\\"Enter Time:\\\"; T R = 10 R = 10 SI = SINTEREST(P, T, R) SI = SINTEREST(P, T, R) PRINT \\\"Simple Interest=\\\"; SI PRINT \\\"Simple Interest=\\\"; SI END END FUNCTION SINTEREST (P, T, R) FUNCTION SINTEREST (A, B, C) S = (P * T * R) \/ 100 S = (A * B * C) \/ 100 SINTEREST = S SINTEREST = S END SUB END SUB \u2022\t In this example, P, T and R are argument variables. Arguments and parameters variables can be used same or different as you like as mentioned in the above examples. FUNCTION can be called without passing argument \u2022\t Write a program to find the sum of first \u2022\t Write a program to ask any two 10 natural numbers. numbers and find their sum without DECLARE FUNCTION SUMN() passing argument. CLS PRINT \\\"Sum=\\\"; SUMN DECLARE FUNCTION SUM() END CLS S = SUM FUNCTION SUMN PRINT \\\"Sum of two numbers=\\\"; S FOR I = 1 TO 10 END S=S+I NEXT I FUNCTION SUM SUMN = S INPUT \\\"Enter first number:\\\"; A INPUT \\\"Enter second number:\\\"; B END FUNCTION SUM = A + B \u2022\t In these examples, arguments are not passed. END FUNCTION 260 Approved by Curriculum Development Center (CDC)","Smart Computer Science Book-10 String Function returns String value PRINT \\\"Sum=\\\"; SUM(10, 20) \u2022\t Write a program to ask a number and find out it PRINT \\\"Sum=\\\"; SUM(10, 5) PRINT \\\"Sum=\\\"; SUM(15, 30) is divisible by 2 or not. END DECLARE FUNCTION DIV$(N) FUNCTION SUM (A, B) SUM = A + B CLS INPUT \\\"Enter a number:\\\"; N D$ = DIV$(N) PRINT D$ END FUNCTION END Argument can be array variable \u2022\t Write a program to read the following FUNCTION DIV$ (N) R = N MOD 2 data and find their sum. 10, 20, 30, 40, 50 IF R = 0 THEN DECLARE FUNCTION SUMARRAY(N()) CLS DIV$ = \\\"It is divisible\\\" DIM N(5) FOR I = 1 TO 5 ELSE READ N(I) DIV$ = \\\"It is not divisible\\\" NEXT I DATA 10,20,30,40,50 END IF PRINT \\\"SUM=\\\"; SUMARRAY(N()) END END FUNCTION \u2022\t In this example, DIV$ is a name of string FUNCTION SUMARRAY (N()) function which returns a string value. FOR I = 1 TO 5 Function can be used in multiple places \u2022\t Write a program to create a Function procedure which should accept 2 numbers and find their sum and test it using in multiple places. DECLARE FUNCTION SUM(A,B) S = S + N(I) CLS NEXT I SUMARRAY = S Test Your Skill-11 END FUNCTION 1. \t Write a program to create a FUNCTION procedure to ask a number and find out whether it is even or odd. 2. \t Write a program to create a FUNCTION procedure which asks a number and find out whether it is perfect square root number or not. 3.\t Write a program to create a FUNCTION procedure to receive 2 numbers and find out greater number. 4.\t Write a program to create a FUNCTION procedure to receive 3 numbers and find out greatest number. 5.\t Write a program to create a FUNCTION procedure to receive 3 numbers and find out middle number. Approved by Curriculum Development Center (CDC) 261","Modular Programming DRY RUN A dry run is the process for manually working through their code to trace the value of variables. There is no software involved in this process. So, a dry run simply means to execute your code manually by going through each line, iteration, operations, function procedures, sub procedures, etc. Sample Dry Run 1. Write the output of given program with the dry run. DECLARE SUB XYZ(P,T,R) CLS P = 1000 PT Dry Run PRINT I T=2 200 R = 10 RI CALL XYZ(P, T, R) 1000 2 10 (1000*10*2)\/100 END SUB XYZ (P, T, R) \u2e2b Output is : 200\t\t I = (P * T * R) \/ 100 PRINT I END SUB 2. Write the output of given program with the dry run. DECLARE FUNCTION XYZ(A,B,C) CLS Dry Run LET A = 10 LET B = 9 A B C PRINT XYZ(A, B, C) AV = (A + B + C) \/ 3 XYZ=AV XYZ=13 LET C = 20 10 9 20 13 AV=(10+9+20)\/3 PRINT XYZ(A, B, C) END \u2e2b Output is : 13\t\t FUNCTION XYZ (A, B, C) AV = (A + B + C) \/ 3 XYZ = AV END SUB 3. Write the output with dry run. Dry Run DECLARE SUB XYZ () CLS I PRINT I^2 CALL XYZ 1 PRINT 1^2 = 1 END 2 PRINT 2^2 = 4 SUB XYZ 3 PRINT 3^2 = 9 FOR I = 1 TO 5 4 PRINT 4^2 = 16 PRINT I ^ 2; 5 PRINT 5^2 = 25 NEXT I END SUB \u2e2b Output is : 1 4 9 16 25\t\t 262 Approved by Curriculum Development Center (CDC)","Smart Computer Science Book-10 4. Write the output of given program with the dry run. DECLARE SUB (N) CLS Dry Run FOR I = 1 TO 5 I N PRINT N ^ 0.5 + 1 READ N 1 16 16 ^ 0.5 + 1 = 5 CALL XYZ(N) 2 9 9^ 0.5 + 1 = 4 3 4 4 ^ 0.5 + 1 = 3 NEXT I 4 25 25 ^ 0.5 + 1 = 6 END DATA 16,9,4,25,36 SUB XYZ (N) 5 36 36 ^ 0.5 + 1 = 7 PRINT N ^ 0.5 + 1; \u2e2b Output is : 5 4 3 6 7\t\t END SUB 5. Write the output of given program with the dry run. DECLARE SUB XYZ(A,B) CLS X=1 x y AB I Dry Run A=B B=S Y=1 1 2 CALL XYZ(X, Y) PRINT A S=A+B 2 3 3 5 END 1 1 11 1 1 1+1=2 5 8 8 13 SUB XYZ (A, B) 2 1 1+2=3 FOR I = 1 TO 5 3 2 2+3=5 PRINT A; 4 3 3+5=8 S=A+B 5 5 5+8=13 A=B B=S \u2e2b Output is : 1 1 2 3 5\t\t NEXT END SUB 6. Write the output of given program with the dry run. DECLARE SUB SHOW (A) CLS N = 87 CALL SHOW(N) END SUB SHOW (A) DO B = A MOD 6 + 3 IF B MOD 4 = 0 THEN GOTO AA PRINT B; AA: Approved by Curriculum Development Center (CDC) 263","Modular Programming A = A - 10 LOOP WHILE A >= 50 END SUB Dry Run N A B=A MOD 6+3 IS B MOD 4 =0 ? PRINT B A=A-10 IS A>=50? 87 87 6 No 6 77 Yes 8 YES 67 YES 4 YES 57 YES 6 NO 6 47 NO \u2e2b Output is : 6 6\t\t 7. Write the output of given program with the dry run. DECLARE FUNCTION SUM (A) CLS A=5 PRINT SUM (A) END FUNCTION SUM (A) FOR K = 1 TO A IF K MOD 2 = 0 THEN S=S+K END IF NEXT K SUM = S END FUNCTION A FOR K= 1 to A Dry Run Sum=S PRINT SUM(A) 51 Sum=6 6 IS K MOD 2 =0 ? S=S+K 2 NO 3 YES S=0+2=2 4 NO 5 YES S=2+4=6 6 NO \u2e2b Output is : 6\t\t 264 Approved by Curriculum Development Center (CDC)","Smart Computer Science Book-10 8. Write the output of given program with the dry run. DECLARE SUB PATTERN() CALL PATTERN Dry Run \u2e2b Output is : END 1 SUB PATTERN I J= 1 TO I PRINT J 22 1 TO 1 1 333 FOR I = 1 TO 5 1 4444 FOR J = 1 TO I 55555 PRINT I; 2 1 TO 2 22 NEXT J 3 1 TO 3 3 33 PRINT 4 1 TO 4 4444 NEXT I 5 1 TO 5 5 55 5 5 END SUB 9. Write the output with the dry run DECLARE SUB XYZ (A) CLS N = 236 CALL XYZ(N) END SUB XYZ (A) DO WHILE A <> 0 R = A MOD 10 S=S+R A = A \\\\ 10 LOOP PRINT S END SUB Dry Run N A IS A<> 0? R= A MOD 10 S=S+R A=A\\\\10 PRINT S 236 236 YES R=236 MOD 10=6 S=0+6=6 A=236\\\\10=23 YES R= 23 MOD 10= 3 S=6+3=9 A=23\\\\10=2 YES R=2 MOD 10 =2 S=9+2=11 A=2\\\\10=0 NO 11 \u2e2b Output is : 11\t\t 10. Write the output of given program with the dry run. DECLARE FUNCTION XYZ(N) CLS N = 145 PRINT XYZ(N) END Approved by Curriculum Development Center (CDC) 265","Modular Programming FUNCTION XYZ (N) WHILE N > 0 R = N MOD 10 X = X * 10 + R N = N \\\\ 10 WEND XYZ = X END FUNCTION Dry Run N PRINT XYZ(N) IS N> 0? R= N MOD 10 X = X * 10 + R N = N \\\\ 10 XYZ=X XYZ=541 145 YES R=145 MOD 10=5 X=0*10+5=5 N=145\\\\10=14 YES R= 14 MOD 10= 4 X=5*10+4=54 N=14\\\\10=1 YES R=1 MOD 10 =1 X=54*10+1=541 N=1\\\\10=0 541 NO \u2e2b Final output is : 541\t\t 11. Write the output of given program with the dry run. DECLARE SUB XYZ(W$) CLS W$ = \\\"PATAN\\\" CALL XYZ(W$) END SUB XYZ (W$) FOR I = LEN(W$) TO 1 STEP -1 PRINT RIGHT$(W$, I) NEXT I END SUB Dry Run W$ FOR LEN(W$) TO 1 STEP -1 PRINT RIGHT$(W$, I) \u2e2b Output is :\t PATAN 5 PRINT RIGHT$(\\\"PATAN\\\",5) = PATAN PATAN ATAN 4 PRINT RIGHT$(\\\"PATAN\\\",4) = ATAN TAN AN 3 PRINT RIGHT$(\\\"PATAN\\\",3) = TAN N 2 PRINT RIGHT$(\\\"PATAN\\\",2) = AN 1 PRINT RIGHT$(\\\"PATAN\\\",1) = N \t 266 Approved by Curriculum Development Center (CDC)","Smart Computer Science Book-10 Points to Know \u2022\t The modular programming is a programming technique which is used to divide a program into smaller logical and manageable part of the program that can perform a specific task. \u2022\t Small manageable part of the program is known as the module or procedure. \u2022\t A Sub procedure is a small, logical and manageable, functional section or branch of a main program which performs the specific task, but does not return any value to its parent module. \u2022\t The call by reference method of passing arguments to a procedure which copies the address of an argument into the respective formal parameters. \u2022\t Values of arguments are copied to procedure's formal parameters and the two copies of parameters are stored in different memory locations. \u2022\t The formal parameter is the variable which is used to receive the value sent from the parent\/main module. \u2022\t The actual parameter is a value which is sent to the procedure (SUB or FUNCTION) for further processing. \u2022\t A variable which is declared in a module and cannot be accessed by other modules is known as local variable. \u2022\t The variable that can be accessed from any procedure or module used in the program is called a global variable. \u2022\t Function procedure is a user defined function which is a small, logical and functional block of codes which performs the specific task and returns a single value to the main module or calling module. Terms to Know Parameter\t: Variable passed with the (Function\/SUB) procedure definition to receive the values sent from the main module. Argument\t: Values passed with the calling procedure to send to the sub\/function procedure. Global Variable\t : The variable which is accessible in all the modules. Local Variable\t :\t The variable which is accessible in only the module where it is defined. Pass by value\t :\t Passing the argument to a procedure that copies the value and make a new address in memory . Pass by reference\t:\tPassing arguments to a procedure that copies the address of an argument into the formal parameter. Approved by Curriculum Development Center (CDC) 267","Modular Programming Worksheet Objective Questions 1. Fill in the blanks: a. The............................... programming technique divides a program into a small part of the program. b. \t The Sub-module is totally controlled by ................................. module. c. \t The Sub Procedure is called by ........................... statement. d. \t After completion of the execution process in the procedure, the execution flow takes place next to the ....................... statement or expression. e. \t Argument can be passed by ............................. or ..................................... f. \t .................................. statement is a non-executable statement g.\t ...................... statement is used to define a sub procedure. h.\t ...................................... are the variables which receive the values sent from the main module. i. \t ................... variable is accessible only in a module where it is defined. j. \t ................................. variable is accessible in all the modules. k.\t ............................ sign is followed by the name of the string function. l.\t Value of .................................... variable is destroyed when the program execution flows out of the procedures. m.\t Function procedure is defined by ................................................ statement.\t 2. Write 'T' for true and 'F' for false statements: a. \tEvery modular program has multiple main modules. b. \tThe main module controls the entire program. c.\t The main module is always located in the top of the sub module. d.\t The sub module is always located in the bottom of the main module. e. \tSub procedures always return the value. f.\t The name of Sub procedure can be treated as a variable name. g.\t SUB is always closed by END SUB. 268 Approved by Curriculum Development Center (CDC)","Smart Computer Science Book-10 h. \tParameter may be string or numeric variable. i. \t Argument values can be string or constant. j.\t The life of global variable is always 'ON' within the whole program main module and procedure. k.\t Argument can be only constant. l.\t Procedures can be called without argument. m.\t\t Array variable is passed always with reference. 3. \tMark the correct option: a. \t Global variables are declared with: ii) DIM SHARED\t \t i) COMMON SHARED\t\t\t iv) All of these iii) SHARED\t\t\t\t b. \t Function Procedure can be called with: \t i) Variable\t\t\t\t ii) PRINT statement\t \t iii) Both i) & ii)\t\t\t\t iv) None of these c. \t Argument can be : ii) Constant\t \t \ti) Variable\t\t\t\t iv) None of these iii) Both i) & ii)\t\t\t\t d. \t SHARED statement is used in : ii) Procedure\t \t \ti) Main module\t\t\t\t iv) None of these iii) Both i) & ii)\t\t\t\t e. \t Argument is also called: ii) Real Parameter\t \t \ti) Actual Parameter\t\t\t iv) None of these iii) Both i) & ii)\t\t\t\t f. \t Simply, parameter is known as: ii) Nominal Parameter\t \t \ti) Formal Parameter\t\t\t iv) None of these iii) Both i) & ii)\t\t\t\t g. \t QBASIC modular programming supports two types of the calling method: \ti) Call by reference\t\t\t ii) Call by value\t \t iii) Both i) & ii)\t\t\t\t v) None of these h. \t Sub Procedure can be called with: ii) Without CALL statement\t\t \ti) CALL statement\t\t\t\t iv) None of these iii) Both i) & ii)\t\t\t\t Approved by Curriculum Development Center (CDC) 269","Modular Programming h. \t FUNCTION Procedure can be: ii) Numeric Function\t \t \ti) String function\t\t\t\t\t iv) None of these iii) Both i) & ii)\t\t\t\t\t ii) Defining Procedure \t iv) All of these i. \t Creating Procedure includes: \ti) Declaring Procedure\t\t\t\t iii) Calling Procedure\t\t\t\t Descriptive Questions 1. Write short answer of the following questions: a.\t What is modular programming? b.\t Write the advantages of modular programming. c. \t What is the main module in modular programming? d. \t What is the Sub module in modular programming? e.\t What is SUB procedure? f.\t Differentiate between call by value and call by reference. g. \t Differentiate between local variable and global variable. h.\t Differentiate between SUB and FUNCTION procedure. i.\t What is FUNCTION procedure? j.\t Differentiate between COMMON SHARED and DIM SHARED. 2. Write the function and syntax of the following statements: a. DECLARE \t\t\t\t b. SUB....END SUB \t\t c. CALL d. FUNCTION......END FUNCTION\t e. COMMON SHARED\t\t f. SHARED Case Study and Application-based Questions 1. Study the following program and answer the questions: DECLARE FUNCTION XYZ(NC) ELSEIF NC <= 250 THEN CLS A = N*2 PRINT XYZ(300) PRINT XYZ(225) ELSEIF NC <= 300 THEN PRINT XYZ(450) A = N^2-100 PRINT XYZ(325) PRINT XYZ(150) ELSEIF NC <= 400 THEN END A = N MOD 2 FUNCTION XYZ (N) ELSE IF NC <= 200 THEN A = INT(N\/2) A = N^2 END IF XYZ = A END FUNCTION 270 Approved by Curriculum Development Center (CDC)","Smart Computer Science Book-10 i) \t Which procedure is used in this program? ................................................................ Ans: ....................................................... v) \tWrite the arguments passed into the ii) \t What is the name of the procedure? procedure. Ans: ....................................................... Ans: ....................................................... iii)\t Write the output of the program. vi)\tWrite the formal parameters used in this program. Ans: ....................................................... Ans: ....................................................... iv) \t Why is the dollar sign not used with the name vii) \tHow are arguments passed? of the procedure? If you use the dollar sign, Ans: ....................................................... what will happen? Write your comment. viii) How are Function Procedures called? Ans: ........................................................ Ans: ....................................................... ................................................................ 2. On the basis of given program answer the asked questions: DECLARE SUM(X, Y) i) \t Write the name of procedures. DECLARE DIFF(I, J) DECLARE PRO(M, N) Ans: ....................................................... CLS ii) \t Write the output, if the value of A=10 and INPUT \\\"Enter two numbers:\\\"; A, B CALL SUM(A, B) B=5? CALL DIFF(A, B) CALL PRO(A, B) Ans: ........................................................ END ................................................................ SUB SUM (X, Y) S=X+Y ................................................................ PRINT S iii) \t List out the arguments and parameters? END SUB Ans: ........................................................ SUB DIFF (I, J) ................................................................ D=I-J iv)\t How are arguments passed ? PRINT D Ans: ....................................................... END SUB ................................................................ SUB PRO (M, N) P=M*N ................................................................ PRINT P END SUB Approved by Curriculum Development Center (CDC) 271","Modular Programming 3. Underline the bugs and correct them: FUNCTION MUL (N, I) a. \t REM To print multiplication table \t T=N*I \t DECLARE SUB Mul( ) \t T = MUL \tCLS END FUNCTION \t INPUT \\\"Enter a number::\\\"; A \t CALL Mul(M) d. \t DECLARE FUNCTION SUM (N()) \tEND \t REM: To find the sum of 10 numbers \tCLS \t SUB Mul (Num) \t DIM N(5) CLS \t FOR I = 1 TO 5 FOR K = 1 TO 10 PRINT N; \u201c*\u201d ; K; \u201c=\u201d ; N * K \t INPUT \\\"Enter number:\\\"; N NEXT K \t NEXT I \t PRINT \\\"Sum=\\\"; SOM(N()) \t SUB END \tEND b. \t REM To reverse the string \t FUNCTION SUM (N()) \tCLS \t FOR I = 1 TO 10 \t DECLARE SUB REV$(N$) \t S=S+N \t INPUT \u201cEnter String:::\u201d;S$ \t NEXT I \t CALL REV(N$) \t SUM = S \tEND \t END FUNCTION \t SUB REV(N$) e. \t DECLARE FUNCTION RES$ (N) \t\t FOR K = LEN$(N$) TO 1 STEP -1 \t REM: To find a number is odd or even \tCLS \t\t\t PRINT MID$(N$,K,1); \t INPUT \\\"Enter a number:\\\"; N$ \t PRINT RES$(N) \t\t NEXT K \tEND \t END SUB \t FUNCTION RES$ (N) \t R=N\\\\2 c.\t REM To display multiplication table\t \t IF R = 0 THEN \tCLS \t EVE$ = \\\"It is even.\\\" \t DECLARE FUNCTION MUL (N, I) \t ELSE \t INPUT \\\"Enter Number :\\\"; X \t ODD$ = \\\"It is odd.\\\" \t FOR J = 1 TO 10 \t END IF \t P = MUL(N, J) \t END FUNCTION \t PRINT X; \\\"*\\\"; J; = ; P \t NEXT J \tEND 272 Approved by Curriculum Development Center (CDC)","Smart Computer Science Book-10 4. \t Write programs using SUB ....... END SUB numbers from 1 to 100. Procedure. h.\tWrite a program to display perfect a. \tWrite a program to ask Gigme's cube root numbers from 1 to 100. monthly income. His monthly expenditure in different headings are i. \tWrite a program to display those as follows: numbers which can read same from both sides. Heading\t\t Expenditure j\t Write a program to display all the below or equal to three digits even \t Food\t\t\t10% numbers. \tEducation\t\t 8% \tMedical\t\t 5% k.\t Write a program to ask a sentence and find out the total number of words. \tClothing\t\t 6% \t Miscellaneous \t 15% l.\t Write a program to ask a word and find out the total number of consonants. \t The program should display the expenditure amount of each heading m.\tWrite a program to ask your name and find out the annual saving. and display only vowels. b.\tWAP to ask your name, age and n. \tWrite a program to ask a sentence find out you can vote in the national and find out total each type of vowels. election or not. If your age is not below 18 years you can vote in the national o.\t Write a program to ask a word and election otherwise you cannot vote. find out whether it is palindrome word or not. c. \tWrite a program to ask age of 100 people and find out the total number p.\t Write a program to display ASCII code of people in the following age group. \t character of 0 to 255. Below 100 q. \tWrite a program to ask a word and \t 10 \u2013 20 display ASCII code value of each character. \t 21 \u2013 30 r.\t Write a program to ask a sentence and \t 31 \u2013 50 find out total capital letters, small letters, digits and others special characters. \t 51\t and Above d.\t Write a program to display multiples \t ASCII Code \t Character of each number from 1 to 5 up to 10th term. \t 65 to 90\t\t Capital A to Z e.\t Write a program to ask 10 numbers \t 97 to 122\t\t Small a to z and find out the sum of odd and even numbers. \t 48 to 57\t\t Numbers 0 to 9 f. \t Write a program to ask 10 numbers s.\t Write a program to ask a word and and find out greatest and smallest display as: If a word is NEPAL, it number. should display nEpAl. g.\t Write a program to display only prime t. \t Write a program to ask a number and find out whether it is negative, positive or zero. Approved by Curriculum Development Center (CDC) 273","Modular Programming u.\tWrite a program to store 10 f. \t Write a program to ask a character students' names in single dimension and find out whether it is the vowel or subscript array N$(10) and display in not. alphabetical order A to Z. g. \tWrite a program to ask day of the 5. \t Write programs using FUNCTION ............ week in the number between 1 \u2013 7 END FUNCTION Procedure. and display its name of the day. If you input 2, then the program should a. \tWrite a program to enter the sides of display Monday. the triangle and find out the area of the triangle. Where, area of the triangle h.\t Write a program to ask 10 positive = s* (s-a) * (s-b) * (s-c). Where s= numbers and find out greatest (a+b+c) \/2. number. b. \tThe length & breadth of a rectangle i.\t Write a program to ask a number and and radius of a circle are input through find out whether it is perfect number the keyboard. Write a program to cal- or not. A perfect number is a positive culate the area and perimeter of the integer that is equal to the sum of rectangle, and the area & circumfer- its positive divisors, excluding the ence of the circle. AC=PI*R^2, Cir- number itself. For instance, 6 has cumference of circle=2*PI*R, AR=L*B divisors 1, 2 and 3, and 1 + 2 + 3 = 6, and PR=2*(A+B) so 6 is a perfect number. c.\t WAP to ask your five subject marks j.\t Write a program to ask a number and find out result that you are failed and find out whether it is prime or or passed. If average is above 40, composite. you are passed otherwise failed. k.\t Write a program to ask a number and d.\tWrite a program to ask marks in find out sum of digits. mathematics, computer and science and find out whether you can study l.\t Write a program to ask number and computer or not. If all the subject find out whether it is Armstrong or not. marks is above 60, you can study computer, otherwise you can not m. Write a program to ask a number and study computer. display in reverse order. e.\t WAP to ask principal amount and time n.\t Write a program to ask 10 numbers then calculate the simple interest on and find out total perfect cube root the basis of the following conditions. numbers. \tTime\t\t\tRate(%) o. \tWrite a program to ask a number and \t1\t\t\t8 find out whether it can read same \t2\t\t\t10 from both sides or not. \t3\t\t\t12 p. \t Write a program to ask a sentence and find out the total number of spaces. \t above 3\t\t 15 q.\t Write a program to ask a word and find out the total number of vowels. 274 Approved by Curriculum Development Center (CDC)","Smart Computer Science Book-10 r.\t Write a program to ask a word and h.\t 1, 11, 111, 1111, 11111,111111 display in reverse order. i. \t 2, 2, 4, 6\u2026\u2026..up to 10th terms j.\t 1, 8, 27, 64 \u2026\u2026\u2026\u2026\u2026. 10th term. 6. Write programs for the following using SUB ............END SUB Procedure. a.\t 2, 8, 18, 32 \u2026\u2026\u2026 up to 10th term. k.\t 5, 25, 125 \u2026\u2026.. up to 10th term. b.\t 5, 25, 125 \u2026\u2026\u2026....up to 5th term. l .\t 5, 125 \u2026\u2026\u2026 up to 10th term. c.\t 1000, 98.5, 97, 96.5 ...up to10th term. m. \t5 55 555 5555 55555 d. \t0.1, 0.11, 0.111, 0.11111, 0.11111 n. \t55555 5555 555 55 5 e. \t-25, -20, -15, -10, 0... up to 20th term. o. 1 \t 2 3 6 f. \t 1000, 729, 512, \u2026\u2026Up to10th term. 45 9 g.\t .1, 0.01, 0.001, 0.0001, 0.00001 78 7. Write programs for the following patterns using SUB ............END SUB Procedure. a.\t1 b.\t 1 2 3 4 5 c. 5 4 3 2 1 d. 1 2 3 4 5 e. \t5 4 3 2 1 \t 22 \t 1234 \t 5432 \t 2345 \t 4321 \t 333 \t 123 \t 543 \t 45 \t 321 \t 4444 \t 12 \t 54 \t 45 \t 21 \t 55555 \t1 \t5 \t5 \t1 f. * g. * h. * i. * * ** * * * * * ** * * ** * * j. P k. PATAN l.\tP m. PATAN A PATA \tPA \t ATAN T PAT \tPAT \t TAN A PA \tPATA \t AN P \tPATAN \tN N Approved by Curriculum Development Center (CDC) 275","Modular Programming o. PATAN \t\t p. N q. P ATA TAN PAT n. T T PATAN ATA PATAN PATAN 8. Write the output of following programs with dry run: a. \t DECLARE SUB xyz() c.\t DECLARE SUB xyz() \tCLS Output \tCLS Output \t CALL xyz \t CALL xyz \tEND \tEND \t SUB xyz \t SUB xyz \t FOR I = 1 TO 5 \t\t W$ = \\\"PATAN\\\" FOR J = 5 TO I STEP -1 \t\t T = 1 PRINT I; \t\t FOR I = LEN(W$) TO 1 STEP -1 NEXT \t\t PRINT TAB(T); RIGHT$(W$, I) PRINT \t\t T = T + 1 NEXT \t\t NEXT END SUB \t END SUB b. \t DECLARE FUNCTION RES (N) d. \t DECLARE FUNCTION RES$(D) \tCLS \tCLS \t PRINT RES(34125) \t N = 505 \tEND \t PRINT RES$(N) \t FUNCTION RES (N) \t FUNCTION RES$ (D) \t WHILE N > 0 \t R = N MOD 10 \t\t WHILE D >= 1 \t X = X * 10 + R \t N = N \\\\ 10 \t\t R = D MOD 8 WEND RES = X \t\t R$ = STR$(R) \t END FUNCTION \t\t R$ = LTRIM$(R$) Output:...................................................... \t\t O$ = R$ + O$ \t\t D = D \\\\ 8 \t\t WEND \t\t RES$ = O$ \t END FUNCTION Output:...................................................... 276 Approved by Curriculum Development Center (CDC)","Smart Computer Science Book-10 e.\t DECLARE FUNCTION xyz(B) g. \t DECLARE SUB XYZ() \tCLS \tCLS \t B = 10101 \t DIM N(3, 3) \t RES = xyz(B) \t FOR I = 1 TO 3 \t PRINT RES \t \t FOR J = 1 TO 3 \tEND \t\t READ N(I, J) \t FUNCTION xyz (B) \t \t NEXT \t B$ = STR$(B) \tNEXT \t B$ = LTRIM$(B$) \t CALL XYZ(N()) \t FOR I = LEN(B$) TO 1 STEP -1 \t DATA 10,20,30 \t X$ = MID$(B$, I, 1) \t DATA 40,50,60 \t x = VAL(X$) \t DATA 70,80,90 \t D=D+x*2^P \tEND \t P=P+1 \t NEXT \t SUB XYZ (N()) \t xyz = D \t FOR I = 1 TO 3 \t FOR J = 1 TO 3 \t END FUNCTION \t\t IF I = J THEN S = S + N(I, J) \t NEXT Output:................................................................. \t NEXT \t PRINT S f. \t DECLARE FUNCTION(N$) CLS \t END SUB N$ = \\\"76\\\" Output:................................................................. PRINT xyz(N$) END h.\t DECLARE SUB xyz() \tCLS FUNCTION xyz (N$) \t CALL xyz FOR I = LEN(N$) TO 1 STEP -1 \tEND X$ = MID$(N$, I, 1) X = VAL(X$) \t SUB xyz D=D+X*8^P \t\t F = 1 P=P+1 \t\t FOR I = 1 TO 5 NEXT \t\t F = F * I xyz = D \t\t NEXT I \t\t PRINT F END FUNCTION \t END SUB Output:................................................................. Output:................................................................. Approved by Curriculum Development Center (CDC) 277","Modular Programming i.\t DECLARE SUB xyz() k. \t DECLARE SUB xyz() \tCLS \tCLS \t N = 18 \t CALL xyz \t CALL xyz(N) \tEND \tEND \t SUB xyz \t SUB xyz (N) \t\t FOR I = 1 TO 5 \t\t READ N \t\t D = 2 \t\t X = N ^ (1 \/ 3) \t\t Y = INT(X) \t\t DO UNTIL N <= 1 \t\t IF X = Y THEN PRINT N; \t\t NEXT I \t\t 10 R = N MOD D \t\t DATA 3,10,27,8,64,35 \t\t IF R = 0 THEN \t END SUB \t\t\t PRINT D; Output:................................................................. \t\t \t N = N \\\\ D l. \t DECLARE SUB xyz(N) \tCLS \t\t ELSE \t CALL xyz(55) \tEND \t\t\t D=D+1 \t SUB xyz (N) \t\t\t GOTO 10 \t\t O = N \t\t DO UNTIL N <= 0 \t\t END IF \t\t R = N MOD 10 \t\t LOOP \t\t RV = RV * 10 + R \t\t N = N \\\\ 10 \t\t END SUB \t\t LOOP \t\t PRINT RV Output:................................................................. \t END SUB j.\t DECLARE FUNCTION xyz(N) Output:................................................................. \tCLS \t N = 153 m. \tDECLARE FUNCTION xyz(A,B) \t PRINT xyz(N) \tCLS \tEND \t PRINT xyz(8, 12) \t FUNCTION xyz (A, B) \t FUNCTION xyz (N) \t\t I = 1 \t\t 10 R = A MOD B \t\t WHILE N >= 1 \t\t IF R = 0 THEN \t\t R = N MOD 10 \t\t S = S + R ^ 3 \t\t H = B \t\t N = N \\\\ 10 \t\t ELSE \t\t WEND \t\t xyz = S \t END FUNCTION Output:................................................................. 278 Approved by Curriculum Development Center (CDC)","Smart Computer Science Book-10 \t\t A = B p.\t DECLARE FUNCTION xyz() \t\t B = R \tCLS \t\t GOTO 10 \t A = 6: B = 8 \t\t END IF \t PRINT xyz \t\t xyz = H \tEND \t END FUNCTION \t FUNCTION xyz Output:................................................................. \t\t SHARED A, B \t\t P = A * B n. \t DECLARE SUB xyz () \t\t LAB: \t\t R = A MOD B \tCLS \t\t IF R = 0 THEN \t\t H = B \t CALL xyz \t\t ELSE \t\t A = B \tEND \t\t B = R \t\t GOTO LAB \t SUB xyz \t\t END IF \t\t L = P \/ H \t X=1 \t\t xyz = L \t Y=1 \t END FUNCTION \t FOR I = 1 TO 10 Output:................................................................. \t PRINT X; \t\t\t Z=X+Y \t X=Y \t Y=Z \t NEXT \t END SUB Output:................................................................. q. \t DECLARE SUB xyz() \t COMMON SHARED N o.\t DECLARE FUNCTION xyz() \tCLS \tCLS \t CALL xyz \t COMMON SHARED A, B \tEND \t A = 8: B = 6 \t PRINT xyz \t SUB xyz \tEND \t\t N = 12 : D = 2 \t\t DO UNTIL N <= 1 \t FUNCTION xyz \t\t 10 R = N MOD D \t\t WHILE B <> 0 \t\t R = A MOD B \t\t IF R = 0 THEN \t\t A = B \t\t B = R \t\t\t PRINT D; \t\t WEND \t\t H = A \t\t\t N=N\\\\D \t\t xyz = H \t\t ELSE \t END FUNCTION \t\t\t D=D+1 \t\t\t GOTO 10 \t\t END IF \t\t LOOP END SUB Output:................................................................. Output:................................................................. Approved by Curriculum Development Center (CDC) 279","Modular Programming u. \t DECLARE FUNCTION xyz$(N) \tCLS r.\t DECLARE SUB xyz(W$) \t X = 4563 \t W$ = \\\"SAGARMATHA\\\" \t PRINT xyz$(X) \t CALL xyz(W$) \tEND \tEND \t FUNCTION xyz$ (N) \t SUB xyz(W$) \t\t N$ = STR$(N) \t\t FOR I = 1 TO LEN(W$) \t\t N$ = LTRIM$(N$) \t\t X$ = MID$(W$, I, 1) + X$ \t\t FOR I = 1 TO LEN(N$) \t\t NEXT \t\t X$ = MID$(N$, I, 1) + X$ \t\t PRINT X$ \t\t NEXT \t\t PRINT X$ \t END SUB \t END FUNCTION Output:................................................................. Output:................................................................. s.\t DECLARE SUB xyz(W$) v. \t DECLARE SUB series (A, B) \tCLS \t X$ = \\\"KATHMANDU\\\" \t CALL series(1, 1) \t CALL xyz(X$) \tEND \tEND \t SUB xyz (W$) \t SUB series (A, B) \t\t FOR I = 1 TO LEN(W$) STEP 2 \t\t X$ = MID$(W$, I, 1) + X$ \t\t CLS \t\t NEXT \t\t PRINT X$ \t\t C = 0 \t END SUB \t\t J=1 Output:................................................................. \t\t WHILE J<=5 t.\t DECLARE FUNCTION xyz(N) \tCLS \t\t PRINT A; \t N = 456 \t PRINT xyz(N) \t\t\t C=A+B \tEND \t FUNCTION xyz (N) \t\t\t A=B \t N$ = STR$(N) \t\t\t B=C \t N$ = LTRIM$(N$) \t FOR I = 1 TO LEN(N$) \t\t\t J=J+1\t \t X$ = MID$(N$, I, 1) \t\t WEND \t x = VAL(X$) \t S=S+x \t END SUB \t NEXT \t xyz = S Output:................................................................. \t END FUNCTION DECLARE SUB XYZ () Output Output:................................................................. CLS CALL XYZ END SUB XYZ \t FOR I = 1 TO 5 280 Approved by Curriculum Development Center (CDC)","Smart Computer Science Book-10 \t\t FOR J = 1 TO I a. Write the output of the program. \t\t PRINT J MOD 2; Ans: ...................................................................... \t\t NEXT J b. List out the parameters and arguments? \t\t PRINT \t NEXT I Ans: ...................................................................... END SUB ......................................................................... 9. \t Study the following programs and answer c. When is value of X printed? the asked questions: Ans: ...................................................................... i) \t DECLARE SUB xyz(X) \tCLS iii) \tDECLARE SUB xyz() \t FOR I= 1 TO 10 \tCLS \t CALL xyz \t\t READ N \tEND \t\t CALL xyz(N) \t SUB xyz \t NEXT i \t DATA 7,15,6,10,15,32,8,22,25,5 \t\t H = 0 \tEND \t\t FOR I = 1 TO 5 \t SUB xyz (X) \t\t READ N \t\t IF X MOD 5 <> 0 THEN PRINT X; \t\t IF N > H THEN H = N \t\t NEXT \t END SUB \t\t DATA 25,15,50,90,10 a. Write the output of this program. \t\t PRINT \t\t PRINT H Ans: ...................................................................... END SUB b. How many times SUB procedure will be called? a. Write the output of the program. Ans: ...................................................................... c. When is value of X printed? Ans: ...................................................................... b. List out the local and global variables. Ans: ...................................................................... Ans: ...................................................................... ii) \t DECLARE SUB xyz(A,B) \tCLS ......................................................................... \t x=4:y=6 c. What is the value of H on the completion of \t CALL xyz(x, y) \tEND loop? \t SUB xyz (A, B) Ans: ...................................................................... \t\t FOR I= A TO B \t\t PRINT A; B; iv)\t DECLARE FUNCTION xyz(N) \t\t A = A + B \tCLS \t\t B = B + A \t X = 55 \t PRINT xyz(X) \t\t NEXT I \tEND \t END SUB \t FUNCTION xyz (N) Approved by Curriculum Development Center (CDC) 281","Modular Programming \t\t DO \t\t X = INT(17 \/ N) \t\t Y = 15 MOD N \t\t R = N MOD 10 \t\t NUM = X + Y \t END FUNCTION \t\t S = S + R a. List out the local and global variables? \t\t N = INT(N \\\\ 10) Ans: ...................................................................... b. \t How many mathematical (library) functions are \t\t LOOP WHILE N <> 0 used in this program. \t xyz = S Ans: ...................................................................... \t END FUNCTION c. \t Write the output of this program. a. What is the purpose of this program? Ans: ...................................................................... Ans: ...................................................................... b. List out the actual parameters and formal parameters. Ans: ...................................................................... ......................................................................... vii) DECLARE SUB XYZ(N) \tCLS v)\t DECLARE FUNCTION XYZ(N) \t A = 55 \t FOR I = 1 TO 5 \t CALL XYZ(A) \tEND \t\t READ N \t SUB XYZ (N) \t\t Z = XYZ(N) \t\t S = S + Z \t\t S = 0 \t NEXT I \t\t WHILE N <> 0 \t PRINT S \t DATA 10,13,15,4,6 \t\t R = N MOD 10 \tEND \t\t S = S * 10 + R \t\t N = N \\\\ 10 \t FUNCTION XYZ (N) \t\t WEND \t\t IF N MOD 2 = 0 THEN XYZ = N \t\t PRINT S \t\t END SUB \t END FUNCTION a. \t What is the name of function? a.\t List out the arguments and parameters. Ans: ...................................................................... Ans: ...................................................................... b. \t How many times function will be called? b. \t What is the purpose of this program? Ans: ...................................................................... Ans: ...................................................................... vi) \tDECLARE FUNCTION NUM () c. \t How many times loop repeat? \t COMMON SHARED N \t N=5 Ans: ...................................................................... \t S = NUM d. \t Write the output of this program? \t PRINT S Ans: ...................................................................... \tEND \t FUNCTION NUM () 282 Approved by Curriculum Development Center (CDC)","File handling in Q-BASIC Smart Computer Science Book-10 Chapter Includes CHAPTER \u2022\t Introduction to data, data file and program file \u2022\t Basic file management 15 \u2022\t Open sequential file in different modes \u2022\t Writing, reading and adding data into the file \u2022\t File management commands \u2022\t Updating records in the data file INTRODUCTION Data is a term that refers to a collection of raw informations which are supplied to the computer for further processing and producing the meaningful result. The file is a collection of related data, program or records stored on a storage medium under a unique name. File Handling is a process to create a data file, write data to the data file and read data from data file as well as delete and update data from data file. QBASIC supports two types of files: i) Program File\t ii) Data File Program File The program file is a collection of set of instructions written in a computer language for data processing under unique file name. These instructions are used to manipulate data and produce the meaningful output. Program file of Q-basic program adds the .BAS extension. Data File In QBASIC, data is stored in a separate file called a data file that is stored in the secondary storage device separately for future use. Data file contains related data like name, number, address, salary, post, etc. Generally It is added .dat extension name with data file but it is not compulsory. Advantages of Data file a. Reusability : Once created data file can be reused. b. Large data storage capacity: Using files, you need not worry about the problem of storing data in bulk. c. Saves time: There are certain programs that require a lot of input from the user. You can easily access any part of the code with the help of certain commands. Approved by Curriculum Development Center (CDC) 283","File handling in Q-BASIC d. Portability: You can easily transfer the contents of a file from one computer system to another without having to worry about the loss of data. e. Can be edited: Once the data file is created, data can be stored, modified or deleted. Types of Data File QBASIC supports two types of data file: i) Random Access Data File: Random access files are recorded-based files with an internal structure that supports \\\"direct access\\\" by record number. This means that your program can read from or write to a specific record in a random access file, say to read the 50th record, without reading through the previous 49 records, it reads that specific record. ii) Sequential Data File: In sequential access data file, you can read and write data in sequential order. Suppose you have to access 100th record, then you need to read 1 to 99 records unnecessary. So, if the file is of a large size, it takes more time to access the data. But it is easy to create sequential data file. Here we discuss about the sequential access data file management. BASIC FILE MANAGEMENT PROCESS There are major four functional processes in the sequential data file management system, which are: i) Create a new data file\t\t\t ii) Write data into the file iii) Read data from the specified data file\t iv) Editing data from the existing file Opening a Sequential Data File Opening data file is the most essential tool, without opening a data file, it is impossible to perform any functional management in the data file. The sequential file can be open in three different modes on the basis of functional operation. Mode Use i) Output or \\\"O\\\" mode : For writing\/recording\/storing data into the file. ii) Input or \\\"I\\\" mode: For reading data from the file for further processing. iii) Append or \\\"A\\\" mode : For adding data to end of the file. OPEN Statement Function\t: This statement is used to open a sequential data file for writing, reading and appending data operation. It is an I\/O statement. Syntax\t\t: OPEN \u201cFile name\u201d [FOR Mode] AS [#file number]. \t\t\t or \t\t OPEN <Mode>, #file number, <\u201cFile name\u201d> \u2022\t FOR is the clause for specific operation 284 Approved by Curriculum Development Center (CDC)","Smart Computer Science Book-10 \u2022\tFile name is any QBASIC valid file name. It can be included along the path for the file. \u2022\tMode represents three possible modes INPUT, OUTPUT and APPEND. \u2022\tAS is the clause for supplying the file number. File number ranges from 1 to 255. \u2022\tFile number represents the label of file. Examples:\t a. OPEN \u201cTEST.DAT\u201d FOR OUTPUT AS #1 \t\t b. OPEN\u201cD:\\\\PROGRAMS\\\\TEST.DAT\u201d FOR OUTPUT AS #1 \t\t c. OPEN \u201cO\u201d, #1, \u201cTEST.DAT\u201d \t\t d. OPEN \u201cO\u201d, #2, \u201cD:\\\\PROGRAM\\\\TEST.DAT\u201d \t\t e. OPEN \u201cTEST.DAT\u201d FOR INPUT AS #1 \t\t f. OPEN \u201cD:\\\\PROGRAMS\\\\TEST.DAT\u201d FOR INPUT AS #1 \t\t g. OPEN \u201cI\u201d, #1, \u201cTEST.DAT\u201d \t\t h. OPEN \u201cI\u201d, #1, \u201cD:\\\\PROGRAM\\\\TEST.DAT\u201d \t\t i. OPEN \u201cTEST.DAT\u201d FOR APPEND AS #1 \t\t j. OPEN\u201cD:\\\\PROGRAMS\\\\TEST.DAT\u201d FOR APPEND AS #1 \t\t k. OPEN \u201cA\u201d, #1, \u201cTEST.DAT\u201d \t\t l. OPEN \u201cA\u201d, #1, \u201cD:\\\\PROGRAM\\\\TEST.DAT\u201d WRITING DATA INTO THE FILE Writing data refer to store or record data in the new created file which is open in the output or append mode. To perform this task, WRITE statement is used. WRITE Statement Function\t: \tIts function is to store or record data in the data file. It can be used with \\\"Output\\\" and \\\"Append\\\" mode. Syntax\t : WRITE [#file number], expression list \u2022\tExpression list is one or more variables separated by comma. \u2022\tFile number is the number used with 'OPEN' statement. Example\t:\t WRITE #1, N$, P$, D$, S \u2022\tIn this example, it stores the data in N$, P$, D$ and S variables in the file open in #1 number. CLOSE Statement Function\t: \tIts function is to close the open data file. Syntax\t : CLOSE [#file number], [#file number]...... \u2022\tFile number is the number used with 'OPEN' statement. Example\t:\t CLOSE #1 \u2022\tIn this example, close the opened data file which is opened in number #1. Approved by Curriculum Development Center (CDC) 285","File handling in Q-BASIC Sample Programs \u2022\t Write a program to create a data file \u2022\t Write a program to create a data file named \u201cADDRESS.DAT\u201d to store named \u201cSALARY.DAT\u201d to store name, name, address, and phone number of department, post and salary of number 10 students. of employees as you like. CLS CLS OPEN \\\"O\\\", #1, \\\"ADDRESS.DAT\\\" OPEN \\\"O\\\", #1, \\\"SALARY.DAT\\\" FOR I=1 TO 10 DO \t INPUT \\\"Enter Name:\\\"; N$ \t INPUT \\\"Enter Name:\\\"; N$ \t INPUT \\\"Enter Address:\\\"; A$ \t INPUT \\\"Enter Department:\\\"; D$ \t INPUT \\\"Enter Phone Number:\\\"; PH \t INPUT \\\"Enter Post:\\\"; P$ \t WRITE #1, N$, A$, PH \t INPUT \\\"Enter Salary:\\\"; S NEXT I WRITE #1, N$, D$, P$,S CLOSE #1 INPUT \\\"Do you want to continue ..[Y\/N]\\\"; X$ END LOOP WHILE X$ = \\\"Y\\\" OR X$ = \\\"y\\\" CLOSE #1 END SKILL TEST-1 1. \t Write a program to store name of item, quantity and rate in the data file \\\"ITEM.DAT\\\" as you like. 2. \t Write a program to create a data file \\\"RES.DAT\\\" and store name, maths, computer and science marks for a few students. 3.\t Create a sequential data file \u201cHOTEL.DAT\u201d to store customer's name, citizen, address and phone number. The program should terminate with user\u2019s choice. 4.\t Write a program to create a data file \\\"LIBRARY.DAT\\\" and store book code, author name and number of books. The program should terminate with your choice. 5. Write a program to create a data file INFO.DAT and record name of students, gender, class and section. READING DATA FROM SEQUENTIAL FILE Once data are stored in the data file that can be read and used in the future as we like. To read the data from the sequential data file, follow these steps: Step-1 : Open the data file in 'Input mode'. Step-2 : Read the data from the file. Step-3 : Close the file. 286 Approved by Curriculum Development Center (CDC)","Smart Computer Science Book-10 INPUT # statement Function\t: \tIts function is to input data from the data file. It reads the data from the data file. Syntax\t : INPUT [#file number], list of variables...... \u2022\tFile number is the number used with 'OPEN' statement. \u2022\tList of variables are those variables contained values through the INPUT mode. Example\t:\t INPUT #1, N$, A$, PH \u2022\tThis example reads the data from file number #1. LINE INPUT # statement Function\t: \tIts function is to input entire line or maximum 255 characters long string from the data file containing spaces, punctuation and special characters. It reads the data from the data file. Syntax\t : LINE INPUT [#file number], list of variable...... \u2022\tFile number is the number used with 'OPEN' statement. \u2022\tList of variables are those variables receive values through the INPUT mode. Example\t:\t LINE INPUT #1, A$ \u2022\tThis example reads the entire line of address from the file number #1. EOF () Function Function\t: \tIts function is to check whether the record pointer is reached at the end of the file or not. It is a logical function. Syntax\t : EOF [#file number] \u2022\tFile number is the number used with 'OPEN' statement . Example\t:\tEOF(1) \u2022\tThis example tests the record pointer is reached at the end of data file or not that is opened with file number #1. PRINT Statement Function\t: \tIts function is to print numeric or string expressions on the screen. Typing shortcut ? will be converted to PRINT. Syntax\t : PRINT [expression] [;|,] [expression...] \u2022\tExpression is a numeric or string expression or values to be displayed onto the screen. Start and End quotes will not be displayed with strings. \u2022\tThe print statement can be followed by a semicolon to stop the print cursor or a comma to tab space for the next print. Example\t:\t PRINT N$, A$, PH \t\t PRINT \\\"Name\\\"; \\\"Ram\\\"; 20, 30 \u2022\tThis example prints the values stored in the respective variables N$, A$, P$, PH and mentioned values \\\"Name\\\"; \\\"Ram\\\"; 20, 30. Approved by Curriculum Development Center (CDC) 287","File handling in Q-BASIC Sample Programs \u2022\t Write a program to read and display all the data END IF from the \u201cADDRESS.DAT\u201d data file containing LOOP WHILE NOT EOF(1) name, address and phone number. CLOSE #1 END CLS OPEN \\\"I\\\", #1, \\\"ADDRESS.DAT\\\" \u2022\t Write a program to display only name and ad- DO dress having an address is \u201cKTM\u201d and \u201cPATAN\u201d from the \u201cADDRESS.DAT\u201d data file. Data file INPUT #1, N$, A$, PH containing name address and phone number. PRINT N$, A$, PH LOOP WHILE NOT EOF(1) CLS CLOSE #1 OPEN \\\"I\\\", #1, \\\"ADDRESS.DAT\\\" END DO \u2022\t Write a program to display those records having INPUT #1, N$, A$, PH address is \u2018KATHMANDU\u201d from the \u201cADDRESS. IF A$ = \\\"KTM\\\" OR A$ = \\\"PATAN\\\" THEN DAT\u201d data file containing name, address and phone number. PRINT N$, A$ END IF CLS LOOP WHILE NOT EOF(1) OPEN \\\"I\\\", #1, \\\"ADDRESS.DAT\\\" CLOSE #1 DO END INPUT #1, N$, A$, PH Logic Gate! IF A$ = \\\"KATHMANDU\\\" THEN \u2022\t We must take input all the data attributes to access data from the PRINT N$, A$, PH file even not to use. In the last example, it is printed only name and address, but accessed all the data; name, address and phone. \u2022\t Variable name and type must be matched with its output mode. SKILL TEST-2 1. \t Write a program to display all the records along with total amount from \\\"ITEM.DAT\\\" data file which contains item name, quantity and rate. 2. \t Write a program to display all the records along with total and percentage marks from \\\"RES.DAT\\\" data file which contains name of students and marks on math, computer and science. 3. \t Write a program to display all the records whose percentage marks above 80 % from \\\"RES.DAT\\\" data file which contains name of students and marks on math, computer and science. 4.\t Write a program to display all the records, accept address is \\\"BKT\\\" and \\\"KTM\\\" from \\\"HOTEL. DAT\\\" data file which contains customer's name, citizen, address and phone number. 5.\t Write a program to display all the records of only author name and number of books from data file \\\"LIBRARY.DAT\\\" which contains Book code, Author name and Number of books. 6. \t Write a program to display all the records of female from data file INFO.DAT which contains name of students, gender, class and section. 288 Approved by Curriculum Development Center (CDC)","Smart Computer Science Book-10 ADDING RECORDS IN SEQUENTIAL FILE In the sequential data file, adding data \t INPUT \\\"Enter Phone Number:\\\"; PH is known as to add records in the end of \t WRITE #1, N$, A$, PH the file. To perform this task, we should \t INPUT \\\"Do you want to Add more ..[Y\/N]\\\"; X$ open the file in the append mode. To LOOP WHILE X$ = \\\"Y\\\" OR X$ = \\\"y\\\" read the data from the sequential data CLOSE #1 file, follow these steps: END Step-1 : Open the data file in \\\"APPEND\\\" mode. \u2022\t Write a program to add more records Step-2 : Read the data from the keyboard. in the \u201cSALARY.DAT\u201d data file Step-3 : Write the data in the file. which contains name of employees, Step-4 : Make the choice to add more. department, post and salary. The Step-5 : Close the data file. program should stop in users' choice. CLS Sample Programs OPEN \\\"A\\\", #1, \\\"SALARY.DAT\\\" \u2022\t Write a program to add more records DO in the \u201cADDRESS.DAT\u201d data file which \t INPUT \\\"Enter Name:\\\"; N$ contains name of students, address \t INPUT \\\"Enter Department:\\\"; D$ and phone number. The program \t INPUT \\\"Enter Post:\\\"; P$ termination should be in users' choice. \t INPUT \\\"Enter Salary:\\\"; S CLS WRITE #1, N$, D$, P$,S DO INPUT \\\"Do you want to continue ..[Y\/N]\\\"; X$ \t OPEN \\\"A\\\", #1, \\\"ADDRESS.DAT\\\" LOOP WHILE X$ = \\\"Y\\\" OR X$ = \\\"y\\\" \t INPUT \\\"Enter Name:\\\"; N$ CLOSE #1 \t INPUT \\\"Enter Address:\\\"; A$ END SKILL TEST-3 1. \t Write a program to add more records as you like in the data file \\\"ITEM.DAT\\\" which contains name of item, quantity and rate. 2. \t Write a program to add more records with user's choice in \\\"RES.DAT\\\" data file which contains name of students and marks on math, computer and science. 3.\t Write a program to add more records in data file \u201cHOTEL.DAT\u201d which contains customer's name, citizen, address and phone number. The program should terminate with user\u2019s choice. 4.\t Write a program to add more records in \\\"LIBRARY.DAT\\\" data file which contains Book code, Author name and Number of books. The program should terminate with user's choice. 5. \t Write a program to add more records in INFO.DAT data which contains name of students, gender, class and section. The program should terminate on the basis of user's choice. Approved by Curriculum Development Center (CDC) 289","File handling in Q-BASIC FILE MANAGEMENT STATEMENT & FUNCTION INPUT$() Function Function\t: \tIts function is to return a specified number of characters from the string or data file. Syntax\t : PRINT (n[# file number]) \u2022\tn represents the number of characters to be read. \u2022\tFile number is the number used with 'OPEN' statement. Example\t:\t INPUT$(5, #1) \u2022\tIt reads 5 characters from the file opened in the file number #1. \t\tINPUT$(5) \u2022\tIt reads 5 characters from the keyboard. PRINT # Statement Function\t: \tIts function is to write data to a sequential file. Syntax\t : PRINT [# file number], expression list. \u2022\tExpression lis is one or more string constant, variable or expression. \u2022\tFile number is the number used with 'OPEN' statement. Example\t:\t PRINT #1, N$,A$, PH \u2022\tIt writes data in the file which is opened in file number #1.\t\t Sample Programs CLS OPEN \\\"O\\\", #1, \\\"ABC.DAT\\\" PRINT #1, \\\"SAGARMATHA\\\" CLOSE #1 OPEN \\\"I\\\", #1, \\\"ABC.DAT\\\" PRINT INPUT$(5, # 1) CLOSE #1 \u2022\tIn this program, it writes 'Sagarmatha' in ABC.DAT data file and returns five characters END as given in the picture. NAME ...AS Statement Function\t: \tIts function is to rename a file of the disk. Syntax\t : NAME old file name AS new file name \u2022\tThe old file name is an already existed file, if any drive and folder specification is required, we must mention the path. \u2022\tNew name is any valid file name which will exist in the same path where old file is existed. Example\t:\t NAME \\\"XYZ.DAT\\\" AS \\\"ABC.DAT\\\" \t\t NAME \\\"D:\\\\QBPROG\\\\XYZ.DAT\\\" AS \\\"ABC.DAT\\\" 290 Approved by Curriculum Development Center (CDC)","Smart Computer Science Book-10 KILL Statement Function\t: \tIts function is to delete a file from the disk. Syntax\t : KILL file specification \u2022\tFile specification is a file name to be deleted which is the string expression. It may be followed by a path. Example\t:\t KILL \\\"XYZ.DAT\\\" \t\t KILL \\\"D:\\\\QBPROG\\\\XYZ.DAT\\\" SHELL Statement Function\t: \tIts function is to run a DOS command. Syntax\t : SHELL [Command string] \u2022\tCommand string is any DOS command enclosed by inverted comma. Example\t:\t SHELL \\\"DIR\/P\\\" \u2022\tIt displays files and subdirectories of the current directory in page wise format. SYSTEM Statement Function\t: \tIts function is to close the QBASIC running windows. Syntax\t : \tSYSTEM Example\t:\t SYSTEM FILES Statement Function\t: \tIts function is to display a list of files residing on the specified disk. Syntax\t : \tFILES <files specification> Example\t:\t FILES \t\t FILES \\\"D:\\\\QBPRG\\\\*.DAT\\\" MKDIR Statement Function\t: \tIts function is to create a new directory. Syntax\t : \tMKDIR <directory specification> [directory name] \u2022\tDirectory specification represents path or location where the new directory to be created. Example\t:\t MKDIR \\\"XYZ\\\" \u2022\tIt creates new directory \\\"XYZ\\\" in current location. \t\t MKDIR \\\"D:\\\\QBPRG\\\\XYZ\\\"\t \t \u2022\tIt creates new directory \\\"XYZ\\\" under QBPROG directory of D: drive. CHDIR Statement Function\t: \tIts function is to change the current directory. Syntax\t : \tCHDIR <directory specification> [directory name] Approved by Curriculum Development Center (CDC) 291","File handling in Q-BASIC \u2022\tDirectory specification represents path or location where new directory to be created. Example\t:\t CHDIR \\\"XYZ\\\" \u2022\tIt changes new directory \\\"XYZ\\\" in current location. \t\t CHDIR \\\"D:\\\\QBPRG\\\\XYZ\\\"\t \t \u2022\tIt changes new directory \\\"XYZ\\\" under QBPROG directory of D: drive. RMDIR Statement Function\t: \tIts function is to remove the directory. Syntax\t : \tRMDIR <directory specification> [directory name] \u2022\tDirectory specification represents path or location from where it is to be removed. Example\t:\t RMDIR \\\"XYZ\\\" \u2022\tIt removes directory \\\"XYZ\\\" of current location. \t\t RMDIR \\\"D:\\\\QBPROG\\\\XYZ\\\"\t \t \u2022\tIt removes directory \\\"XYZ\\\" from a QBPROG directory of D: drive. Copying Data Copying data is known as the transfer data from one file to another file. Sometimes it may come the situation to perform this task. How to copy data from one file to another file is illustrated in the following sample programs. Sample Programs \u2022\t Write a program to create a sequential data \u2022\t Write a program to create a sequential data file file \u201cFAILED.DAT\u201d and transfer those records \u201cFEMALE.DAT\u201d and transfer all the female's having average is below 40 from \u201cRES.DAT\u201d records from \\\"INFO.DAT\\\" data file which data file in the PROG sub directory of D: drive. contains name of students, gender, class and The file contains name of students, math, section. computer and science marks of few students. CLS CLS OPEN \\\"INFO.DAT\\\" FOR INPUT AS #1 OPEN \\\"D:\\\\PROG\\\\RES.DAT\\\" FOR INPUT AS #1 OPEN \\\"FEMALE.DAT\\\" FOR OUTPUT AS #2 OPEN \\\"D:\\\\PROG\\\\FAILED.DAT\\\" FOR OUTPUT AS #2 WHILE NOT EOF(1) WHILE NOT EOF(1) INPUT #1, N$, G$, CL, SEC$ INPUT #1, N$, M, C, S IF UCASE$( G$)=\\\"FEMALE\\\" THEN T=M+S+C \t\t WRITE #2, N$, G$, CL, SEC$ AV = T \/ 3 \t END IF IF AV < 40 THEN WRITE #2, N$, S, C WEND WEND PRINT \\\"Data are copied...\\\" PRINT \\\" Data are copied...\\\" CLOSE #1, #2 CLOSE #1, #2 END END 292 Approved by Curriculum Development Center (CDC)","Smart Computer Science Book-10 SKILL TEST-4 1. \t Write a program to create a sequential data file \u201cCITIZEN.DAT\u201d and transfer all the Indian citizen's records from \\\"HOTEL.DAT\\\" data file which contains customer's name, citizen, address and phone number. 2.\t Write a program to create a sequential data file \u201cSECTION.DAT\u201d and copy all the records having section 'A' of class 10 students from \\\"INFO.DAT\\\" data file which contains name of students, gender, class and section. Finally, the program should display message that how many records are copied. 3. \t Write a program to create a sequential data file \u201cNILL.DAT\u201d and transfer all the records having no more books available for current issue from \\\"LIBRARY.DAT\\\" data file which contains Book code, Author name and Number of books. Finally, the program should display message as to how many records are copied. Editing or Updating Data CLS OPEN \\\"RES.DAT\\\" FOR INPUT AS #1 Sometimes there may be stored wrong OPEN \\\"DUMMY.DAT\\\" FOR OUTPUT AS #2 data due to different causes or some WHILE NOT EOF(1) times data can be varied time to time in the data file which leads to make the INPUT #1, N$, M, S, C correction or modification that is known N$ = UCASE$(N$) as the updating data. IF N$ = \\\"SUMI\\\" THEN N$ = \\\"SUMIT\\\" WRITE #2, N$, M, S, C Sample Programs WEND CLOSE #1, #2 \u2022\t There is wrong spelling in name \u201cSUMI\u201d, it KILL \\\"RES.DAT\\\" should be \u201cSUMIT\u201d in the \u201cRES.DAT\u201d data file NAME \\\"DUMMY.DAT\\\" AS \\\"RES.DAT\\\" which contains name of students and marks on END math, computer and science. Write a program to make necessary correction of this name. SKILL TEST-5 1.\t Nepal Government has increased the sales tax in every sales item by 2.5 %, so it is essential to in- crease the sales rate with the same percentage of ITEM.DAT data file which contains name of item, quantity and rate. Write an appropriate program to solve this problem. 2.\t There is a problem in mathematics marks of every student in 'RES.DAT\\\" data file which contains name of students and marks on math, computer and science. It is needed to increase the marks by 10 on mathematics for every one. Write a program to solve this problem. 3. \t It seems \\\"INDIN\\\" in the list of recorded data in citizen attribute of \\\"HOTEL.DAT\\\" data file. It should be 'INDIAN\\\". The file contains guests' name, citizen, address and phone number. Write a program to make necessary correction. 4.\t Write a program for necessary modification in the \\\"Library.DAT\\\" data file which contains Book code, Author name, Number of books. When any book is issued, the number of books should be updated. 5.\t Write a program for necessary modification in the \\\"ITEM.DAT\\\" data file which contains name of item, quantity and rate. The program should add quantity of each item by 150. Approved by Curriculum Development Center (CDC) 293","File handling in Q-BASIC Deleting Records PRINT PRINT Unnecessary record\/s should be INPUT \\\"Do you want to delete [Y\/N] \\\"; X$ deleted from the file. To delete IF X$ <> \\\"Y\\\" AND X$ <> \\\"y\\\" THEN unnecessary records, follow these \t WRITE #2, N$, M, S, C steps: END IF WEND Step-1 : \tOpen the main data file in \\\"Input\\\" CLOSE #1, #2 mode. KILL \\\"RES.DAT\\\" NAME \\\"DUMMY.DAT\\\" AS \\\"RES.DAT\\\" Step-2 : Open the dummy data file in \\\"output\\\" END mode. \u2022\t Write a program to delete those records Step-2 : \tRead the data from the main data file having average marks are below 40 from the \u201cRES.DAT\u201d data file that you have created Step-3 : \tWrite the necessary data of the main during the Skill TEST-1 session. data file in the dummy data file. CLS Step-4 :\t If the end of the main file, close both OPEN \\\"RES.DAT\\\" FOR INPUT AS #1 files. OPEN \\\"DUMMY.DAT\\\" FOR OUTPUT AS #2 WHILE NOT EOF(1) Step-5 : \tDelete main file. INPUT #1, N$, M, S, C Step-6 : \tRename dummy data file as main AV = (M + S + C) \/ 3 data file. IF NOT (AV < 40) THEN Sample Programs WRITE #2, N$, M, S, C END IF \u2022\t Write a program to delete unnecessary data WEND from the \u201cRES.DAT\u201d data file which contains CLOSE the names of students and marks on math, KILL \\\"RES.DAT\\\" computer and science. NAME \\\"DUMMY.DAT\\\" AS \\\"RES.DAT\\\" PRINT \\\"Records are deleted.....\\\" CLS END OPEN \\\"RES.DAT\\\" FOR INPUT AS #1 OPEN \\\"DUMMY.DAT\\\" FOR OUTPUT AS #2 WHILE NOT EOF(1) INPUT #1, N$, M, S, C PRINT N$, M, S, C PRINT SKILL TEST-6 1.\t Write a program to delete unnecessary records from \\\"LIBRARY.DAT\\\" data file which contains Book code, Author name and Number of books. 2.\t Write a program to delete those records having citizen is \\\"INDIAN\\\" and \\\"AMERICAN\\\" from \\\"HOTEL.DAT\\\" data file which contains customer's name, citizen, address and phone number. 3. \t Write a program to delete those records having a gender is \\\"MALE\\\" from \\\"INFO.DAT\\\" data file which contains name of students, gender, class and section. 294 Approved by Curriculum Development Center (CDC)","Smart Computer Science Book-10 Points to Know \u2022\t Data as a term refers to a collection of raw informations which are supplied to the computer for further processing and produced the meaningful result. \u2022\t The file is a collection of related data, program or records stored on a storage medium under a unique name. \u2022\t The program file is a collection of set of instructions written in a computer language for data processing under unique file name. \u2022\t In QBASIC, you can store data in a separate file called a data file which is stored in the secondary storage device separately for future use. \u2022\t QBASIC supports two types of data file : Random Access file and Sequential Access file. \u2022\t The sequential file can be opened in three different modes :OUTPUT, INPUT and APPEND mode, which are represented by \\\"O\\\", \\\"I\\\" and \\\"A\\\" respectively. Terms to Know Data\t: Collection of raw information. Program\t: A collection of instructions that can be executed by a computer to perform a specific task. Instruction\t: An order given to a computer processor by a computer program. Output\t:\t Result generated by a computer is referred to as output. Input\t:\t To provide or give something to the computer. Sequential Access\t:\t Reading data one after another from beginning to end. Random Access\t :\t The ability to access data randomly. Worksheet Objective Questions 1. Fill in the blanks: a. \t ..................... is a collection of raw information. b. \t ....................... is a collection of related information under a unique name. c. \t LINE INPUT can receive maximum .................. characters long string. d. \t 'Adding records' always adds records to the ............................. of file. Approved by Curriculum Development Center (CDC) 295","File handling in Q-BASIC e. \t To read the data file, it should be opened in ......................... mode. f. \t To record the data in the sequential file, the file should be opened in ............... mode. g.\t ............................ mode is used to add records in the sequential file. 2. Write 'T' for true and 'F' for false statements: a. \tQBASIC program file automatically adds .BAS extension. b. \tData file automatically adds .DAT extension. c.\t Data file is a separate file than the program file. d.\t Once created data file can be reused. e. \tYou can easily transfer data file from one system to another. f.\t INPUT$ () is used to get input from the specified number of characters. 3. Mark the correct option: ii) Data File\t \t iv) None of these a. \t QBASIC supports: i) Program File\t\t\t\t\t iii) Both i) & ii)\t\t\t\t\t b. \t Data can be: \t i) Modified\t\t\t\t\t ii) Copy\t \t iii) Delete\t\t\t\t\tiv) All of these c. \t QBASIC supports ii) Sequential Access\t \t \ti) Random Access file\t\t\t\t iv) None of these iii) Both i) & ii)\t\t\t\t\t d. \t Data file can be opened in: \ti) OUTPUT mode\t\t\t\t\tii) INPUT mode\t\t iii) APPEND mode\t\t\t\t iv) All of these e. \t File number ranges from: ii) 1 to 255\t \t \ti) 0 to 255\t\t\t\t\t iv) 1 to 150 iii) 1 to 256\t\t\t\t\t f. \t To get input data from file: ii) INPUT \t \t \ti) INPUT #\t\t\t\t\t iv) LINE INPUT iii) INPUT $\t\t\t\t\t 296 Approved by Curriculum Development Center (CDC)","Smart Computer Science Book-10 g. \t EOF is a \ti) Statement\t\t\t\t\tii) Function\t\t iii) Both i) & ii)\t\t\t\t\t v) None of these h. \t ? represents \ti) PRINT \t\t\t\t\t ii) PRINT #\t \t iii) PRINT$\t\t\t\t\tv) INPUT i. \t The command used to record data in sequential file. \ti) WRITE# \t\t\t\t\t ii) PRINT #\t \t iii) Both i) & ii)\t\t\t\t\t v) None of these Descriptive Questions 1. Write short answer of the following questions: a.\t What is program file? b.\t What is data file? c.\t Differentiate between random access file and sequential access file. d. \t Write the advantages of data file. e.\t Why do you use EOF ? f.\t Write the major operation of the sequential access file. g. \t Differentiate between INPUT# and INPUT. h.\t Differentiate between LINE INPUT# and INPUT# . i.\t Why do you use the file number? j.\t Differentiate between INPUT mode and OUTPUT mode. k. \t Differentiate between OUTPUT mode and APPEND mode. l. \t Differentiate between WRITE# and PRINT# 2. Write the function and syntax of the following statements: a. CLOSE \t b. KILL \t\t\t c. NAME... AS..\t\t d. RMDIR e. FILES\t f. OPEN\t\t g. MKDIR\t\t h. CHDIR\t Approved by Curriculum Development Center (CDC) 297","File handling in Q-BASIC Case Study and Application-based Questions 1. Study the following program and answer the asked questions: A. OPEN \u201cDETAIL.DAT\u201d FOR INPUT AS #1 Ans: ........................................................ \t OPEN \u201cTEMP.DAT\u201d FOR OUTPUT AS #2 ................................................................ \t INPUT \u201cNAME OF THE STUDENTS:\u201d;NM$ \t FOR I= 1 TO 10 ii) \tIn place of OR, if you write AND, what \t INPUT #1, N$,C,A difference does it make? \t IF NM$<> N$ THEN \t\t WRITE #2, N$,C,A Ans: ........................................................ \t\t END IF ................................................................ \t NEXT I \t CLOSE #1,#2 iii)\t Why is INPUT followed by #1? \t KILL \u201cDETAIL.DAT\u201d \t NAME \u201cTEMP.DAT\u201d AS \u201cDETAIL.DAT\u201d Ans: ........................................................ \tEND ................................................................ i) \t What is the main objective of the above iv)\t What is the purpose of this program? program? Ans: ........................................................ Ans: ........................................................ ................................................................ v)\t Why is the DO.....LOOP statement placed? ................................................................ Ans: ........................................................ ii) \t If the data file \u201cDETAIL.DAT\u201d contains only ................................................................ 8 records, will the program run? If not, what will be the error message? Ans: ........................................................ C. CLS \t OPEN \\\"D:\\\\PROG\\\\INFO.DAT\\\" FOR INPUT AS #1 ................................................................ \t OPEN \\\"D:\\\\PROG\\\\DUMMY.DAT\\\" FOR OUTPUT AS B. CLS #2 \t OPEN \\\"I\\\", #1, \\\"ADDRESS.DAT\\\" \t WHILE NOT EOF(1) \tDO INPUT #1, N$, G$, AD$, PH INPUT #1, N$, A$, PH IF G$=\\\"MALE\\\" THEN IF A$ = \\\"KTM\\\" OR A$ = \\\"BKT\\\" THEN WRITE #2, N$, M, S, C PRINT N$, A$, PH END IF END IF \tWEND \t LOOP WHILE NOT EOF(1) \t CLOSE #1, #2 \t CLOSE #1 \t KILL \\\"D:\\\\PROG\\\\MARKS.DAT\\\" \tEND \t NAME \\\"D:\\\\PROG\\\\DUMY.DAT\\\" AS \\\"D:\\\\PROG\\\\INFO. i) \t If .DAT is removed from the file name, what DAT\\\" difference does it make? \tEND 298 Approved by Curriculum Development Center (CDC)","Smart Computer Science Book-10 i) \t What is the purpose of this program? ................................................................ Ans: ........................................................ ................................................................ ................................................................ ................................................................ ii) \t What is the location of the file? ................................................................ Ans: ........................................................ ................................................................ ................................................................ ................................................................ iii)\tIf the KILL statement is removed, what ................................................................ difference does it make? ................................................................ ................................................................ Ans: ........................................................ ................................................................ ................................................................ ................................................................ B. CLS ................................................................ \t OPEN \\\"I\\\", #1, \\\"POP.DAT\\\" \t WHILE EOF() iv)\t Why is NAME .... AS... statement used? Is it necessary? \t INPUT N$, G$, A$, T \t PRINT N$, G$, A$, T Ans: ........................................................ \tLOOP \t CLOSE (1) ................................................................ \tEND ................................................................ Ans: ....................................................... ................................................................ 2. Rewrite the following programs ................................................................ after correcting the bugs: ................................................................ ................................................................ A. CLS ................................................................ \tDO ................................................................ ................................................................ \t\t OPEN \\\"O\\\"; #1; \\\"POP.DAT\\\" \t\t INPUT \\\"ENTER NAME:\\\"; N$ \t\t INPUT \\\"ENTER GENDER:\\\"; G$ \t\t INPUT \\\"ENTER ADDRESS:\\\"; A$ \t\t INPUT \\\"ENTER TELEPHONE:\\\"; T \t\t WRITE #1 N$, G$, A$, T$ \t\t INPUT \\\"DO YOU NEED MORE [Y\/N]:\\\"; X$ \t LOOP WHILE X$ = \\\"Y\\\" AND X$ = \\\"Y\\\" \tCLOSE \tEND Ans: ....................................................... Approved by Curriculum Development Center (CDC) 299","File handling in Q-BASIC c. REM: TO ADD RECORDS and rate to the existing value. \tCLS k. \tTo delete unnecessary records from STOCK.DAT data file. \t OPEN \\\"O\\\", #1, \\\"POP.DAT\\\" \t Now, write programs for the \tDO Jogindar's department store for the above mentioned cases. INPUT \\\"ENTER NAME:\\\"; N$ 4.\t Shivani Gupta is a human resource INPUT \\\"ENTER GENDER:\\\"; G$ manager. She wants to make the computerized system in her INPUT \\\"ENTER ADDRESS:\\\"; A department. For this computerization, she wants some computer programs INPUT \\\"ENTER TELEPHONE:\\\"; T to solve the following problems. RECORD #1, N$, G$, A, T$ a. \tShe wants to create a sequential main data file EMPLOYEE.DAT and store Employee INPUT \\\"DO YOU NEED MORE [Y\/N]:\\\"; X$ ID, name, gender, department, Post, Salary. The program should terminate in \t WHILE X$ <> \\\"Y\\\" AND X$ = \\\"Y\\\" the user's choice. \tCLOSE b. When a new employee is hired, she wants to add a record in the EMPLOYEE.DAT file. \tEND c.\tSometimes, an employee may leave the 3. \tJogindar Yadav is a business man. company that would be deleted from the He is operating a departmental store data file. in Jankapur Dham, Nepal. Jogindar wants the computer program to d.\tShe wants to know how many employees perform the following tasks. are working in the company. a. \tTo create a sequential data file STCOCK. e.\tShe wants to display records of desired DAT and store Product code, name, department. quantity and rate of some items. f.\t She needs to display records of specified b. \tTo display all the contents of the STOCK. post. DAT data file. g.\t She wants to produce the list of specified c. \tTo display all the records of the STOCK. employees' genders and their post. DAT data file along with total amount of each item. Also add the feature to find the h. \tSometimes she has to submit the report of total amount of all the products. total expenditure in salary to the MD of the company. d. \tTo display the item code, quantity and quantity of STOCK.DAT data file. i.\t Sometimes she needs to copy the desired records in the new file. e. \tTo find out total types of products stored in the STOCK.DAT data file. j. When salary is increased, she needs to up- date records. f. \t To list out the records having quantity below 100. k.\t When salary is decreased, she has to up- date records. g. \tTo add more records in the STOCK.DAT data file. \t Now, write programs for the Shivani h.\t To copy the required records from STOCK. DAT in a new data file COPY.DAT. i.\t To update records by adding the quantity and rate to the existing value. j.\t To update records by subtracting quantity 300 Approved by Curriculum Development Center (CDC)"]


Like this book? You can publish your book online for free in a few minutes!
Create your own flipbook