(X) Ch-11 Modular Programming with sub procedure

  1. Determine whether each of the following statements are true or false.
  2. (a) The program’s entry point is the main-module, which is located at the top of other modules. TRUE (b) Arguments can be sent to the subprogram only by ‘reference’. FALSE (c) Arguments ‘sent by value’ is local to the sub-program. TRUE (d) Array arguments are always passed by value. FALSE (e) If the user does not declare sub-program or function, then QBASIC automatically generates a statements to declare it at the time of saving the program. TRUE (f) A sub-program name cannot end with data type suffix ($, % etc). TRUE (g) Procedures are used to divide programs into different sectors. TRUE
  3. Fill the blanks:
  4. (a) QBASIC is a modular programming language. (b) It is possible to use a single module in different places , which reduces the Program codes. (c) Arguments can be passed to the sub-program by value or Reference . (d) “Arguments by value” is sent to procedures by enclosing argument variable by Parenthesis. (e) Variables defined inside a sub-program are local by default. (f) Defining of a sub-program always begins with a SUB statement.
  5. Answer the following questions:
  6. (a) What is modular programming language? Write some advantages of it. Ans: The process of subdividing a computer program into separate sub-programs is called modular programming . A module is a separate software component. It can often be used in a variety of applications and functions with other components of the system. The advantage of Modular programming language are:- - Different programmer can design different programs modules independently, - It is easy to use,code and test, - It is possible to use a single module in different places, which reduces the program codes. (b) Why is QBASIC called modular programming language? Write the different modules supported by QBASIC. Ans: QBASIC is called modular programming language because it allows the user to divide program into manageable and functional modules or blocks with the help of sub procedure and function procedure. There are two modules supported by QBASIC.They are Sub and Function module. (c) Differentiate between a main-module and a sub-module. Ans:
    Main Module Sub Module
    1. It is generally started by declare statement. 1. It is started by SUB or FUNCTION Procedure.
    2. Generally, main module takes input from user. 2. Here, real processing takes place .
    (d) Define sub program. Write any four features of the sub program of QBASIC. Ans: A subprogram is a procedure which is used to divide a program into well-defined structures and fulfills the objective of a program. It use SUB-END SUB statement. Any four features of the SUB Procedures are as follows: 1. A call to a subprogram can pass a specific argument values for the subprogram to work with, which are called arguments. 2. Arguments can be passed to the subprogram reference or by value. 3. Arguments sent by reference serve as a medium for sending information back to the calling procedure. 4. Variables used inside a subprogram are local by default. (e) Differentiate between the arguments "passed by value" and "passed by reference". Ans:
    Passed By ValuePassed by Reference
    1. We use Passed By Value , When it is necessary to keep the original value of the argument variable even after Sub program or Function Call. 1. We use Passed By Reference , When it is not necessary to keep the original value of the argument variable even after Sub program or Function Call.
    2. To pass the “argument by value” the argument variable is enclosed in parenthesis. 2. To pass the “argument by reference ” the argument variable is not enclosed in parenthesis.
    (f) Differentiate between actual parameters and formal parameters. Ans:
    Actual ParameterFormal Parameter
    1. The parameters which are used to receive data (argument values) sent to the procedures are called actual parameter. 1. The variables which are used with DECLARE statement to declare variables types and number of data to be passed to procedure is called Formal Parameter.
    2. Actual parameter is also called argument or Real Parameter. 2. Formal parameter is also called parameter.
    (g) What is global variable? Give one example. Ans: A variable in main module which can be accessed from any modules or procedure of a program is called global variable. Variable can be made global declaring them with DIM SHARED or COMMON SHARED or SHARED attribute. DIM SHARED PIE HEere, PIE is global variable. (h) What is local variable? Give one example. Ans: A variable which is defined in a module and is not accessible to any other modules is called local variable. It is only accessible to the module where it is defined. Value of local variable is destroyed when execution of module is over. SUB ADD(X,Y) A=X+Y PRINT A END SUB In the above program, variable "A" is called local variable. By default its value is not accesssed from outside the procedure.
  7. Find the output of the following programs:
  8. (a) DECLARE SUB SUM (A,B) CLS A=5 B=4 C=6 D=7 CALL SUM(A,B) CALL SUM(C,D) END SUB SUM (x,y) S=(x^2)+(y^2) PRINT “SUM=”;S END SUB ANSWER: DRY RUN: S = 5^2+4^2=25+16=41 S=6^2+7^2=36+49=85 Hence the final output will be: SUM = 41 SUM = 85 (b) DECLARE SUB CAL (N) FOR J = 1 TO 3 READ N CALL CAL (N) NEXT J DATA 2,5,4 END SUB CAL (N) PRINT N^3 END SUB ANSWER: DRY RUN: 2^3 =8 5 ^ 3 = 125 4 ^ 3 = 64 Hence the final output will be: 8 125 64
  9. Debug the following programs:
  10. (a) REM program to print the greatest number DECLARE GRE SUB(A,B,C) INPUT "Enter 3 different numbers";A,B,C CALL GRE(A,B,C) END SUB GRE(A,B) IF A<B & A<C THEN A=G ELSE IF B>A & B>C THEN G=B ELSE C<A AND C<B THEN G=C END IF PRINT G; " IS GREATEST NUMBER" END SUB Answer: REM program to print the greatest number DECLARE SUB GRE (A,B,C) INPUT "Enter 3 different numbers";A,B,C CALL GRE(A,B,C) END SUB GRE(A,B,C) IF A>B AND A>C THEN G = A ELSEIF B>A AND B>C THEN G = B ELSE G = C END IF PRINT G; " IS GREATEST NUMBER" END SUB (b) REM print string in reverse form DECLARE SUB REV$(N$) CLS INPUT "Enter a string ";M$ CALL REV$(N$) END SUB REV(N$) FOR J = LEN$(N$) TO 1 STEP-1 C$=MID$(N$,1,J) W$=W$+C$ NEXT J PRINT W$ SUB END Answer: DECLARE SUB REV(N$) CLS INPUT "Enter a string ";M$ CALL REV(M$) END SUB REV(N$) FOR J = LEN(N$) TO 1 STEP-1 C$ = MID$(N$, J, 1) W$ = W$ + C$ NEXT J PRINT W$ END SUB
  11. Write the purpose and syntax of the following statements:
  12. (a) DECLARE Syntax: DECLARE SUB or FUNCTION Name ( Parameter List) Purpose: DECLARE statement is used to declare the sub or function program. (b) CALL Syntax: CALL sub-program name (Argument List) Purpose: CALL statement is used to transfers control to a SUB procedure. (c) SUB ... END SUB Syntax: SUB Sub-program name (parameter list) Statement Block END SUB Purpose: SUB ... END SUB statement is used to define a sub program. (d) DIM SHARED Syntax: DIM SHARED variable list Purpose: DIM SHARED is used to make global variable. (e) SHARED Syntax: SHARED variable list Purpose: Variables of sub procedure can be shared. (f) READ-DATA Syntax: READ Variable list DATA values of corresponding variable Purpose: READ-DATA is used to supply fixed values.
  13. Write down the programs for the following:
  14. (a) Write a program to create a sub program named SUM ( ), which accepts 3 different numbers and find their sum and average. Answer: DECLARE SUB SUM (A, B, C) CLS INPUT "Enter any three different numbers" ; A, B, C CALL SUM (A, B, C) END SUB SUM (A, B, C) S = A + B + C AVG = S / 3 PRINT " Sum of three different numbers is "; S PRINT " Average of three different numbers is " ; AVG END SUB (b) Write a sub procedure to find the volume of a box where V=L*B*H. Answer: DECLARE SUB VOL (L,B,H) CLS INPUT “Enter Length, Breadth and Height ” ; L,B,H CALL VOL (L,B,H) END SUB VOL (L,B,H) V = L*B*H PRINT "Volume of a box is ";V END SUB (c) . Write a program to create a sub-program named LNGST ( ) , which accepts three different name as parameters and prints the shortest name. Answer: DECLARE SUB LNGST (A$, B$, C$) CLS INPUT A$, B$, C$ CALL LNGST(A$, B$, C$) END SUB LNGST (A$, B$, C$) P = LEN(A$) L = LEN(B$) K = LEN(C$) IF P < L AND P < K THEN S$ = A$ ELSEIF L < P AND L < K THEN S$ = B$ ELSE S$ = C$ END IF PRINT S$ ; "is the smallest string." END SUB (d) Create a sub-program that prints area accepting the length and breadth of a rectangle. Answer: DECLARE SUB AREA (L,B) CLS INPUT "Enter Length and Breadth " ; L,B CALL AREA (L,B) END SUB AREA (L,B) A = L*B PRINT “Area of rectangle ” ; A END SUB (e) If the sum of the cube of the digits of a number is equal to the given number, then the number is called Armstrong Number. Write a sub program to check whether the input number is an Armstrong number or not. ( For example: 153 is an Armstrong number, because 153=1^3+5^3+3^3). [1,153,370,371,407] Answer: DECLARE SUB ARM (N) CLS INPUT "ENTER ANY NUMBER", N CALL ARM(N) END SUB ARM (N) M = N WHILE N < > 0 R = N MOD 10 S = S + R ^ 3 N = N \ 10 WEND IF S = M THEN PRINT M; "IS ARMSTRONG NUMBER" ELSE PRINT M ; "IS NOT ARMSTRONG NUMBER" END IF END SUB (f) Write a program to input 10 different numbers then find the smallest number using the sub procedure. Answer: DECLARE SUB SMALL(N()) CLS DIM N(10) FOR J= 1 TO 10 INPUT"ENTER THE NUMBERS";N(J) NEXT J CALL SMALL(N()) END SUB SMALL (N()) S=N(1) FOR K= 2 TO 10 IF N(K)<S THEN S=N(K) END IF NEXT K PRINT "Smallest number is ";S END SUB (g) Write a program to input 15 different numbers in the main module and then print only the even numbers with their sum using the sub-procedure. Answer: DECLARE SUB EVEN(N()) CLS DIM N(15) FOR J= 1 TO 15 INPUT"ENTER THE NUMBERS";N(J) NEXT J CALL EVEN(N()) END SUB EVEN(N()) FOR K= 1 TO 15 IF N(K) MOD 2 = 0 THEN PRINT N(K) S=S+N(K) END IF NEXT K PRINT "Sum is ";S END SUB (h) Write a program to input a string then print in alternate capital letters using the sub-procedure. Answer: DECLARE SUB ALTERNATE(A$) INPUT "Enter a string";A$ CALL ALTERNATE(A$) END SUB ALTERNATE(A$) FOR I = 1 TO LEN(A$) C$ = mid$(A$, I, 1) IF I MOD 2 = 0 THEN C$ = LCASE$(C$) ELSE C$ = UCASE$(C$) END IF ANS$=ANS$ + C$ NEXT I PRINT "String in alternate capital : "; ANS$ END SUB (i) Write a program to input a string and then count the total number of vowel characters of the string using sub-procedure. Answer: DECLARE SUB VCOUNT(W$) INPUT "Enter any string ";W$ CALL VCOUNT(W$) END SUB VCOUNT(W$) W$ = UCASE$(W$) FOR I= 1 TO LEN(W$) C$ = MID$(W$, I, 1) SELECT CASE C$ CASE "A","E","I","O","U" C = C + 1 END SELECT NEXT I PRINT "Number of vowels=";C END SUB (j) Write a program to input the income of an employee and then calculate tax using following conditions: INCOME TAX RATES <=2,00,000 No Tax Next 1,00,000 7% Next 1,00,000 9% Above that 12% Answer: DECLARE SUB TAX_CAL(INCOME) INPUT "Enter your income";INCOME CALL TAX_CAL(INCOME) END SUB TAX_CAL(INCOME) IF INCOME<=200000 THEN TAX = O ELSEIF INCOME<=300000 THEN TAX = ((INCOME - 200000) * 7 / 100) ELSEIF INCOME<=400000 THEN TAX = (100000 * 7 / 100) + ((INCOME - 300000) * 9 / 100) ELSE TAX = (100000 * 7 / 100) + (100000 * 9 / 100) + ((INCOME - 400000) * 12 / 100) END IF PRINT "Tax amount=";TAX END SUB (k) Write a menu-driven program for the following: MAIN-MENU 1. Find area of rectangle. 2. FInd area of triangle. 3. Find area of 4 walls. 4. Exit Enter Choice::::: 1/2/3/4:: Answer: DECLARE SUB MENU () DECLARE SUB aor () DECLARE SUB aot () DECLARE SUB aofw () DECLARE SUB exit () CLS CALL MENU INPUT "Enter your choice"; ch SELECT CASE ch CASE 1 CALL aor CASE 2 CALL aot CASE 3 CALL aofw END SELECT IF ch = 4 THEN PRINT "BYE BYE , SEE YOU AGAIN" END SUB MENU CLS PRINT "MAIN-MENU" PRINT "1. Area of rectangle " PRINT "2. Area of triangle " PRINT "3. Area of four wall " PRINT "4. Exit" PRINT "Enter your choice : 1/2/3/4/" END SUB SUB aor INPUT "Enter length and breadth : "; l,b a = L * B PRINT "Area of rectangle ="; a END SUB SUB aot INPUT "Enter breadth and height : "; b,h a = (1 / 2) * B * H PRINT "Area of triangle ="; a END SUB SUB aofw INPUT "Enter length breadth and height : "; l,b,h a = 2 * H * (L + B) PRINT "Area of four walls ="; a END SUB