subroutines ii. an extended example subroutines are best used to simplify large and unwieldy...

22
Subroutines II

Upload: kimberly-norman

Post on 18-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Subroutines II. An Extended Example Subroutines are best used to simplify large and unwieldy programs. Here is one example of why and how they should

Subroutines II

Page 2: Subroutines II. An Extended Example Subroutines are best used to simplify large and unwieldy programs. Here is one example of why and how they should

An Extended Example

• Subroutines are best used to simplify large and unwieldy programs.

• Here is one example of why and how they should be used.

Page 3: Subroutines II. An Extended Example Subroutines are best used to simplify large and unwieldy programs. Here is one example of why and how they should

The problem

• The Engineering Dept. At a certain university wishes to develop a program to determine the academic standing of their students.

• You have been hired to write the program.

• What do you do first?

Page 4: Subroutines II. An Extended Example Subroutines are best used to simplify large and unwieldy programs. Here is one example of why and how they should

1. Problem specification

• What is ‘academic standing’?

• The answer:– academic standing is based on two things

• the number of credit hours taken

• the gpa

– academic standing needs to be evaluated at the end of each of the first three years of college

Page 5: Subroutines II. An Extended Example Subroutines are best used to simplify large and unwieldy programs. Here is one example of why and how they should

Problem specification (con’t)

• How is academic standing evaluated?

• Answer: To be in good standing, a student must have met the following standards– Year 1: completed 25 hours with gpa > 1.70– Year 2: completed 50 hours with gpa > 1.85– Year 3: completed 75 hours with gpa > 1.95

Page 6: Subroutines II. An Extended Example Subroutines are best used to simplify large and unwieldy programs. Here is one example of why and how they should

2. Input format

• Input the following:– Student’s id number– Class level (1,2,3)– Cumulative hours– Cumulative GPA– Hours and grade for each course completed in

the current year

Page 7: Subroutines II. An Extended Example Subroutines are best used to simplify large and unwieldy programs. Here is one example of why and how they should

Output format

• Output the following:– Student id number– Class level– Current GPA– Updated cumulative hours and cumulative GPA– Indication of academic standing– Number of students processed– Number of students in good standing– Average of all current GPAs

Page 8: Subroutines II. An Extended Example Subroutines are best used to simplify large and unwieldy programs. Here is one example of why and how they should

3. Variable Dictionary

Variable name Type Usage DescriptionStudentNumber Integer Input Student id numberClass Integer Input Class year (1,2,3)CumulativeHours Real Input Total course hours completedCumulativeGPA Real Input Cumulative Grade Point Average (GPA)CurrentGPA Real Input GPA for the last semesterHoursGradeNewHoursNumStudents Integer Output Total number of students processedNum_in_Good_Standing Integer Output Total of students in good standingSum_of_GPAs Real Inter. Total of all student GPAs

Page 9: Subroutines II. An Extended Example Subroutines are best used to simplify large and unwieldy programs. Here is one example of why and how they should

Structure diagram

Main ProgramAcademicStanding

Instruct user(Inform)

Get student info(Process)

Print summary(Wrap up)

repeat

Page 10: Subroutines II. An Extended Example Subroutines are best used to simplify large and unwieldy programs. Here is one example of why and how they should

RefinementMain program

(Academic standing)

Instruct user(Inform)

Get student info(Process)

Print summary(WrapUp)

Read info andcalculate stats(CalculateStats)

Check standing(CheckEligibility)

Display report(Report)

Page 11: Subroutines II. An Extended Example Subroutines are best used to simplify large and unwieldy programs. Here is one example of why and how they should

Further refinementMain program

(Academic standing)

Instruct user(Inform)

Get student info(Process)

Print summary(WrapUp)

Read info andcalculate stats(CalculateStats)

Check standing(CheckEligibility)

Display report(Report)

Check cumulative GPA(Report)

Check cumulative hours(Report)

Page 12: Subroutines II. An Extended Example Subroutines are best used to simplify large and unwieldy programs. Here is one example of why and how they should

Program AcademicStanding

