CS101 Computer Programming and Utilization End Sem 19/11/2003 2.30-5.30 (Answer in the question paper itself. No extra sheet will be given. You may use rough sheets but do not attach any with the quetion-cum- answer paper.) Q1. Read the program below very carefully and fill the statements in the places marked ****1****, ****2*** etc. Use the side of the paper or the back side or the extra blank sheet provided. program abc implicit none integer,parameter:: lower=0,upper=101, size=20,left=1,right=2 integer,dimension(size)::a integer:: j,nvals type:: cell integer:: val type(cell),pointer:: forward,back end type type(cell),target:: first,last !first and last cells type(cell),pointer::moving first%val=lower last%val= upper first%forward=>last nullify(first%back) last%back=>first nullify(last%forward) write (*, '("Give the number of numbers to be sorted")') read *,nvals write (*, '("Give the numbers (between 1 to 100)")') read *, (a(j), j=1,nvals) call read_and_insert call print_seq contains subroutine read_and_insert integer:: i,v,d moving=>first do i=1,nvals v=a(i) call position(v,d) !return the position and the direction of linking call add_elt(d,v) end do end subroutine subroutine print_seq type(cell),pointer::ptr write (*, '("Ascending order")') ptr=>first%forward do write(*,'(i4)',advance='no') ptr%val ptr=>ptr%forward if (.not.associated(ptr%forward)) exit end do write(*,*) write (*, '("Descending order")') ****1**** end subroutine subroutine add_elt(direction,val) integer,intent(in):: direction,val type(cell),pointer:: ptr,temp nullify(ptr) allocate(ptr) ptr%val=val nullify(ptr%forward,ptr%back) if (direction==left) then ****2**** else ****3**** end if end subroutine subroutine position(vl,dir) !take in the value and return the position ! and direction of linking integer,intent(in):: vl integer,intent(out):: dir !print *, "in position", " vl=",vl," moving%val=",moving%val do if (vl > moving%val) then !move forward if (.not. associated(moving%forward)) then dir=left exit else ****4**** end if else !move backward ****5**** end if end do end subroutine end program 5 X 4 = 20 ****1*** ptr=>last%back do write(*,'(i4)',advance='no') ptr%val ptr=>ptr%back if (.not.associated(ptr%back)) exit end do ****2*** temp=>moving%back moving%back=>ptr temp%front=>ptr ptr%back=>temp ptr%front=>moving moving=>moving%back ****3*** temp=>moving%front moving%front=>ptr temp%back=>ptr ptr%front=>temp ptr%back=>moving moving=>moving%front ****4*** moving=>moving%front if (vl < moving%val) then dir=left exit end if ****5*** if (.not. associated(moving%back)) then dir=right exit else moving=>moving%back if (vl > moving%val) then dir=right exit end if end if Q2. Write a generic "reversal subroutine", 'rev(x,y)' where 'x' and 'y' can be either character strings or integers. 'y' is the reverse of characters of 'x' in case 'x' is a character string and is the reverse of the digits of 'x' in case 'x' is an integer. 10 (3 for correct module delaration 3 each for the subroutines rev_i and rev_s and 1 for main program) module generic_rev implicit none interface all_purpose_rev module procedure rev_i module procedure rev_s end interface contains subroutine rev_i(a,b) integer, intent(in)::a integer,intent(out)::b integer:: t t=a do if (t == 0) exit b = 10 * b + mod(t, 10) t = t / 10 end do end subroutine rev_i subroutine rev_s(s1,s2) character(len=*),intent(in)::s1 character(len=*),intent(out)::s2 integer:: i,j,leng leng=len_trim(s1) do i=1,leng j=leng-i+1 s2(i:i)=s1(j:j) end do end subroutine rev_s end module generic_rev program test_generic_rev use generic_rev implicit none integer:: p,q character(len=10)::str1,str2 write(*,'("Give integer p")') read *, p write(*,'("Give string str1")') read *, str1 call all_purpose_rev(p,q) call all_purpose_rev(str1,str2) write(*,10) q write(*,20) str2 10 format("Integer reversal result=",i3) 20 format("String reversal result=",a) end program Q3. Answer as directed: (a) Internal subroutines are preceded by ______________ statement; Subroutines outside the program environment are called ____________ subroutines. (fill in the blank) 2 - contains; external (b) Interface declarations remove the restrictions of _____________, _____________ and ________________ matching of formal and actual parameters. (fill in the blank) 3 - type, number and order (c) The commands for drawing an 'arrow' and a circle in PGPLOT are _____________ and ____________ respectively. (fill in the blank) 2 - PGARRO, PGCIRC (d) What does the program fragment do (Num1, Num2 and Num3 are positive integers)? 2 IF ( Num1 < Num2 ) THEN Num3 = Num1;Num1 = Num2;Num2 = Num3 END IF DO Num3 = MOD( Num1, Num2 ) IF ( Num3 == 0 ) EXIT Num1 = Num2;Num2 = Num3 END DO - Computes GCD. (e) Describe in one line what the output of the following program is? 3 program abc implicit none integer, parameter :: max = 9 integer :: i, j 10 format (9(2x,i1,a,i1,a,i2)) do i = 1, max write(*,10) (i, '*', j, '=', i*j, j = 1, max) end do end program - prints the multiplication table of '1' to '9'. (f) Give in one line the purpose of the intrinsic function 'present'. 2 - checks in the arguement is present in the called procedure. (g) Is the following program correct? What is getting 'dereferenced' here? 2 integer:: a integer, target:: b,c integer, pointer:: p1,p2 p1=>a; b=5;p2=>b; c=p2 - No, 'a' does not have the target attribute and so 'p1=>a' is wrong. 'p2' is getting dereferenced here. (h) Mention the four essential components in the 'activation record' of a function or subroutine. 4 - arguements, local variables, return value, return address ----------paper ends-----------