You are on page 1of 2

PROGRAM Lab4Ex1 IMPLICIT NONE ! Student number: 10047525D ! Name: Lam Nga Man !

Declarations CHARACTER(6)::choice CHARACTER(len=80), dimension (:), allocatable :: name REAL, dimension (:), allocatable :: marks integer:: dim1, dim2=2 integer:: i, j,max_loc,min_loc REAL::calAAvg,sum,max_mark,min_mark CHARACTER(len=80)::max_person,min_person !Do loop DO !1st Output WRITE(*,*)"This program can perform the following task" WRITE(*,*)"1. Input students marks of an assessment;" WRITE(*,*)"2. Calculate the average of the inputted marks of an assessment;" WRITE(*,*)"3. Find the students of the highest mark and lowest mark;" WRITE(*,*)"4. Reset the assessment." WRITE(*,*)"To exit this program, enter Q as your selection." WRITE(*,*)"Please indicate your selection:" !Input READ(*,*)choice !Quit the program IF(choice=="Q")EXIT IF (choice=="1")THEN !Input WRITE(*,*)"Please enter the number of students:" read(*,*)dim1 ! now that the size of a is know, allocate memory for it ALLOCATE ( name(dim1) ) ALLOCATE ( marks(dim1) ) !Do loop DO i=1,dim1 WRITE(*,*)"Name of Student",i,":" READ(*,'(T1,A80)')name(i) WRITE(*,*) trim(name(i)), "'s mark:" READ(*,*)marks(i) end do ELSE IF (choice=="2")THEN sum=0 DO i=1,dim1 sum=marks(i)+sum END DO !Output WRITE(*,*)"The average of the assessment is",calAAvg(sum,dim1),"."

ELSE IF(choice=="3")THEN call getMinMax WRITE(*,*)"The student with the highest mark is", trim(max_person), "." , trim(m ax_person), "s mark is", max_mark, "." WRITE(*,*)"The student with the lowest mark is", trim(min_person), "." , trim(mi n_person), "s mark is", min_mark, "." ELSE IF(choice=="4")THEN deallocate (name) deallocate (marks) WRITE(*,*)"The assessment has been reset." ELSE WRITE(*,*)"The number you have entered is invalid ,please choose again." END IF END DO END PROGRAM Lab4ex1 REAL FUNCTION calAAvg(sum,dim1) IMPLICIT NONE !Declaration REAL::sum integer::dim1 calAAvg=sum/dim1 RETURN END FUNCTION calAAvg SUBROUTINE getMinMax IMPLICIT NONE !Declaration CHARACTER(len=80),dimension (:), allocatable :: name REAL, dimension (:), allocatable :: marks REAL::max_mark,min_mark CHARACTER(len=80)::max_person,min_person integer:: max_loc,min_loc,dim1 max_loc=MAXLOC(marks,dim1) min_loc=MINLOC(marks,dim1) max_person=(name(max_loc)) min_person=(name(min_loc)) max_mark=(marks(max_loc)) min_mark=(marks(min_loc)) RETURN END SUBROUTINE getMinMax

You might also like