Incremental ProgrammingIncremental Programming ReviewIncrement Programming Review (con’t)Example of Incremental ProgrammingRough AlgorithmReport DesignVersion #1Output #1Version #2Output #2Version #3Version #3 (con’t)Output #3Version #4Version #4 (con’t)Output #4Version #5Version #5 (con’t)Output #5Final VersionFinal Version (con’t)Final OutputNo “Big Bang,” Please!CMSC 104, Version 9/01 1Incremental ProgrammingTopics•Review of Incremental Programming•Example of Incremental ProgrammingReading•None.CMSC 104, Version 9/01 2Incremental Programming Review•It is best not to take the “big bang” approach to coding.•Write your code in incomplete but working pieces.•For example, for your projects,oDon’t write the whole program at once.oJust write enough to display the user prompt on the screen.oGet that part working first (compile and run).oNext, write the part that gets the value from the user, and then just print it out.CMSC 104, Version 9/01 3Increment Programming Review (con’t)oGet that working (compile and run). oNext, change the code so that you use the value in a calculation and print out the answer.oGet that working (compile and run). oContinue this process until you have the final version.oGet the final version working.•Bottom line: Always have a working version of your program!CMSC 104, Version 9/01 4Example of Incremental ProgrammingProblem:•Write an interactive program that allows the user to calculate the interest accrued on a savings account. The interest is compounded annually.•The user must supply the principal amount, the interest rate, and the number of years over which to compute the interest.CMSC 104, Version 9/01 5Rough AlgorithmPrint explanation of the programGet <principal> from userGet <interest rate> from userGet <number of years> from user<amount> = <principal>While (<number of years> > 0 )amount = amount + (amount X <interest rate>) <number of years> = <number of year> + 1End_while<interest accrued> = <amount> - <principal>Display reportCMSC 104, Version 9/01 6Report DesignInterest rate : 7.0000 % Period : 20 years Principal at start of period : 1000.00 Interest accrued : 2869.68 Total amount at end of period : 3869.68CMSC 104, Version 9/01 7Version #1/* Filename: interest.c* Author: Sue Bogar* Date written: 11/14//99* Description: This program computes the interest accrued in an account* that compounds interest annually. */#include <stdio.h>int main ( ){/* Print Instructions */printf (“This program computes the interest accrued in an account that\n”);printf (“compounds interest annually. You will need to enter the amount\n”);printf (“of the principal, the interest rate and the number of years.\n\n”); return 0;}CMSC 104, Version 9/01 8Output #1This program computes the interest accrued in an account that compounds interest annually. You will need to enter the amount of the principal, the interest rate and the number of years.CMSC 104, Version 9/01 9Version #2/* Filename: interest.c* Author: Sue Bogar* Date written: 11/14//99* Description: This program computes the interest accrued in an account* that compounds interest annually. */#include <stdio.h>int main ( ){float principal, rate ;int years ;/* Print Instructions */printf (“This program computes the interest accrued in an account that\n”) ;printf (“compounds interest annually. You will need to enter the amount\n”) ;printf (“of the principal, the interest rate and the number of years.\n\n”) ;/* Get input from user */printf (“Enter the principal amount : “) ;scanf (“%f”, &principal) ;printf (“Enter the interest rate as a decimal (for 7%% enter .07) : “) ;scanf (“%f”, &rate) ;printf (“Enter the number of years : “) ;scanf (“%d”, &years) ;printf (“\nprincipal = %f, rate = %f, years = %d\n”, principal, rate, years ) ; return 0 ;}CMSC 104, Version 9/01 10Output #2This program computes the interest accrued in an account thatcompounds interest annually. You will need to enter the amountof the principal, the interest rate and the number of years.Enter the principal amount : 1000.00Enter the interest rate as a decimal (for 7% enter .07) : .07Enter the number of years : 20principal = 1000.000000, rate = 0.070000, years = 20CMSC 104, Version 9/01 11Version #3/* Filename: interest.c* Author: Sue Bogar* Date written: 11/14//99* Description: This program computes the interest accrued in an account* that compounds interest annually. */#include <stdio.h>int main ( ){float principal, rate, amount, interest ;int years, i ;/* Print Instructions */printf (“This program computes the interest accrued in an account that\n”);printf (“compounds interest annually. You will need to enter the amount\n”);printf (“of the principal, the interest rate and the number of years.\n\n”);/* Get input from user */printf (“Enter the principal amount : “);scanf (“%f”, &principal);printf (“Enter the interest rate as a decimal (for 7%% enter .07) : “) ;scanf (“%f”, &rate);printf (“Enter the number of years : “);scanf (“%d”, &years);CMSC 104, Version 9/01 12Version #3 (con’t)/* Save the original principal amount by varying another variable, amount */amount = principal;/* Calculate total amount in the account after the specified number of years */for ( i = 0 ; i < 1 ; i++ ){ amount += amount * rate ;}/* Calculate accrued interest */interest = amount - principal ;printf (“\nprincipal = %f, rate = %f, years = %d\n”, principal, rate, years ) ;printf (“amount = %f, interest = %f\n”); return 0 ;}CMSC 104, Version 9/01 13Output #3This program computes the interest accrued in an account thatcompounds interest annually. You will need to enter the amountof the principal, the interest rate and the number of years.Enter the principal amount : 1000.00Enter the interest rate as a decimal (for 7% enter .07) : .07Enter the number of years : 20principal = 1000.000000, rate = 0.070000, years = 20amount = 1070.000000, interest = 70.000000CMSC 104, Version 9/01 14Version #4/* Filename: interest.c* Author: Sue Bogar* Date written: 11/14//99* Description: This program computes the interest accrued in an account* that compounds interest annually. */#include
View Full Document