Transcript Lect19.ppt

Incremental Programming
Topics
• Review of Incremental Programming
• Example of Incremental Programming
Reading
• None.
CMSC 104, Lecture 19
1
Incremental 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,
o
o
o
o
Don’t write the whole program at once.
Just write enough to display the user prompt on
the screen.
Get that part working first (compile and run).
Next, write the part that gets the value from the
user, and then just print it out.
CMSC 104, Lecture 19
2
Increment Programming Review (con’t)
o
o
o
o
o
Get that working (compile and run).
Next, change the code so that you use the
value in a calculation and print out the answer.
Get that working (compile and run).
Continue this process until you have the final
version.
Get the final version working.
• Bottom line: Always have a working
version of your program!
CMSC 104, Lecture 19
3
Example of Incremental Programming
Problem:
• 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, Lecture 19
4
Rough Algorithm
Print explanation of the program
Get <principal> from user
Get <interest rate> from user
Get <number of years> from user
<amount> = <principal>
While (<number of years> > 0 )
amount = amount + (amount X <interest rate>)
<number of years> = <number of year> + 1
End_while
<interest accrued> = <amount> - <principal>
Display report
CMSC 104, Lecture 19
5
Report Design
Interest rate : 7.0000 %
Period : 20 years
Principal at start of period : 1000.00
Interest accrued : 2869.68
Total amount at end of period : 3869.68
CMSC 104, Lecture 19
6
Version #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, Lecture 19
7
Output #1
This 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, Lecture 19
8
Version #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, Lecture 19
9
Output #2
This 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.
Enter the principal amount : 1000.00
Enter the interest rate as a decimal (for 7% enter .07) : .07
Enter the number of years : 20
principal = 1000.000000, rate = 0.070000, years = 20
CMSC 104, Lecture 19
10
Version #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, Lecture 19
11
Version #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, Lecture 19
12
Output #3
This 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.
Enter the principal amount : 1000.00
Enter the interest rate as a decimal (for 7% enter .07) : .07
Enter the number of years : 20
principal = 1000.000000, rate = 0.070000, years = 20
amount = 1070.000000, interest = 70.000000
CMSC 104, Lecture 19
13
Version #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 <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, Lecture 19
14
Version #4 (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 < 2 ; 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, Lecture 19
15
Output #4
This 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.
Enter the principal amount : 1000.00
Enter the interest rate as a decimal (for 7% enter .07) : .07
Enter the number of years : 20
principal = 1000.000000, rate = 0.070000, years = 20
amount = 1144.900000, interest = 144.900000
CMSC 104, Lecture 19
16
Version #5
/* 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, Lecture 19
17
Version #5 (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 < years ; 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, Lecture 19
18
Output #5
This 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.
Enter the principal amount : 1000.00
Enter the interest rate as a decimal (for 7% enter .07) : .07
Enter the number of years : 20
principal = 1000.000000, rate = 0.070000, years = 20
amount = 3869.680000, interest = 2869.680000
CMSC 104, Lecture 19
19
Final Version
/* 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, Lecture 19
20
Final Version (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 < years ; i++ )
{
amount += amount * rate ;
}
/* Calculate accrued interest */
interest = amount - principal ;
/* Print report */
printf (“Interest rate : %.4f %%\n”, 100 * rate ) ;
printf (“
Period : %d years\n\n”, years ) ;
printf (“
Principal at start of period : %9.2f”, principal );
printf (“
Interest accrued : %9.2f”, interest );
printf (“Total amount at end of period : %9.2f”, amount);
return 0 ;
}
CMSC 104, Lecture 19
21
Final Output
This 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.
Enter the principal amount : 1000.00
Enter the interest rate as a decimal (for 7% enter .07) : .07
Enter the number of years : 20
Interest rate : 7.0000 %
Period : 20 years
Principal at start of period : 1000.00
Interest accrued : 2869.68
Total amount at end of period : 3869.68
CMSC 104, Lecture 19
22
No “Big Bang,” Please!
• We now have a working program that
we are convinced is logically correct
and that solves the problem.
• Would we have this much confidence in
our program if we had used the “big
bang” approach? I doubt it!
CMSC 104, Lecture 19
23
Improvements to Interest
• We now have a working program that
we are convinced that is correct and
solves the problem.
• Our program is not robust, since none
of the input provided by the user is
checked for validity.
• We need to decide what is valid.
CMSC 104, Lecture 19
24
Improvements to Interest
• What range of values will we allow for
the principal ?
Positive values less than or equal to
100,000.
• How about the interest rate ?
Positive values less than 1.
• And the numbers of years ?
Years between 1 and 100.
CMSC 104, Lecture 19
25
Improved Interest #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>
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 principal from user */
printf (“Enter the principal amount : “);
scanf (“%f”, &principal);
/* Validate principal amount */
while ( principal < 0.0 || principal > 100000.0 )
{
printf (“The principal amount must be a value between 0 and 100000\n”) ;
printf (“Enter the principal amount : “);
scanf (“%f”, &principal);
}
CMSC 104, Lecture 19
26
Improved Interest #1 (con’t)
printf (“Enter the interest rate as a decimal (for 7%% enter .07) : “) ;
scanf (“%f”, &rate);
printf (“Enter the number of years : “);
scanf (“%d”, &years);
/* 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 < years ; i++ )
{
amount += amount * rate ;
}
/* Calculate accrued interest */
interest = amount - principal ;
/* Print report */
printf (“Interest rate : %.4f %%\n”, 100 * rate ) ;
printf (“
Period : %d years\n\n”, years ) ;
printf (“
Principal at start of period : %9.2f”, principal );
printf (“
Interest accrued : %9.2f”, interest );
printf (“Total amount at end of period : %9.2f”, amount);
}
CMSC 104, Lecture 19
27
Interest : Testing Principal Validity Check
This 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.
Enter the principal amount : 100000.01
The principal amount must be between 0 and 100000.
Enter the principal amount : -.01
The principal amount must be between 0 and 100000.
Enter the principal amount : 1000.00
Enter the interest rate as a decimal (for 7% enter .07) : .07
Enter the number of years : 20
Interest rate : 7.0000 %
Period : 20 years
Principal at start of period : 1000.00
Interest accrued : 2869.68
Total amount at end of period : 3869.68
CMSC 104, Lecture 19
28
Improved Interest #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>
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 valid principal amount from user */
printf (“Enter the principal amount : “);
scanf (“%f”, &principal);
while ( principal < 0.0 || principal > 100000.0 )
{
printf (“The principal amount must be a value between 0 and 100000\n”) ;
printf (“Enter the principal amount : “);
scanf (“%f”, &principal);
}
CMSC 104, Lecture 19
29
Improved Interest #2 (con’t)
/* Get valid interest rate from user */
printf (“Enter the interest rate as a decimal (for 7%% enter .07) : “) ;
scanf (“%f”, &rate);
while ( rate < 0.0 || rate > 1.0 )
{
printf (“The interest rate must be between 0 and 1\n”) ;
printf (“Enter the interest rate as a decimal (for 7%% enter .07) : “) ;
scanf (“%f”, &rate);
}
printf (“Enter the number of years : “);
scanf (“%d”, &years);
/* 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 < years ; i++ )
{
amount += amount * rate ;
}
CMSC 104, Lecture 19
30
Improved Interest #2 (con’t)
/* Calculate accrued interest */
interest = amount - principal ;
/* Print report */
printf (“Interest rate : %.4f %%\n”, 100 * rate ) ;
printf (“
Period : %d years\n\n”, years ) ;
printf (“
Principal at start of period : %9.2f”, principal );
printf (“
Interest accrued : %9.2f”, interest );
printf (“Total amount at end of period : %9.2f”, amount);
}
CMSC 104, Lecture 19
31
Interest : Testing Interest Rate Validity Check
This 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.
Enter the principal amount : 1000.00
Enter the interest rate as a decimal (for 7% enter .07) : 1.0001
The interest rate must be between 0 and 1.
Enter the interest rate as a decimal (for 7% enter .07) : -.0001
The interest rate must be between 0 and 1.
Enter the interest rate as a decimal (for 7% enter .07) : .07
Enter the number of years : 20
Interest rate : 7.0000 %
Period : 20 years
Principal at start of period : 1000.00
Interest accrued : 2869.68
Total amount at end of period : 3869.68
CMSC 104, Lecture 19
32
Improved Interest #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>
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 valid principal amount from user */
printf (“Enter the principal amount : “);
scanf (“%f”, &principal);
while ( principal < 0.0 || principal > 100000.0 )
{
printf (“The principal amount must be a value between 0 and 100000\n”) ;
printf (“Enter the principal amount : “);
scanf (“%f”, &principal);
}
CMSC 104, Lecture 19
33
Improved Interest #3 (con’t)
/* Get valid interest rate from user */
printf (“Enter the interest rate as a decimal (for 7%% enter .07) : “) ;
scanf (“%f”, &rate);
while ( rate < 0.0 || rate > 1.0 )
{
printf (“The interest rate must be between 0 and 1\n”) ;
printf (“Enter the interest rate as a decimal (for 7%% enter .07) : “) ;
scanf (“%f”, &rate);
}
/* Get valid number of years from user */
printf (“Enter the number of years : “);
scanf (“%d”, &years);
while ( years < 1 || years > 100 )
{
printf (“The number of years must be between 1 and 100, inclusive\n”) ;
printf (“Enter the number of years : “);
scanf (“%d”, &years);
}
CMSC 104, Lecture 19
34
Improved Interest #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 < years ; i++ )
{
amount += amount * rate ;
}
/* Calculate accrued interest */
interest = amount - principal ;
/* Print report */
printf (“Interest rate : %.4f %%\n”, 100 * rate ) ;
printf (“
Period : %d years\n\n”, years ) ;
printf (“
Principal at start of period : %9.2f”, principal );
printf (“
Interest accrued : %9.2f”, interest );
printf (“Total amount at end of period : %9.2f”, amount);
}
CMSC 104, Lecture 19
35
Interest : Testing Years Validity Check
This 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.
Enter the principal amount : 1000.00
Enter the interest rate as a decimal (for 7% enter .07) : .07
Enter the number of years : 101
The number of years must be between 1 and 100, inclusive.
Enter the number of years : 0
The number of years must be between 1 and 100, inclusive.
Enter the number of years : 20
Interest rate : 7.0000 %
Period : 20 years
Principal at start of period : 1000.00
Interest accrued : 2869.68
Total amount at end of period : 3869.68
CMSC 104, Lecture 19
36
Problem : Combinatorics
• Combinatorics is the art of enumerating
combinations and permutations.
• Find and count all positive integers with
three digits or less whose digits add up to a
number specified by the user.
• What constraints should we put on the sum
of the digits?
CMSC 104, Lecture 19
37
Combinatorics: Thinking through the problem
• The smallest number is 0, so the input
should be 0 or greater.
• The largest number is 999, so the input
should be 27 (9 + 9 + 9) or less.
• We could have a single for loop that
goes through the integers from 0 to 999.
• For each integer, we’ll need to break the
integer up into its individual digits, add
them up and compare that sum to num.
CMSC 104, Lecture 19
38
Combinatorics: Thinking through the problem (con’t)
• We’ve already learned the trick used to separate an
integer into its digits. It involved using integer division
and modulus within a loop. Each iteration of the loop
stripped one digit off of the integer.
• So within the for loop, we’ll need to have another loop
that separates the integer into its digits, sums them,
compares the sum to num and prints and counts the
integer if they are equal.
• This approach will solve the problem, but it takes a lot
of work at each step and will take a long time.
How many steps ?
1000 * 3
• There must be a quicker and easier way.
CMSC 104, Lecture 19
39
Combinatorics: Thinking through the problem (con’t)
• Instead of working with the integers 0
through 999, we could work with each of
the digits instead.
• We could vary just one of the digits at a
time using nested for loops, like this : 0
0 0, 0 0 1, … 0 0 9, 0 1 0, … 9 9 9
• This will eliminate the need to split the
integer into its digits.
• Each for loop will control one of the digits.
CMSC 104, Lecture 19
40
Combinatorics: Thinking through the problem (con’t)
for ( i = 0 ; i < 10 ; i++)
{
for ( j = 0 ; j < 10 ; j++)
{
for ( k = 0 ; k < 10 ; k++)
{
if ( i + j + k == num )
{
printf ( “%d%d%d\n”, i, j, k ) ;
counter++ ;
}
}
}
}
How long does this take ? 1000 ( 10 * 10 * 10 )
CMSC 104, Lecture 19
41
Algorithm : Combinatorics
• Print explanation
• Get num from user
• Validate value of num (0 <= num <= 27)
• Vary 1 digit at a time from the digit sequence
000 to the digit sequence 999
If the sum of the digits is equal to num,
print the 3 digits (or less) with no space
between them. Don’t print leading 0’s.
o Increment the counter.
• Print the count
o
CMSC 104, Lecture 19
42
Combinatorics: Incremental Programming
• Write the explanation, get it running
• Get num from user and validate it, get this
running.
• Write the nested for loops that will print all
three digits when they sum to num. (has
leading 0’s) and print the count. Don’t
forget to initialize count to 0 before
beginning. Get this running.
• Modify the code to eliminate leading 0’s
CMSC 104, Lecture 19
43