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 CS 302 Section 70 - Quiz 2

Quiz #2

  1. Suppose we have a set S of n elements. Also, suppose we wish to put some of these elements into a list L. Our list L is a k-permutation of the set S if
    a) L has size k
    b) every element in L is distinct.
    For 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.
    The mathematical formula for the number of k-permutations when S is size n is
    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:
    ANSWER = 1
    DO I = N, N-K+1 ,-1
       ANSWER = ANSWER*I
    END DO
    
    A different way is by counting up
    ANSWER = 1
    DO I=0,K-1
       ANSWER = ANSWER*(N-I)
    END DO
    
    Note that this goes to K-1 because (N-K+1) = (N-(K-1)), so I must start at 0 and end at K-1.

    b) Write a DO WHILE/END DO loop to do the same. Make the same assumptions as in pt a).

    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
    

  2. What's wrong with these three code fragments? (Both problems are logicalerrors)
    a)
            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 end
    Should be DO I=N,1,-1
    b)
    	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.
  3. Consider the following Subroutine:
    
    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
    	END
    
    
    Which arguments are
    a) Input arguments only
    OPER

    OPER is used by the function, but not assigned to. So the value from the main program is used, hence it is an input argument.

    b) Output arguments only
    DONE

    The 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.

    c) Both input and output arguments
    VALUE

    The 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.


Copyright © 1996 Jeff Lampert (tick@cs.wisc.edu). Last modified September 23, 1996.