Skip to main content
Engineering LibreTexts

8.6: Example Two

  • Page ID
    54277
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    A typical problem is to assign grades based on a typical grading standard.

    Understand the Problem

    For this example, the program will assign grades using the following grade scale:

    A B C D F
    >=90 80 - 89 70 - 79 60 - 69 <=59

    The program will read three test scores, compute the average, and display the appropriate grade based on the average.

    Create the Algorithm

    The algorithm is the name for the ordered sequence of steps involved in solving the problem.

    For this problem, the variables will be declared and an initial header displayed. Then, the test1, test2, and test3 values will need to be read from the user.

    ! declare variables
    !     reals -> test1, test2, test3
    !     integer -> testave
    ! display initial header
    ! read the test1, test2, and test3 values
    

    Next, the average can be calculated. The average will be converted to the nearest integer and, based on that, the appropriate grade can be determined and displayed. Formalizing this, the following steps can be developed.

    ! calculate the testave and convert to integer
    ! determine grade
    !         A - >= 90
    !         B - 80 to 89
    !         C - 70 to 79
    !         D - 60 to 69
    !         F - <= 59
    

    For convenience, the steps are written as program comments.

    Implement the Program

    Based on the algorithm, the following program can be created.

    program grades
    
    ! declare variables
    implicit none
    real :: test1, test2, test3
    integer :: testave
    
    ! display initial header
        write (*,*) "Grade Assignment Program"
        write (*,*)
        write (*,*) "Enter test 1, test 2 and test 3 values"
    
    ! read the test1, test2, and test3 values
        read (*,*) test1, test2, test3
    
    ! calculate the average and convert to integer
        testave = nint ((test1 + test2 + test3)/3.0)
    
    ! determine grade
    !   A→>=90,B→80-89,C→70-79,D→60-69,F→<=59
    
        select case (testave)
            case(90:)
                write (*,*) "Grade is: A"
            case(80:89)
                write (*,*) "Grade is: B"
            case(70:79)
                write (*,*) "Grade is: C"
            case(60:69)
                write (*,*) "Grade is: D"
            case(:59)
                write (*,*) "Grade is: F"
        end select
    
    end program grades
    

    The indentation is not required, but does help make the program easier to read.

    Test/Debug the Program

    Once the program is written, testing should be performed to ensure that the program works. The testing will be based on the specific parameters of the program.

    In this example, each of the three possible values for the discriminant should be tested.

    C:\mydir> grade
     Grade Assignment Program
      Enter test 1, test 2 and test 3 values
    70 80 90
    
     Grade is: B
    
    C:\mydir>
    

    The program should be tested with a series of data items to ensure appropriate grade assignment for each grade. Test values for each grade should be entered for the testing.


    This page titled 8.6: Example Two is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?