Example Using Functions

Slides:



Advertisements
Similar presentations
C Functions. What are they? In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive.
Advertisements

Compound Interest.
CONTINUOUSLY COMPOUNDED INTEREST FORMULA amount at the end Principal (amount at start) annual interest rate (as a decimal) time (in years)
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
CMSC 104, Version 9/011 Incremental Programming Topics Review of Incremental Programming Example of Incremental Programming Reading None.
 The initialization, loop-continuation condition and increment portions of a for statement can contain arithmetic expressions.  For example,
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Objectives: Determine the Future Value of a Lump Sum of Money Determine the Present Value of a Lump Sum of Money Determine the Time required to Double.
CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules.
How to design and code functions Chapter 4 (ctd).
Exercise 1 #include int main() { printf(“Hello C Programming!\n”); return 0; } 1.Run your Visual Studio 2008 or Create a new “project” and add.
1 TOPICS TO DISCUSS : FUNCTIONS TYPES OF FUNCTIONS HEADER FILES PRESENTED BY : AVISHEK MAJUMDAR(837837) GUNJAN AGARWAL(856587) SATYAPRIYA DEY(856624)
L19 a 1 Example Using Arrays Using Top-Down Design and Functions to Simplify Code and Create Modules.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.

CMSC 104, Version 8/061L16IncrementalProg.ppt Incremental Programming Topics Review of Incremental Programming Example of Incremental Programming Reading.
Algorithm: procedure in terms of
Computer Science 210 Computer Organization
8.3 Compound Interest HW: (1-21 Odds, Odds)
Chapter 4 C Program Control Part I
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
- Standard C Statements
Lecture 7: Repeating a Known Number of Times
CS1010 Discussion Group 11 Week 4 – Overview of C programming.
Functions in C Mrs. Chitra M. Gaikwad.
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Computer Science 210 Computer Organization
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions Declarations CSCI 230
Arrays, Part 1 of 2 Topics Definition of a Data Structure
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
Value returning Functions
CSCE 206 Lab Structured Programming in C
Incremental Programming
Functions, Part 1 of 3 Topics Using Predefined Functions
Chapter 8 The Loops By: Mr. Baha Hanene.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
A function with one argument
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Examples Example Problems, their Algorithms, and their C Source Code.
Arrays I Handling lists of data.
Functions, Part 1 of 3 Topics Using Predefined Functions
Introduction to C Programming
EPSII 59:006 Spring 2004.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Dale Roberts, Lecturer IUPUI
CSCE 206 Lab Structured Programming in C
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Incremental Programming
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
and Functions to Simplify Code
CPS125.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
and Functions to Simplify Code
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Presentation transcript:

Example Using Functions Using Top-Down Design and Functions to Simplify Code and Create Modules

Problem: Interest Compute interest that is compounded annually. 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. The user must supply the principal amount, interest rate and the number of years .

Algorithm: Interest Print explanation of the program Get principal from user. Get interest rate from user. Get number of years from user. For the number of years specified Calculate the amount in the account at the end of the year. (amount += amount * rate) interest accrued = amount - principal Print report.

Deciding on functions: Interest Write the Function Prototypes void PrintInstructions (void) ; float GetPrincipal (float max, float min) ; float GetInterestRate (float max, float min) ; int GetYears (int max, int min) ; float CalculateAmount (float principal, float rate, int years) ; void PrintReport (float principal, float rate, int years, float amount, float interest) ;

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

Improved Interest with functions Programmed Incrementally, #1 Write and test PrintInstructions( ) /*****************************************************************************************\ * Filename: interest.c * * Author: * * Description: This program computes the interest accrued in an account * * that compounds interest annually. * \*****************************************************************************************/ #include <stdio.h> void PrintInstructions (void) ; int main ( ) { PrintInstructions ( ) ; return 0; } /************************************************** * PrintInstructions is a procedure that prints the program instructions for the user * It takes no arguments and returns no values \*************************************************/ void PrintInstructions (void) printf (“This program computes the interest accrued in an account\n ” ) ; printf (“that compounds interest annually. You will need to enter the\n ” ) ; printf (“amount of the principal, the interest rate and the number of years. \n\n ” ) ;

Interest : 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.

