Computer Science Canada

Suggestions?

Author:  Thuged_Out_G [ Fri Nov 21, 2003 3:12 am ]
Post subject:  Suggestions?

i have to make this program for compsci class, ive finished it...i was just wondering if someone could read the program, and give me ideas to help me improve things, or make the code more effecient.

code:

fcn annuity (balance, interest, terms : real) : real
    result balance * (1 + interest) ** terms
end annuity

fcn futureValue (balance1, interest1, terms1 : real) : real
    result balance1 * ((1 + interest1) ** terms1 - 1) div interest1
end futureValue

var win := Window.Open ("position:top,center,notoolbar,graphics:300;400,")
var balance, balance1, interest, interest1, terms, terms1, num : real
var answer : string

loop
    put "  -    You can calculate   -"
    put "  -    Annuities(1)   -"
    put "  -    Future Value(2) -"
    put "  -    Both(1.2)    -"
    put "  -    or  -"
    put "  -    Exit(0) -"
    put "  -    Selection:   -" ..
    get num
    cls

    loop
        if num = 0 then
            loop
                Window.Close (win)
            end loop
        end if

        if num = 1 then
            put "Calculating Annuities"
            put skip
            put "Please enter in the balance: "..
            get balance
            cls
            put "Please enter in the interest rate(as a decimal): " ..
            get interest
            cls
            put "Please enter in the number of terms: " ..
            get terms
            cls
            put "The value is: ", annuity (balance, interest, terms)
        end if

        if num = 2 then
            put "Please enter in the balance: "
            get balance1
            cls
            put "Please enter in the interest rate(as a decimal): " ..
            get interest1
            cls
            put "Please enter in the number of terms: " ..
            get terms1
            cls
            put "The value is: ", futureValue (balance1, interest1, terms1)
        end if

        if num = 1.2 then
            put "Calculating Annuities"
            put skip
            put "Please enter in the balance: "
            get balance
            cls
            put "Please enter in the interest rate(as a decimal): " ..
            get interest
            cls
            put "Please enter in the number of terms: " ..
            get terms
            cls
            put "The value is: ", annuity (balance, interest, terms)
        end if

        put skip
        put "Do future value now?(y/n): " ..
        get answer
        cls

        if answer = "y" then
            put "Calculating Future Value"
            put skip
            put "Please enter in the balance: "
            get balance1
            cls
            put "Please enter in the interest rate(as a decimal): " ..
            get interest1
            cls
            put "Please enter in the number of terms: " ..
            get terms1
            cls
            put "The value is: ", futureValue (balance1, interest1, terms1)
        else
            exit
        end if
    end loop
end loop


btw, the formulas may not be correct, but i can fix those later...i jsut tried to play it from memory of personal finance lol...i hate that course Laughing

Author:  AsianSensation [ Fri Nov 21, 2003 5:18 pm ]
Post subject: 

from a cursory glance, I can't see why you have balance and balance1 and etc. They are just variables, you can reuse them over and over, don't have to use different variables unless they are going to be doing different things

also, make the getting the variable part a procedure, that would shorten your code by alot.

like this:
code:
proc GetVar
    put "Please enter in the balance: "
    get balance
    cls
    put "Please enter in the interest rate(as a decimal): " ..
    get interest
    cls
    put "Please enter in the number of terms: " ..
    get terms
    cls
end GetVar


so instead of typing that over and over again, you can just use the procedure GetVar


: