| |
Attendance at physical inventory counting
This is a small program inside a big program.
This is useful where the small program has to be repeated so many times in the same big program.
In the main program whenever the small is required to use the GO SUB command. When the GO SUB is executed, the computer will go back to the main program and continue with the execution of the big program. The GO SUB is the opposite of GO TO where the computer does not return.
Note that the result of GO SUB is brought back and used in the book of the main program.
The Sub-routine program is usually stated with REM statement and is after the END of the main program.
The GO SUB would be executed in the main program more than one time,however,in each care a new value which has being calculated will be used.
Question
A team of ‘R’ players can be selected from N players.
Write a program to calculate the number of ways of selecting a team from the available players.
HINT: N!
R!*(N-R)!
Solution
10 READ N
20 FACT = N
30 GO SUB 170
40 X= FACT
50 READ R
60 FACT = R
70 GOSUB 170
80 Y= FACT
90 W= (N-R)
100 FACT = W
110 GO SUB 170
120 Z = FACT
130 A = X/(Y*Z)
140 DATE 6,2
150 PRINT X,7,2,A
160 END
170 REM SUBROUTINE TO CALCULATE FACTORIAL OF A NO.
180 M = FACT
190 FOR I = M-1 to 1 STEP - 1
260 FACT = FACT * I
210 NEXT I
220 RETURN
EXP
FACT = Factorial
After the GO-SUB ,the results obtained are represented s,x,y and z before the final line.130
Note the assignment of facts
e.g FACT= 01
GO SUB
X= FACT.
i.e the result of N! u,x, which has replaced FACT
6*5*4*3*2*1 = X= 720
2*1 =Y= 2
4*3*2*1 = 2= 2Y
= 720 =15 = A
24 *2
The GO SUB line no. can then be filled after the SUB ROUTINE program has being written.
By Y. O. Olajide(fca)
MP Olajide and associates Nig.
www.olajideassociates.com |
|