PROGRAM Academic_Standing!-----------------------------------------------------------------------! Program to determine academic standing of engineering students! according to two criteria: cumulative hours and cumulative GPA.! It also counts the total # of students checked and the # found to! be in good standing, and calculates the average current GPA for all! students. Variables used are:! NumStudents : total number of students! Num_in_Good_Standing : number in good standing! Response : user response to more-data inquiry! Sum_of_GPAs : sum of all current GPAs !! Input: Response; also several items of student information in ! subroutine CalculateStats ! Output: Student report by subroutine Report and summary statistics ! by subroutine WrapUp !-----------------------------------------------------------------------

Page 13: Subroutines II. An Extended Example Subroutines are best used to simplify large and unwieldy programs. Here is one example of why and how they should

Main Program IMPLICIT NONE INTEGER :: NumStudents = 0, Num_in_Good_Standing = 0 REAL :: Sum_of_GPAs = 0.0 CHARACTER(1) :: Response

CALL Inform

! Repeat the following until no more data DO CALL Process(NumStudents, Num_in_Good_Standing, Sum_of_GPAs) WRITE (*, '(// 1X, A)', ADVANCE = "NO") "More (Y or N)? " READ *, Response IF (Response /= "Y") EXIT END DO CALL WrapUp(NumStudents, Num_in_Good_Standing, Sum_of_GPAs)

CONTAINS

Page 14: Subroutines II. An Extended Example Subroutines are best used to simplify large and unwieldy programs. Here is one example of why and how they should

Subroutine Inform

!-Inform----------------------------------------------------- SUBROUTINE Inform PRINT *, "*********** Inform called ***********" END SUBROUTINE Inform

Page 15: Subroutines II. An Extended Example Subroutines are best used to simplify large and unwieldy programs. Here is one example of why and how they should

Subroutine Process

!--Process------------------------------------------------------------ ! Accepts student information, determines academic standing, and ! maintains counts of # processed and # in good standing, and a sum ! of current GPAs. Variables used are: ! NumStudents : total number of students ! Num_in_Good_Standing : number in good standing ! Sum_of_GPAs : sum of all current GPAs ! StudentNumber : student's number ! Class : student's class ! CumulativeHours : student's cumulative hours ! CumulativeGPA : student's cumulative GPA ! CurrentGPA : student's current GPA ! InGoodStanding : indicates whether student is in good ! standing ! ! Accepts: NumStudents, Num_in_Good_Standing, and Sum_of_GPAs ! Returns: Updated values of NumStudents, Num_in_Good_Standing, and ! Sum_of_GPAs !---------------------------------------------------------------------

Page 16: Subroutines II. An Extended Example Subroutines are best used to simplify large and unwieldy programs. Here is one example of why and how they should

Subroutine Process

SUBROUTINE Process(NumStudents,Num_in_Good_Standing,Sum_of_GPAs) INTEGER, INTENT(INOUT) :: NumStudents, Num_in_Good_StandingREAL, INTENT(INOUT) :: Sum_of_GPAsREAL :: CumulativeHours, CumulativeGPA, CurrentGPAINTEGER :: StudentNumber, ClassLOGICAL :: InGoodStanding CALL CalculateStats(StudentNumber, Class, NumStudents, & CumulativeHours, CumulativeGPA, CurrentGPA, & Sum_of_GPAs)CALL CheckEligibilityCALL Report(StudentNumber, Class, CumulativeHours, CurrentGPA, & CumulativeGPA)

END SUBROUTINE Process

Page 17: Subroutines II. An Extended Example Subroutines are best used to simplify large and unwieldy programs. Here is one example of why and how they should

Subroutine Wrapup

!-WrapUp------------------------------------------------------ SUBROUTINE WrapUp(NumStudents,Num_in_Good_Standing,Sum_of_GPAs) INTEGER, INTENT(IN) :: NumStudents, Num_in_Good_Standing REAL, INTENT(IN) :: Sum_of_GPAs PRINT *, "*********** WrapUp called ***********" PRINT *, "Number of Students =", NumStudents PRINT *, "Sum of GPAs =", Sum_of_GPAs END SUBROUTINE WrapUp

Page 18: Subroutines II. An Extended Example Subroutines are best used to simplify large and unwieldy programs. Here is one example of why and how they should