Improved Interest with functions Programmed Incrementally, # 2 Add to main ( ), write & debug GetPrincipal ( ) /*****************************************************************************************\ * Filename: interest.c * * Author: * * Description: This program computes the interest accrued in an account * * that compounds interest annually. * \******************************************************************************************/ #include <stdio.h> #define MAXPRIN 100000.00 #define MINPRIN 0.00 void PrintInstructions (void) ; float GetPrincipal (float max, float min) ; int main ( ) { float principal ; PrintInstructions ( ) ; principal = GetPrincipal (MAXPRIN, MINPRIN) ; printf (“The principal is %.2f \n ” , principal) ; return 0; }

Improved Interest with functions Programmed Incrementally, # 2 Writing of GetPrincipal ( ) /************************************************** * GetPrincipal gets the principal amount from the user and returns it to the calling function. * It assures that the principal is between the minimum amount passed to this function and the * maximum amount passed to this function. \*************************************************/ float GetPrincipal (float max, float min) { float principal ; printf (“Enter the principal amount : “) ; scanf (“%f”, &principal) ; /* assure input is between min and max */ while ( principal < min || principal > max ) printf (“The principal amount must be a value between %.2f and %.2f\n”, min, max) ; printf (“Enter the principal amount : “); scanf (“ %f ”, &principal); } return principal ;

Interest : Output #2 Test GetPrincipal ( ) 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 : 200000.00 The principal amount must be a value between 0.00 and 100000.00 Enter the principal amount : -5.00 Enter the principal amount : 100000.01 Enter the principal amount : - .01 Enter the principal amount : 1000.00 The principal is 1000.00

Improved Interest with functions Programmed Incrementally, # 3 Add to main ( ), write and test GetRate ( ) /*****************************************************************************************\ * Filename: interest.c * * Author: * * Description: This program computes the interest accrued in an account * * that compounds interest annually. * \*****************************************************************************************/ #include <stdio.h> #define MAXPRIN 100000.00 #define MINPRIN 0.00 #define MAXRATE 1.00 #define MINRATE 0.00 void PrintInstructions (void) ; float GetPrincipal (float max, float min) ; float GetRate ( float max, float min); int main ( ) { float principal, rate ; PrintInstructions ( ) ; principal = GetPrincipal (MAXPRIN, MINPRIN) ; rate = GetRate (MAXRATE, MINRATE) ; printf (“The interest rate is %.4f \n ” , rate) ; return 0; }

Improved Interest with functions Programmed Incrementally, # 3 Write and test GetRate ( ) /************************************************** * GetRate gets the interest rate from the user and returns it to the calling function. * It assures that the rate is between the minimum amount passed to this function and the * maximum amount passed to this function. \*************************************************/ float GetRate (float max, float min) { float rate; printf (“Enter the interest rate as a decimal (for 7%% enter .07) : “) ; scanf (“%f”, &rate); /* assure input is between min and max */ while ( rate < min || rate > max ) printf (“The interest rate must be between %.4f and %.4f\n”, min, max ) ; printf (“Enter the interest rate as a decimal (for 7%% enter .07) : “) ; scanf ( “ %f ”, &rate); } return rate ;

Interest : Output #3 Test GetRate ( ) 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) : 5 The interest rate must be between 0.0000 and 1.0000 Enter the interest rate as a decimal (for 7% enter .07) : - .0001 Enter the interest rate as a decimal (for 7% enter .07) : .07 The interest rate is .0700

Improved Interest with functions Programmed Incrementally, # 4 Add to main ( ), write and test GetYears ( ) /***********************************************************\ * Filename: interest.c * * Author: * * Description: This program computes the interest * * accrued in an account that * * compounds interest annually. * \***********************************************************/ #include <stdio.h> #define MAXPRIN 100000.00 #define MINPRIN 0.00 #define MAXRATE 1.00 #define MINRATE 0.00 #define MINYEARS 1 #define MAXYEARS 100 void PrintInstructions (void) ; float GetPrincipal (float max, float min) ; float GetRate ( float max, float min) ; int GetYears (int max, int min) ; int main ( ) { float principal, rate ; int years ; PrintInstructions ( ) ; principal = GetPrincipal (MAXPRIN, MINPRIN) ; rate = GetRate (MAXRATE, MINRATE) ; years = GetYears (MAXYEARS, MINYEARS) ; printf (“years is %d\n”, years) ; return 0; }

