Examples Example Problems their Algorithms and their C
Examples Example Problems, their Algorithms, and their C Source Code. CMSC 104 1
Problem: Interest Compute interest that is compounded annually. l Write an interactive program that allows the user to calculate the interest accrued in a savings account that is compounded annually over a period of years. l The user must supply the principal amount, interest rate and the number of CMSC 104 years. l 2
Algorithm: Interest Print explanation of the program l Get principal from user. l Get interest rate from user. l Get number of years from user. l For the number of years specified l o Calculate the amount in the account at the end of the year. (amount += amount * rate) interest accrued = amount - principal l Print report. CMSC 104 l 3
Interest : Design the Report 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 4
Interest, programmed incrementally #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 ( ) { /* Print Instructions */ printf (“This program computes the interest accrued in an account”); printf (“that compounds interest annually. You will need to enter the”); printf (“amount of the principal, the interest rate and the number of years. nn”); } CMSC 104 5
First Output : Interest 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 6
Interest, programmed incrementally #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 ; int years ; */ /* Print Instructions */ printf (“This program computes the interest accrued in an account”) ; printf (“that compounds interest annually. You will need to enter the”) ; printf (“amount of the principal, the interest rate and the number of years. nn”) ; CMSC 104 /* 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 = %dn”, principal, rate, years ) ; } 7
Second Output : Interest 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 8
Interest, programmed incrementally #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”); printf (“that compounds interest annually. You will need to enter the”); printf (“amount of the principal, the interest rate and the number of years. nn”); /* 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 9
Interest, programmed incrementally #3 /* 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 = %dn”, principal, rate, years ) ; printf (“amount = %f, interest = %fn”); } CMSC 104 10
Third Output : Interest 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 11
Interest, programmed incrementally #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> main ( ) { float principal, rate, amount, interest ; int years, I ; */ /* Print Instructions */ printf (“This program computes the interest accrued in an account”); printf (“that compounds interest annually. You will need to enter the”); printf (“amount of the principal, the interest rate and the number of years. nn”); /* 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 12
Interest, programmed incrementally #4 /* 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 = %dn”, principal, rate, years ) ; printf (“amount = %f, interest = %fn”); } CMSC 104 13
Fourth Output : Interest 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 14
Interest, programmed incrementally #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> main ( ) { float principal, rate, amount, interest ; int years, I ; */ /* Print Instructions */ printf (“This program computes the interest accrued in an account”); printf (“that compounds interest annually. You will need to enter the”); printf (“amount of the principal, the interest rate and the number of years. nn”); /* 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 15
Interest, programmed incrementally #5 /* 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 = %dn”, principal, rate, years ) ; printf (“amount = %f, interest = %fn”); } CMSC 104 16
Fifth Output : Interest 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 17
Source code in C: Interest /* 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”); printf (“that compounds interest annually. You will need to enter the”); printf (“amount of the principal, the interest rate and the number of years. nn”); /* 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 18
Source code in C: Interest /* 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 : %. 4 f %%n”, 100 * rate ) ; printf (“ Period : %d yearsnn”, years ) ; printf (“ Principal at start of period : %9. 2 f”, principal ); printf (“ Interest accrued : %9. 2 f”, interest ); printf (“Total amount at end of period : %9. 2 f”, amount); } CMSC 104 19
Final Output : Interest 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 20
Improvements to Interest We now have a working program that we are convinced that is correct and solves the problem. l Our program is not robust, since none of the input provided by the user is checked for validity. l We need to decide what is valid. l CMSC 104 21
Improvements to Interest l What range of values will we allow for the principal ? Positive values less than or equal to 100, 000. l How about the interest rate ? Positive values less than 1. l And the numbers of years ? Years between 1 and 100. CMSC 104 22
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”); printf (“that compounds interest annually. You will need to enter the”); printf (“amount of the principal, the interest rate and the number of years. nn”); /* 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 100000n”) ; printf (“Enter the principal amount : “); scanf (“%f”, &principal); CMSC 104 } 23
Improved Interest #1 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 : %. 4 f %%n”, 100 * rate ) ; printf (“ Period : %d yearsnn”, years ) ; printf (“ Principal at start of period : %9. 2 f”, principal ); printf (“ Interest accrued : %9. 2 f”, interest ); printf (“Total amount at end of period : %9. 2 f”, amount); } CMSC 104 24
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 CMSC 104 Principal at start of period : 1000. 00 Interest accrued : 2869. 68 Total amount at end of period : 3869. 68 25
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”); printf (“that compounds interest annually. You will need to enter the”); printf (“amount of the principal, the interest rate and the number of years. nn”); CMSC 104 /* 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 100000n”) ; printf (“Enter the principal amount : “); scanf (“%f”, &principal); } 26
Improved Interest #2 /* 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 1n”) ; 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 27
Improved Interest #2 /* Calculate accrued interest */ interest = amount - principal ; /* Print report */ printf (“Interest rate : %. 4 f %%n”, 100 * rate ) ; printf (“ Period : %d yearsnn”, years ) ; printf (“ Principal at start of period : %9. 2 f”, principal ); printf (“ Interest accrued : %9. 2 f”, interest ); printf (“Total amount at end of period : %9. 2 f”, amount); } CMSC 104 28
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 CMSC 104 Principal at start of period : 1000. 00 Interest accrued : 2869. 68 Total amount at end of period : 3869. 68 29
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”); printf (“that compounds interest annually. You will need to enter the”); printf (“amount of the principal, the interest rate and the number of years. nn”); CMSC 104 /* 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 100000n”) ; printf (“Enter the principal amount : “); scanf (“%f”, &principal); } 30
Improved Interest #3 /* 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 1n”) ; 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, inclusiven”) ; printf (“Enter the number of years : “); scanf (“%d”, &years); } CMSC 104 31
Improved Interest #3 /* 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 : %. 4 f %%n”, 100 * rate ) ; printf (“ Period : %d yearsnn”, years ) ; printf (“ Principal at start of period : %9. 2 f”, principal ); printf (“ Interest accrued : %9. 2 f”, interest ); printf (“Total amount at end of period : %9. 2 f”, amount); } CMSC 104 32
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 CMSC 104 Principal at start of period : 1000. 00 Interest accrued : 2869. 68 Total amount at end of period : 3869. 68 33
Problem : Combinatorics l Combinatorics is the art of enumerating combinations and permutations. l Find and count all positive integers with three digits or less whose digits add up to a number specified by the user. l What constraints should we put on the sum of the digits? CMSC 104 34
Combinatorics Thinking through the problem The smallest number is 0, so the input should be 0 or greater. l The largest number is 999, so the input should be 27 (9 + 9) or less. l We could have a single for loop that goes through the integers from 0 to 999. l For each integer, we’ll need to break the integer up into its individual digits, add them up and compare that sum to num. l CMSC 104 35
Combinatorics Thinking through the problem l l 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 36
Combinatorics Thinking through the problem Instead of working with the integers 0 through 999, we could work with each of the digits instead. l 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 l This will eliminate the need to split the integer into its digits. l Each for loop will control one of the CMSC 104 37 digits. l
Combinatorics Thinking through the problem 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%dn”, I, j, k ) ; counter++ ; } } How long does this take ? 1000 ( 10 * 10 ) CMSC 104 38
Algorithm : Combinatorics Print explanation l Get num from user (0 <= num <= 27) l Validate value of num l Vary 1 digit at a time from the digit sequence 000 to the digit sequence 999 l o 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. CMSC 104 l Print the count 39
Combinatorics Incremental Programming Write the explanation, get it running l Get num from user and validate it, get this running. l 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. l Modify the code to eliminate leading 0’s l CMSC 104 40
- Slides: 40