Name: Siddiqui, Matheen EXECUTION TESTS ========= ===== ============================================================ hw2-histogram < hw2-histogram.input1 WELCOME TO HW2-HISTOGRAM!! This program will display a histogram for a list of integers which you provide. You will be asked to enter a sentinel value. This should be some integer which does not occur in the data set, and is used to indicate the end of the data. Then you will enter scores. Once the sentinel value has been entered, you will be asked to provide the upper and lower bounds for the histogram as well as the size of each subinterval. Then the program will generate the desired histogram. Multiple histograms can be displayed for the same data. Enter sentinel value: Now enter values separated by returns and ended with -1. Low value in histogram (-1 to quit): High value in histogram (-1 to quit): Step size: < 2: 2: 3: 4: ** 5: ** 6: 7: ** 8: * 9: ** 10: * 11: ** 12: * >= 13: Low value in histogram (-1 to quit): High value in histogram (-1 to quit): Step size: < 2: 2: ** 5: **** 8: **** 11: *** >= 14: Low value in histogram (-1 to quit): ============================================================ hw2-histogram < hw2-histogram.input2 WELCOME TO HW2-HISTOGRAM!! This program will display a histogram for a list of integers which you provide. You will be asked to enter a sentinel value. This should be some integer which does not occur in the data set, and is used to indicate the end of the data. Then you will enter scores. Once the sentinel value has been entered, you will be asked to provide the upper and lower bounds for the histogram as well as the size of each subinterval. Then the program will generate the desired histogram. Multiple histograms can be displayed for the same data. Enter sentinel value: Now enter values separated by returns and ended with -1. Low value in histogram (-1 to quit): High value in histogram (-1 to quit): Step size: < 2: 2: * 3: ** 4: ********** 5: ************** 6: *************** 7: ***************** 8: ************** 9: *********** 10: ********* 11: ***** 12: ** >= 13: Low value in histogram (-1 to quit): High value in histogram (-1 to quit): Step size: < 4: *** 4: ************************ 6: ******************************** 8: ************************* 10: ************** >= 12: ** Low value in histogram (-1 to quit): ============================================================ hw2-bank < hw2-bank.input1 Error: GetInteger: unexpected end of file This program calculates and displays the new balances of the user's bank account. The interest rate, initial deposit, monthly payments, and years to display are entered by the user. The user can enter up to 4 sets,or plans, of these for one table. The balances for each plan are printed in table format for every month of the first year, and then at the start of every year for the length of time specified by the user. To stop entering data for a given set of plans, enter a negative number for the interst. To terminate the program enter zero plans, or a negative number for the interest of the first plan. The initial deposit and monthly payments can have any value (negatives are allowed; you might start out in debt, in which case you accumulate negative interest, or lose money each month) and the years to display must be at least 1. Enter the interest rate 1(negative value to quit): Enter initial amount 1: $ Enter monthly payment amount 1: $ Enter the interest rate 2(negative value to quit): Enter initial amount 2: $ Enter monthly payment amount 2: $ Enter the interest rate 3(negative value to quit): Enter initial amount 3: $ Enter monthly payment amount 3: $ Enter the interest rate 4(negative value to quit): Enter number of years: Plan 1: Initial Amount: $1000.00 Interest Rate: 10.0000 Monthly Payment: $100.00 Plan 2: Initial Amount: $1000.00 Interest Rate: 9.0000 Monthly Payment: $100.00 Plan 3: Initial Amount: $1000.00 Interest Rate: 9.0000 Monthly Payment: $150.00 Year Month Balance 1 Balance 2 Balance 3 0 0 1000.00 1000.00 1000.00 0 1 1108.33 1107.50 1157.50 0 2 1217.57 1215.81 1316.18 0 3 1327.72 1324.92 1476.05 0 4 1438.78 1434.86 1637.12 0 5 1550.77 1545.62 1799.40 0 6 1663.69 1657.22 1962.90 0 7 1777.56 1769.64 2127.62 0 8 1892.37 1882.92 2293.58 0 9 2008.14 1997.04 2460.78 0 10 2124.87 2112.02 2629.23 0 11 2242.58 2227.86 2798.95 1 0 2361.27 2344.57 2969.94 2 0 3865.08 3815.26 5124.68 3 0 5526.36 5423.92 7481.55 Enter the interest rate 1(negative value to quit): Enter initial amount 1: $ Enter monthly payment amount 1: $ Enter the interest rate 2(negative value to quit): Enter initial amount 2: $ Enter monthly payment amount 2: $ Enter the interest rate 3(negative value to quit): Enter number of years: Please enter at least 1 for the number of years: ============================================================ hw2-bank < hw2-bank.input2 This program calculates and displays the new balances of the user's bank account. The interest rate, initial deposit, monthly payments, and years to display are entered by the user. The user can enter up to 4 sets,or plans, of these for one table. The balances for each plan are printed in table format for every month of the first year, and then at the start of every year for the length of time specified by the user. To stop entering data for a given set of plans, enter a negative number for the interst. To terminate the program enter zero plans, or a negative number for the interest of the first plan. The initial deposit and monthly payments can have any value (negatives are allowed; you might start out in debt, in which case you accumulate negative interest, or lose money each month) and the years to display must be at least 1. Enter the interest rate 1(negative value to quit): Enter initial amount 1: $ Enter monthly payment amount 1: $ Enter the interest rate 2(negative value to quit): Enter initial amount 2: $ Enter monthly payment amount 2: $ Enter the interest rate 3(negative value to quit): Enter initial amount 3: $ Enter monthly payment amount 3: $ Enter the interest rate 4(negative value to quit): Enter initial amount 4: $ Enter monthly payment amount 4: $ Enter number of years: Plan 1: Initial Amount: $-1000.00 Interest Rate: 18.0000 Monthly Payment: $10.00 Plan 2: Initial Amount: $-1000.00 Interest Rate: 18.0000 Monthly Payment: $20.00 Plan 3: Initial Amount: $-1000.00 Interest Rate: 18.0000 Monthly Payment: $30.00 Plan 4: Initial Amount: $-1000.00 Interest Rate: 18.0000 Monthly Payment: $40.00 Year Month Balance 1 Balance 2 Balance 3 Balance 4 0 0 -1000.00 -1000.00 -1000.00 -1000.00 0 1 -1005.00 -995.00 -985.00 -975.00 0 2 -1010.08 -989.92 -969.77 -949.62 0 3 -1015.23 -984.77 -954.32 -923.87 0 4 -1020.45 -979.55 -938.64 -897.73 0 5 -1025.76 -974.24 -922.72 -871.19 0 6 -1031.15 -968.85 -906.56 -844.26 0 7 -1036.61 -963.39 -890.16 -816.93 0 8 -1042.16 -957.84 -873.51 -789.18 0 9 -1047.80 -952.20 -856.61 -761.02 0 10 -1053.51 -946.49 -839.46 -732.43 0 11 -1059.32 -940.68 -822.05 -703.42 1 0 -1065.21 -934.79 -804.38 -673.97 2 0 -1143.17 -856.83 -570.50 -284.16 3 0 -1236.38 -763.62 -290.86 181.90 4 0 -1347.83 -652.17 43.48 739.13 Enter the interest rate 1(negative value to quit): SOURCE FILES ====== ===== /* Name: Matheen Siddiqui * File: hw-util.h * Last modified on Thurss Sept 25 * -------------- * package to produce dynamic arrays */ #ifndef _hw2_util_h #define _hw2_util_h /* *MINSIZE is the smallest size of the dynamic array *INITVAL is the value the elements of the dynamic array * are initilized to when an array is resized. */ #define MINSIZE 5 #define INITVAL -1 /* * Function: GetDynamicIntArray * Usage: intArrayPtr = GetDynamicIntArray( sentinal, message, sizePtr ); * ---------------------------------------------------------------- * This function returns a pointer to an array of ints, read * from standard input. ints are read until an int equal to sentinal * is scaned in. The function repeatedly prompts with the string, message, * before it reads input values. The int pointer sizePtr points to * the effective size of the array. */ int* GetDynamicIntArray(int sentinal, char *message, int *sizePtr); /* * Function: PutInDynamicArray * Usage: PutInDynamicArray( item, position, &arrayPtr, &size ); * ---------------------------------------------------------------- * This funtion places the int, item, at the location specified by * position (an int), in the array pointed to by arrayPtr, * which is an int*. The actual size of the array is specified by the * int, size whose address you pass. Note you pass &arrayPtr, * the address of the pointer to the array of ints. * If you try to put an element out of the bounds * of arrayPtr, as specified by size, the arrayPtr will be changed to point * to new array large enough to contain all the data, * and size is modifyed accordingly. Also note, that * created locations are initilized to INITVAL */ void PutInDynamicArray( int item, int position, int **arrayPtrPtr, int *sizePtr ); #endif /*end of file*/ /* Name: Matheen Siddiqui * File: hw-util.c * Last modified on Thurs Sept 25 * -------------- * Implements the hw2-util.h interface. */ #include #include "genlib.h" #include "simpio.h" #include "hw2-util.h" /*******helper function prototypes*****/ /* * Function: MakeIntArray * Usage: intArrayPtr = MakeIntArray( numItems ); * ---------------------------------------------------------------- * This function returns a pointer to an allocated int array, whose * size is specified by the int, numItems. */ static int* MakeIntArray( int numItems ); /* * Function: CopyArray * Usage: CopyArray( destPtr, sourcePtr, num_elements ); * ---------------------------------------------------------------- * This function copies the first num_elemnts items from the array at * sourcePtr to the array at destPtr. Bounds are not checked. */ static void CopyArray( int *destPtr, int *sourcePtr, int num_elements ); /*******function definitions**********/ /* * Function: GetDynamicIntArray * ------------------------------------- * This function uses a while loop to continuosly read from standard input, * and uses PutInDynamicArray to put the int to the end of the array. * The resizing of the array is handled in PutInDynamicArray */ int *GetDynamicIntArray(int sentinal, char * message, int *sizePtr) { /*the dynamic array to hold ints*/ int *dynArray = (int*) GetBlock( MINSIZE * sizeof( int )); int item; /*numbers read form standard input*/ int position = 0; /*the position to put element into array*/ int actSize = MINSIZE; /*actual size of array*/ /*break out of loop when sentinal is entered*/ while( TRUE ) { /*get input from user, and if sentinal is entered break*/ printf("%s", message ); item = GetInteger(); if (item == sentinal ) { break; } /*add to array*/ PutInDynamicArray( item, position, &dynArray, &actSize ); /*add to next position*/ position++; } /*assign the effective size*/ *sizePtr = position; return dynArray; } /*end GetDynamicArray*/ /* * Function: MakeIntArray * ------------------------------------- * calls GetBlock to find the pointer to the memory */ static int *MakeIntArray( int numItems ) { return (int *) GetBlock( numItems * sizeof( int )); } /* * Function: CopyArray * ------------------------------------- * loops through and copies first num_elements of sourcePtr * to destPtr. */ static void CopyArray( int *destPtr, int *sourcePtr, int num_elements ) { int i; /*loop through and copy elements*/ for( i = 0; i < num_elements; i++ ) { destPtr[i] = sourcePtr[i]; } } /* * Function: PutInDynamicArray * ------------------------------------- * this function first tries to put item into arrayPtr at position. If it is * able to it returns. If postion is out of the bounds of arrayPtr * (ie position >= sizePtr) then a new array is allocated that can * contain item. This array is found * by using a while loop to double the size of the array until item can * be placed. Then the old information is copied from the old array into * the new one, values are initilized, and item is placed into the new array */ void PutInDynamicArray( int item, int position, int **arrayPtrPtr, int *sizePtr ) { int *newArPtr, /*int pointer to new array*/ *arrayPtr = *arrayPtrPtr, /*a pointer to the actual array*/ i; /*loop counter*/ /* if placing item within bounds of arrayPtr, then put * the item at postion in array and return */ if ( position < *sizePtr ) { arrayPtr[position] = item; return; } /*placing item out of bounds of the memory block at arrayPtr*/ /*the loop breaks when item can be placed in the new array*/ while( TRUE ) { /*double the sizePtr, and make a new array with that size*/ *sizePtr = (*sizePtr) * 2; newArPtr = MakeIntArray( *sizePtr ); /*if item can be put into newArPtr at position break*/ if ( position < *sizePtr ) { break; /*break out of loop*/ } /*end while*/ /*must get rid of this new array since item did not fit*/ FreeBlock( newArPtr ); } /*copy all the old information into new array*/ CopyArray( newArPtr, arrayPtr, *sizePtr / 2 ); /*initilize new locations to INITVAL*/ for( i = *sizePtr / 2; i < *sizePtr; i++ ) { newArPtr[i] = INITVAL; } /*get rid of old array*/ FreeBlock( arrayPtr ); /*put in item*/ newArPtr[position] = item; /*re assign **arrayPtrPtr to point to pointer of new array*/ *arrayPtrPtr = newArPtr; } /* end PutInDynamicArray*/ /*end of file*/ /* Name: Matheen Siddiqui * File: hw2-stats.h * Last modified on Thurs Sept 25 * -------------- * Library package to print a histogram */ #ifndef _hw2_stats_h #define _hw2_stats_h /*ITEM is used to represent an item in the histogram*/ #define ITEM '*' /* * Function: PrintHistogram * Usage: PrintHistogram( intArr, sizeIntArr, lo, hi, step ); * ------------------- * This function prints a histogram to the standard output. * The int array pointer, intArr, is the data the histogram is based * on. lo is the minimum value of the ranges and hi is the maximum * (both are ints). step is the number of integers in each range. * The histogram is printed in rows, using a ITEM to represent an element * in the given range. There is one range for values below lo, then * there are ranges, of size step that start at lo and end when a range * contains hi. Finnaly there is a range for values greater then * that of the last range. * The first range is labeled with a = before its starting value. */ void PrintHistogram(int *intArr, int size, int lo, int hi, int step); /* * Function: CountRanges * Usage: rangeArr = CountRanges( intArrPtr, size, lo, hi, step, sizeRangePtr ); * ------------------- * This function returns a pointer to an int array that holds the number * of elemnts in each range of the histogram of intArr, an int array pointer, * where lo is the minimum value of the histogram, hi is the maximum, and step * is the size of each range (all ints). The size of rangeArr is located at * the address specified by sizeRangePtr, after the function returns. * rangeArr[0] would then hold the number of values in * the first range, (less then lo), rangeArr[1] would hold the number * of values in the second range ( lo to size -1 inclusize) and so on,.. * until rangeArr[*sizeRangeArr], which holds the number of values in * last range (as specified in PrintHistogram). */ int* CountRanges( int *intArrPtr, int size, int lo, int hi, int step, int *sizeRangePtr ); #endif /*end of file*/ /* Name: Matheen Siddiqui * File: hw2-stats.c * Last modified on Thurs Sept 25 * -------------- * Implements the hw2-stats.h interface */ #include #include "genlib.h" #include "simpio.h" #include "hw2-stats.h" /******helper function prototypes*/ /* * Function: PrintRowItem * Usage : PrintRowItem( numItem ); * ------------------- * prints numItem ITEMs next to each other on standard output * a \n is printed after the ITEMs */ static void PrintRowItem( int nunItem ); /* * Function: GetLocation * Usage: range = GetLocation( num, lo, hi, step ) * ------------------- * this function returns which index of the array that holds * range of the histogram defined by * lo, hi and step, an element, num, is in. all vars are ints. */ static int GetLocation( int num, int lo, int hi, int step ); /******function definitions*******/ /* * Function: PrintHistogram * ------------------- * This function prints the histogram by first finding the number of * values in each range, using CountRanges and then printing corresponding * number of item's using PrintRowItem. The range lables are found * by using a counter in a for loop and incrementing it by step. */ void PrintHistogram(int *intArr, int sizeIntArr, int lo, int hi, int step) { int i, /*loop counter*/ rangeName, /*the starting value of each range*/ *rangeArrPtr, /*the pointer to the array that holds the number * of elements in each range*/ sizeRangeArr; /*the size of the array pointed to by rangeArrPtr*/ /*find the number of elements of intArr in each of the ranges*/ rangeArrPtr = CountRanges( intArr, sizeIntArr, lo, hi, step, &sizeRangeArr ); /*print the minimum range*/ printf("< %3d: ", lo ); PrintRowItem( rangeArrPtr[0] ); /*print the middle ranges*/ for( i = 1, rangeName = lo; i < sizeRangeArr-1; i++, rangeName += step ) { printf(" %3d: ", rangeName ); PrintRowItem( rangeArrPtr[i] ); } /*print the last range*/ printf(">=%3d: ", rangeName ); PrintRowItem( rangeArrPtr[sizeRangeArr - 1] ); /*free mem*/ free( rangeArrPtr ); } /*end PrintHistogram */ /* * Function: PrintRowItem * ------------------- * uses a loop to print numItem ITEM. */ static void PrintRowItem( int numItem ) { int i; /*loop counter*/ /*loop and print*/ for( i = 0; i < numItem; i++ ) { printf("%c", ITEM ); } /*new line*/ printf("\n"); } /* * Function: CountRanges * ------------------- * This function first finds the number of ranges in the histogram, * then creates an array of that size, initialized to 0. Then * the range of each value in intArrPtr is found using GetLocation, * and the corresponding value in the range array is incremented. */ int *CountRanges( int *intArrPtr, int size, int lo, int hi, int step, int *sizeRangeArr ) { /* the size of the range array. it is found by using (hi-lo)/step * +1 to find the number of middle ranges. Then 2 is added for * the =last range*/ int numRanges = (hi - lo)/step + 3, i, /*loop counter*/ /* this array holds the number of values of intArrPtr in * each range*/ *rangeArrPtr = (int *) GetBlock( numRanges * sizeof( int )); /*initialize to 0*/ for( i = 0; i < numRanges; i++ ) { rangeArrPtr[ i ] = 0; } for( i = 0; i < size; i++ ) { /* find what range the ith element in intArrPtr belongs to * and increment the number of values in that range by 1. * Recall that rangeArrPtr holds the number * of values in each range of the histogram.*/ rangeArrPtr[ GetLocation( intArrPtr[i], lo,hi,step )]++; } *sizeRangeArr = numRanges; return rangeArrPtr; } /*end CountRanges*/ /* * Function: GetLocation * ------------------- * uses (num-lo)/step + 1 to find the index of the range array * num belongs to. There are special cases for range number in the * =last ranges. */ static int GetLocation( int num, int lo, int hi, int step ) { /* max position is the index of the last position * in the range array. It is found the same way as numRanges * in CountRanges, but 1 must be subtracted to translate the * size of an array to the last index.*/ int maxPosition = (hi - lo)/step + 2; int position; /*the index of num*/ /*if less then minimum, num is in the fist range*/ if ( num < lo ) return 0; /* find what range num is in. * the + 1 is neccisary because the 0th index is for the = maxPosition ) return maxPosition; /*return range number of other elements*/ return position; } /*end GetLocation*/ /*end of file*/ /* * File: hw2-bank.c * Author: Matheen Siddiqui * Class: cs113 * Assignment: Home Work 2 * Due Date: Thursday, Sept. 25, 1997 * Last modified: Thursday, Sept. 25, 1997 * ----------------------------------------------------- * * USAGE: hw1-bank (no commandline arguments) * ====== * * * OVERVIEW: * ========= This program asks the user for up to four sets of interest rates, monthly payments, and initial deposits. It then asks for a length of time, in years, to print the balances of each set of data. Balances for each month are printed in the first year, and then the balances at the start of each year for the specified length of time. This program asks for the interest rate, initial balance, and monthly payment for each set until a negative value is entered for the interest, or the number of sets exceeds 4. Then the table is printed for the data and the user is reprompted for new data. To end this program enter 0 sets of data,( a negative interest on the first plan). r * * Algorithm Notes: * ================ * this program caculates the new balence using: newbalance = oldbalance + interst*oldbalnce + monthlypayment. to calculate the new balance at the end of each month. To find the interest at the end of a year that formula is used 12 time. * * Error Handling: * =============== If the user enters an invalid number, he is reprompted. Negative and zero values for length of time are considered invalid. * * */ #include #include #include "genlib.h" #include "simpio.h" /* Constants */ /* MONTHS_PER_YEAR is used to specify the number of months in a year MONTH is used to show that a month is used in calculating the new balance MAXPLAN is the maximum number of plans that can be entered. */ #define MONTHS_PER_YEAR 12 #define MONTH 1 #define MAXPLAN 4 /* Function Prototypes */ /* * Function: DisplayInstruction * Usage : DisplayInstructions() * --------------------------------- * this function takes no arguments and displays the * program instructions to standard output */ void DisplayInstructions(void); /* * Function: DisplayTable * Usage : DisplayTable(numPlan, intrstRate, initDepos, monPay, lenTime) * --------------------------------- * This function takes arrays that hold interest rate (intrstRate), * initial deposit (initDeopos) monthly payments (monPay) and * length of time to print in years (lenTime) for each plan and prints a table * of balances. The corresponding entries in each array hold the data for * a plan. The function prints, the balances at the end * of each month for the first year, and * then at the start of each year for lenTime years. On the table * there is a column for each of the numPlan balances. * * intrstRate, initDepos, monPay are doubles. * lenTime is an int, which must be at least 1. */ void DisplayTable( int numPlan, double intrstRate[], double initDepos[], double monPay[], int lenTime ); /* * Function: CalcNewBal * Usage : newBal = CalcNewBal(balance, intrstRate, monPay, months); * --------------------------------- * this function takes the current balance, balance, the interest rate * ,intrstRate, the monthly payment, monPay, and the number of months * the balance is going to sit in the bank,months, and returns the * new balnce at the end of the months. */ double CalcNewBal( double balance, double intrstRate, double monPay, int months); /* * Function: GetPlans * Usage : numPlan = GetPlan(intrstRateAr, initDeposAr, monPayAr, &lenTime) * --------------------------------- * This function reads in up to MAXPLAN plans from the user. intrstRateAr, * will holds the interest rates for the respective * plans, similarlay initDeposAr holds the initial deposits for each plan, * and monPayAr holds the monthly payments for each plan. lenTime is an int * that contains the length in years to print the table, after the function * is called. The number of plans entered by the user is returned. If 0 * is returned no plans were entered. (the arrays are double arrays) */ int GetPlans( double intrstRateAr[], double initDeposAr[], double monPayAr[], int *lenTime ); /* * Function: GetIntrstRate * Usage: intrstRate = GetIntrstRate( ); * ----------------------------- * This function returns an interest rate, a double, * specified by the user. * The interest rate may be negative. * num, is the plan number used to tell the user which plan * data is being entered for. */ double GetIntrstRate(int num); /* * Function: GetInitDepos * Usage: initDepos = GetInitDepos( ); * ----------------------------- * This function returns an initial deposit, a double, specified * by the user. There is no restriction on the range of initial deposit. * num, is the plan number used to tell the user which plan * data is being entered for. */ double GetInitDepos( int num ); /* * Function: GetMonPay * Usage: monPay = GetMonPay(); * ----------------------------- * This function returns a valid monthly payment, a long, * specified by the user. This can be positive negative or 0. * num, is the plan number used to tell the user which plan * data is being entered for. */ double GetMonPay( int num ); /* * Function: GetLenTime * Usage: lenTime = GetLenTime(); * ----------------------------- * This function returns a valid length of time, an int, * specified by the user. This value will be at least 1 */ int GetLenTime(void); /* Main Program */ int main() { double intrstRate[MAXPLAN], /*interest rate*/ initDepos[MAXPLAN], /*initial deposit*/ monPay[MAXPLAN]; /*monthly payments*/ int lenTime, /*length of time in year, for table to print*/ numPlan; /*the number of plans*/ DisplayInstructions(); /*loop is broken when no plans are entered is entered*/ while(TRUE) { numPlan = GetPlans( intrstRate, initDepos, monPay, &lenTime ); /*break condition*/ if (numPlan == 0){ break; } /*print table*/ DisplayTable( numPlan, intrstRate, initDepos, monPay, lenTime ); printf("\n\n"); } /* end while(TRUE) */ exit(0); /* let the OS know that the program terminated normally */ } /* end main */ /* * Function: DisplayInstructions * ----------------------------- * This function prints to standard output instructions for the user. */ void DisplayInstructions( void ) { printf("\n\nThis program calculates and displays the new balances\n"); printf("of the user's bank account. The interest rate, initial"); printf(" deposit,\n"); printf("monthly payments, and years to display are entered by the user.\n"); printf("The user can enter up to %d sets,or plans, of these for one\n" "table. The balances for each plan are printed in table format\n" "for every month ", MAXPLAN ); printf("of the first year, and then at the start of every year for\n"); printf("the length of time specified by the user.\n\n"); printf("To stop entering data for a given set of plans, enter a negative\n" "number for the interst. To terminate the program enter\n" "zero plans, or a negative number for the interest of the first\n" "plan.\n\n"); printf("The initial deposit and monthly payments\n"); printf("can have any value (negatives are allowed; you might start out\n"); printf("in debt, in which case you accumulate negative interest,\n" "or lose money each month) and the years\n"); printf("to display must be at least 1.\n\n"); } /* end DisplayInstructions() */ /* * Function: DisplayTable * ----------------------------- * This function takes prints a table of the new balances. The * table is printed with 2 for loops. The first loop prints the * new balnces at the end of each month for the first year, using a * nested loop to find the new balance of each plan. The * second prints the balnces at the start of each year. This loop too has * a nested loop to loop through the balances for all the planes. * New balances are calculated using CalcNewBal */ void DisplayTable( int numPlan, double intrstRate[], double initDepos[], double monPay[], int lenTime) { int mon=0, year=0, i; /*loop counters*/ double balance[MAXPLAN]; /*the current balance*/ for( i = 0; i < numPlan; i++ ) { balance[i] = initDepos[i]; } /*print the table heading*/ /*in the table 5 space are used in the first and second column, and 15 in the 3rd.*/ /*the loop prints the status of each plan*/ for( i = 0 ; i < numPlan; i++ ) { printf("\n\nPlan %d:\n", i + 1); printf("Initial Amount: $%0.2f\n", initDepos[i]); printf("Interest Rate: %0.4f\n", intrstRate[i]); printf("Monthly Payment: $%0.2f\n", monPay[i]); } printf("\n\n%5s %5s ", "Year","Month"); /*print a column heading for each plan's balance*/ for( i = 0; i < numPlan; i++ ) { printf("%13s %d", "Balance", i+1 ); } printf("\n"); /*print the first year*/ /*divide interest by 12 to get monthly interest*/ /*turn interest from a percent to a fraction*/ for( i = 0; i < numPlan; i++ ) { intrstRate[i] /= 100 * 12; } /*print by month balances for first year*/ for( year = 0, mon = 0 ; mon < MONTHS_PER_YEAR; mon++ ) { printf("\n%5d %5d", year,mon ); /*calculate and print the balance for each plan*/ for( i = 0; i < numPlan; i++ ) { printf("%15.2f", balance[i] ); balance[i] = CalcNewBal( balance[i], intrstRate[i], monPay[i], MONTH ); } } /*print balance of each plan at end of first year*/ printf("\n%5d %5d", 1, 0 ); for( i = 0; i < numPlan; i++ ) { printf("%15.2f", balance[i] ); } /*print the balance at the start of the rest of the years*/ for( year = 2, mon = 0; year <= lenTime; year++ ) { printf("\n%5d %5d", year,mon ); /*calculate and print for each plan*/ for( i = 0; i < numPlan; i++ ) { balance[i] = CalcNewBal( balance[i], intrstRate[i], monPay[i], MONTHS_PER_YEAR ); printf("%15.2f", balance[i] ); } } } /* end DisplayTable() */ /* * Function: CalcNewBal * ----------------------------- * The new balnces is found by using * newbal = oldbal + intrstRate*oldbal + monPay * for the number of months specified. */ double CalcNewBal( double balance, double intrstRate, double monPay, int months) { int i; /*loop var*/ /*calculate the new balance at the end of each month*/ for( i = 0; i < months ; i++ ) { balance += balance * intrstRate + monPay; } return (balance); } /*end CalcNewBal()*/ /* * Function: GetPlans * --------------------------------- * this function uses a for loop to consecutively read in the * interst, initial deposit, and monthly payment, for each plan. * When a negative is entered for the intrest this loop is * brooken, with break. Then the length of time is read. */ int GetPlans( double intrstRateAr[], double initDeposAr[], double monPayAr[], int *lenTime ) { int numPlan = 0; /*loop will break if negative interest entered*/ for( numPlan = 0; numPlan < MAXPLAN; numPlan++ ) { /*get input from user*/ intrstRateAr[numPlan] = GetIntrstRate(numPlan + 1); /*break out of while, if negative interest rate is entered*/ if ( intrstRateAr[numPlan] < 0 ) { break; } /*get other values*/ initDeposAr[numPlan] = GetInitDepos(numPlan + 1); monPayAr[ numPlan ] = GetMonPay( numPlan + 1); } if (numPlan > 0 ) { *lenTime = GetLenTime(); } return numPlan; } /*end GetPlans*/ /* * Function: GetIntrstRate * ----------------------------- * This function uses GetReal to read input */ double GetIntrstRate(int num ) { double intrst; /*the interest rate to be entered*/ /*prompt and get interest rate*/ printf("\nEnter the interest rate %d" "(negative value to quit): ", num ); intrst = GetReal(); return (intrst); } /*end GetIntrstRate()*/ /* * Function: GetInitDepos * ----------------------------- * uses GetReal to read input */ double GetInitDepos(int num) { double init; /*the initial deposit to be entered*/ /*prompt and get init depos*/ printf("\nEnter initial amount %d: $", num); init = GetReal(); return (init); } /*end GetInitDepos()*/ /* * Function: GetMonPay * ----------------------------- * Prompts and gets input using GetReal */ double GetMonPay(int num ) { double monPay; /*the monthly payment to be entered*/ /*prompt and get monthly payment*/ printf("\nEnter monthly payment amount %d: $", num); monPay = GetReal(); return (monPay); }/*end GetMonPay()*/ /* * Function: GetLenTime * ----------------------------- * The function loops while numbers entered by user are invalid. */ int GetLenTime(void) { double time; /*length of time to be entered by user*/ /*loops and reprompts for input while negative and 0 values are entered, * to ensure a valid number will be received from the user*/ printf("\nEnter number of years: "); while( (time = GetInteger()) <= 0 ) { printf("Please enter at least 1 for the number of years: "); } return (time); }/*end GetLenTime()*/ /* ----------- END OF FILE: hw2-bank.c ---------- */