Improved Interest with functions Programmed Incrementally, # 4 Write and test GetYears ( ) /************************************************** * GetYears gets the years of the investment from the user and returns it to the calling function. * It assures that the years are between the minimum amount passed to this function and the * maximum amount passed to this function. \*************************************************/ int GetYears (int max, int min) { int years ; printf (“Enter the number of years : “); scanf (“%d”, &years); /* assure input is between min and max */ while ( years < min || years > max ) printf (“The number of years must be between 1 and 100, inclusive \n ” ) ; } return years ;

Interest : Output #4 Test GetYears ( ) 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 : 0 The number of years must be between 1 and 100, inclusive Enter the number of years : 101 Enter the number of years : 20 years is 20

Improved Interest with functions Programmed Incrementally, # 5 Add to main ( ), write & test CalculateAmount ( ) /***********************************************************\ * Filename: interest.c * * Author: * * Description: This program computes the interest * * accrued in an account that * * compounds interest annually. * \***********************************************************/ #include <stdio.h> #define MAXPRIN 100000.00 #define MINPRIN 0.00 #define MAXRATE 1.00 #define MINRATE 0.00 #define MINYEARS 1 #define MAXYEARS 100 void PrintInstructions (void) ; float GetPrincipal (float max, float min) ; float GetRate ( float max, float min) ; int GetYears (int max, int min) ; float CalculateAmount (float principal, float rate, int years ) ; int main ( ) { float principal, rate, amount ; int years ; PrintInstructions ( ) ; principal = GetPrincipal (MAXPRIN, MINPRIN) ; rate = GetRate (MAXRATE, MINRATE) ; years = GetYears (MAXYEARS, MINYEARS) ; amount = CalculateAmount (principal, rate, years) ; printf (“amount is %.2f\n”, amount) ; return 0; }

Improved Interest with functions Programmed Incrementally, # 5 Write and test CalculateAmount( ) /************************************************** * CalculateAmount calculates and returns the amount in the account when the account * is opened with the principal amount passed in, at the annual percentage rate passed in, * compounded annually, for the number of years passed in. \*************************************************/ float CalculateAmount (float principal, float rate, int years) { int i ; /* Calculate total amount in the account after the specified number of years */ for ( i = 0 ; i < years ; i++ ) principal += principal * rate ; } return principal;

Interest : Output #5 Test CalculateAmount ( ) 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 amount is 3869.68

Improved Interest with functions Programmed Incrementally, # 6 Add to main ( ), write & test PrintReport ( ) /***********************************************************\ * Filename: interest.c * * Author: * * Description: This program computes the interest * * accrued in an account that * * compounds interest annually. * \***********************************************************/ #include <stdio.h> #define MAXPRIN 100000.00 #define MINPRIN 0.00 #define MAXRATE 1.00 #define MINRATE 0.00 #define MINYEARS 1 #define MAXYEARS 100 void PrintInstructions (void) ; float GetPrincipal (float max, float min) ; float GetRate ( float max, float min) ; int GetYears (int max, int min) ; float CalculateAmount (float principal, float rate, int years ) ; void PrintReport (float principal, float rate, int years, float amount, float interest) ; int main ( ) { float principal, rate, amount, interest ; int years ; PrintInstructions ( ) ; /Get valid input from user */ principal = GetPrincipal (MAXPRIN, MINPRIN) ; rate = GetRate (MAXRATE, MINRATE) ; years = GetYears (MAXYEARS, MINYEARS) ; /* Do Calculations */ amount = CalculateAmount (principal, rate, years) ; interest = amount - principal ; PrintReport (principal, rate, years, amount, interest ) ; return 0; }

Improved Interest with functions Programmed Incrementally, # 6 Write and test PrintReport( ) /************************************************** * PrintReport takes original principal amount, the interest rate, amount of years of the * investment, amount in the account at the end of the term and the accrued interest as * arguments. It prints these values in a report format. \*************************************************/ void PrintReport (float principal, float rate, int years, float amount, float interest) { printf (“\n\n”) ; printf (“Interest rate : %.4f %% \n ” , 100 * rate ) ; printf (“ Period : %d years \n \n ” , years ) ; printf (“ Principal at start of period : %9.2f \n ” , principal ) ; printf (“ Interest accrued : %9.2f \n ” , interest ) ; printf (“Total amount at end of period : %9.2f /n ” , amount) ; }

Interest : Output #6 Final output: Test PrintReport ( ) 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