Subroutine CalculateStats

!-CalculateStats------------------------------------------------------ ! Subroutine to read a student's number, class, cumulative hours, ! and cumulative GPA; then read Hours and Grade for courses taken ! during the current year, and calculate current GPA, update ! cumulative hours, cumulative GPA, and count (NumStudents) of ! students processed. Hours = 0 and Grade = 0 are used to signal the ! end of data for a student. Other local variables used are: ! NewHours : total hours earned during current year ! NewHonorPoints : honor points earned in current year ! OldHonorPoints : honor points earned in past years ! ! Accepts: NumStudents and Sum_of_GPAs ! Input: StudentNumber, Class, CumulativeHours, CumulativeGPA; ! also Hours and Grade for each of several courses ! Returns: StudentNumber, Class, CumulativeHours, CumulativeGPA, ! CurrentGPA and updated values of NumStudents and ! Sum_of_GPAs !---------------------------------------------------------------------

Page 19: Subroutines II. An Extended Example Subroutines are best used to simplify large and unwieldy programs. Here is one example of why and how they should

Subroutine CalculateStats SUBROUTINE CalculateStats(StudentNumber, Class, NumStudents, & CumulativeHours, CumulativeGPA, & CurrentGPA, Sum_of_GPAs) INTEGER, INTENT(INOUT) :: NumStudents INTEGER, INTENT(OUT) :: StudentNumber, Class REAL, INTENT(INOUT) :: Sum_of_GPAs REAL, INTENT(OUT)::CumulativeHours,CumulativeGPA,CurrentGPA REAL :: Hours,Grade,NewHours,NewHonorPoints,OldHonorPoints WRITE (*, '(1X, A)', ADVANCE = "NO") & "Enter student number, class, cum. hours, cum. gpa: " READ *, StudentNumber,Class,CumulativeHours,CumulativeGPA OldHonorPoints = CumulativeHours * CumulativeGPA NewHours = 0.0 NewHonorPoints = 0.0

Page 20: Subroutines II. An Extended Example Subroutines are best used to simplify large and unwieldy programs. Here is one example of why and how they should

CalculateStats (con’t) DO WRITE (*, '(1X, A)', ADVANCE = "NO") "Hours and grade? " READ *, Hours, Grade IF (Hours <= 0.0) EXIT ! Terminate repetition if end-of-data signaled ! Otherwise continue with the following NewHours = NewHours + Hours NewHonorPoints = NewHonorPoints + Hours * Grade END DO IF (NewHours == 0.0) THEN CurrentGPA = 0.0 ELSE CurrentGPA = NewHonorPoints / NewHours END IF Sum_of_GPAs = Sum_of_GPAs + CurrentGPA CumulativeHours = CumulativeHours + NewHours CumulativeGPA = (OldHonorPoints + NewHonorPoints) / CumulativeHours NumStudents = NumStudents + 1 END SUBROUTINE CalculateStats

Page 21: Subroutines II. An Extended Example Subroutines are best used to simplify large and unwieldy programs. Here is one example of why and how they should

Subroutine CheckEligibility !-CheckEligibility------------------------------------------- SUBROUTINE CheckEligibility PRINT *, "*********** CheckEligibility called ***********" END SUBROUTINE CheckEligibility

Page 22: Subroutines II. An Extended Example Subroutines are best used to simplify large and unwieldy programs. Here is one example of why and how they should

Subroutine Report !-Report-------------------------------------------------------------- SUBROUTINE Report(StudentNumber, Class, CumulativeHours, & CurrentGPA, CumulativeGPA) INTEGER,INTENT(IN) :: StudentNumber, Class REAL,INTENT(IN)::CumulativeHours, CurrentGPA, CumulativeGPA PRINT *, "*********** Report called ***********" !----- Temporary printout ----- PRINT *, "StudentNumber:", StudentNumber PRINT *, "Class: ", Class PRINT *, "Cum. Hours: ", CumulativeHours PRINT *, "Curr. GPA: ", CurrentGPA PRINT *, "Cum. GPA: ", CumulativeGPA END SUBROUTINE Report END PROGRAM Academic_Standing