Date: Wed, 11 Dec 1996 22:33:33 GMT Server: NCSA/1.5 Content-type: text/html Last-modified: Wed, 02 Oct 1996 20:33:39 GMT Content-length: 4661
a) L has size kFor example: S = {0,1,2,3,4,5,6,7,8,9}. A 3-permutation of S is a 3 digit number where no two digits are the same (345 for example). 344, however, would not be a permutation, since the last two digits are the same.
b) every element in L is distinct.
n * (n-1) * (n-2) * (n-3) * ... * (n-k+1)a) Write a DO/END DO (not a WHILE loop) to calculate this number. Assume n and k are initialized already.
There are several ways to do it; here's one way:b) Write a DO WHILE/END DO loop to do the same. Make the same assumptions as in pt a).ANSWER = 1 DO I = N, N-K+1 ,-1 ANSWER = ANSWER*I END DOA different way is by counting upANSWER = 1 DO I=0,K-1 ANSWER = ANSWER*(N-I) END DONote that this goes to K-1 because (N-K+1) = (N-(K-1)), so I must start at 0 and end at K-1.
I'll just do the parallel DO WHILE loop to the first example in pt a):ANSWER = 1 I = N DO WHILE(I .GE. N-K+1) ANSWER = ANSWER*I I = I-1 END DO
PRINT *,'Space Shuttle ready to launch' PRINT *,'Begin countdown at what number?' READ *,N DO I=N,1 PRINT *,'T minus',I END DO PRINT *,'BLASTOFF!'
Loop wants to count down, needs a -1 on the endb)
Should be DO I=N,1,-1
PRINT *,'When you were born, the President was ' IF (AGE .GE. 4) THEN PRINT *,'Bill Clinton' ELSE IF (AGE .GE. 8) THEN PRINT *,'George Bush' ELSE IF (AGE .GE. 16) THEN PRINT *,'Ronald Reagan' ELSE IF (AGE .GE. 20) THEN PRINT *,'Jimmy Carter' ELSE IF (AGE .GE. 22) THEN PRINT *,'Gerald Ford' ELSE IF (AGE .GE. 28) THEN PRINT *,'Richard Nixon' ELSE PRINT *,'Whew! Older than I am!' END IF
These should all be .LE. Otherwise, for anyone older or equal to 4, (AGE .GE. 4) will be .TRUE., so it'll always print Bill Clinton.c)
LOGICAL DONE DONE = .TRUE. DO WHILE(.NOT. DONE) PRINT *,'Enter a zero' READ *,A IF (A .EQ. 0) DONE = .TRUE. ELSE DONE = .FALSE. PRINT *,'You did not listen' END IF END DO PRINT *,'Good, you entered a zero'
The loop will run only while .NOT. DONE is .TRUE., i.e. only while DONE is .FALSE., so DONE must be set to .FALSE., not set to .TRUE., at the top of the program.
C Adds or subtracts 1 from value and reports C if operation was carried out SUBROUTINE ADDSUB1(OPER,VALUE,DONE) CHARACTER*3 OPER INTEGER VALUE LOGICAL DONE IF (OPER .EQ. 'ADD') THEN VALUE = VALUE + 1 DONE = .TRUE. ELSE IF (OPER .EQ. 'SUB') THEN VALUE = VALUE - 1 DONE = .TRUE. ELSE DONE = .FALSE. END IF RETURN ENDWhich arguments are
OPERb) Output arguments onlyOPER is used by the function, but not assigned to. So the value from the main program is used, hence it is an input argument.
DONEc) Both input and output argumentsThe value of DONE is never used (i.e. found on the right hand side of an expression), so no old value from the main program is used, so this is not an input argument. DONE is, however, assigned to, so that value gets passed out of the function, back to the main program, hence it's an output argument.
VALUEThe old value of VALUE is used on the right hand side of VALUE = VALUE + 1 (i.e. the value of VALUE from the main program). The left hand side now has the new value of VALUE, to be passed back to the main program. So VALUE is both an input argument *and* an output argument.