Name: Blumstein, David Log created: Thu Sep 18 17:26:39 EDT 1997 EXECUTION TESTS ========= ===== ============================================================ hw1-factor < hw1-factor.input Enter Integer: 27302748 = 2 * 2 * 3 * 11 * 17 * 23 * 23 * 23 ============================================================ hw1-pi < hw1-pi.input This program estimates PI by taking the area of rectangles such that their midpoints lie on the perimeterthe circle. Area of 1/4 circle, 2 inches radius=PI Enter a number of rectangles: With 50 rectangles, pi is approximated to be: 3.1425655525 ============================================================ hw1-bank < hw1-bank.input This program calculates the interest added to your account. It will list your balance at the beginning of each of the first 12 months and the beginning of each year thereafter. 1. A negative interest rate quits, 2. A negative payment amount assumes that you withdraw that amount from your initial deposit(or loan). 3. A negative initial deposit assumes that you borrow that amount. 4. A negative number of years is unacceptable. Enter interest rate (negative value to quit): Enter initial deposit: Enter monthly payment: Enter number of years from now to which to calculate: initial amount: 1000.00; interest rate: 10.000000; monthly payment: 100.00 yrs mos balance 0 0 1000.00 0 1 1108.33 0 2 1217.57 0 3 1327.72 0 4 1438.78 0 5 1550.77 0 6 1663.69 0 7 1777.56 0 8 1892.37 0 9 2008.14 0 10 2124.87 0 11 2242.58 1 0 2361.27 2 0 3865.08 3 0 5526.36 This program calculates the interest added to your account. It will list your balance at the beginning of each of the first 12 months and the beginning of each year thereafter. 1. A negative interest rate quits, 2. A negative payment amount assumes that you withdraw that amount from your initial deposit(or loan). 3. A negative initial deposit assumes that you borrow that amount. 4. A negative number of years is unacceptable. Enter interest rate (negative value to quit): Enter initial deposit: Enter monthly payment: Enter number of years from now to which to calculate: initial amount: -1000.00; interest rate: 18.000000; monthly payment: 50.00 yrs mos balance 0 0 -1000.00 0 1 -965.00 0 2 -929.48 0 3 -893.42 0 4 -856.82 0 5 -819.67 0 6 -781.97 0 7 -743.70 0 8 -704.85 0 9 -665.42 0 10 -625.40 0 11 -584.79 1 0 -543.56 2 0 2.17 3 0 654.66 4 0 1434.78 5 0 2367.51 This program calculates the interest added to your account. It will list your balance at the beginning of each of the first 12 months and the beginning of each year thereafter. 1. A negative interest rate quits, 2. A negative payment amount assumes that you withdraw that amount from your initial deposit(or loan). 3. A negative initial deposit assumes that you borrow that amount. 4. A negative number of years is unacceptable. Enter interest rate (negative value to quit): SOURCE FILES ====== ===== /* * File: hw1-factor.c * Author: David Blumstein * Class: CS113, Fall '97 * Assignment: HW 1, Problem 1 (Roberts, Chap. 1, Ex. 8) * Due: Mon, 9/15/97 * Modified: Sat, 9/13/97 * ------------------------------------------------------ * * USAGE: hw1-factor (no commandline arguments) * ===== * * OVERVIEW: This program factors a user inputed integer using 'prime * ======== factorization' * * ALGORITHM NOTES: Beginning with 2, the first possible prime factor, it * =============== divides by the prime factors as many times as possible * before moving to the next possible prime factor * * KNOWN BUGS: The program in its present form is limited to factoring numbers * ========== no larger than 2^100, to deal with larger #s, MAXSIZE must be * be increased. * * ERROR HANDLING: Negative numbers are not accepted, the input loops until * ============== the user enters a positive integer. GetInteger outlaws * non-integers * * OTHER COMMENTS: none * ============== * */ #include #include #include "genlib.h" #include "simpio.h" #define MAXSIZE 100 /* 2^MAXSIZE is the largest # we can factor */ void PrintFactors (int factors[MAXSIZE], int count, int number); int main() { int i; /* loop variable */ int number; /* user-entered integer */ int newNumber; /* copy of number for mathematical purposes */ int count=0; /* number of factors */ int factors[MAXSIZE]; /* list of factors */ /* gets user input, loops until user enters a positive integer */ while (TRUE) { printf("\nEnter Integer: "); number=GetInteger(); if (number>0) break; printf("\nNegative numbers have too many factors!"); printf("\ne.g. (-10)=2*2*5=(-2)*2*5=(-2)*(-2)*(-5)=...\n"); printf("\nPlease Enter a POSITIVE integer."); } /* end while(TRUE) */ /* creates copy of user-entered integer */ newNumber=number; /* loops through all possible factors of integer, adding each confirmed * factor to the array factors[] and dividing newNumber by that factor. */ for (i=2; i #include #include "genlib.h" #include "simpio.h" #include #define RADIUS 2 /* circle radius */ double Height(double mid1, int i); double XDistance(double mid1, int i); void PrintDirections(); int main() { int rectangles; /* user-entered # of rectangles */ int i; /* loop variable to loop through rectangles */ double mid1; /* x displacement of the midpoint of the 1st rectangle */ double pi=0; /* the approximation for PI */ /* directions */ PrintDirections(); /* gets input, negative and zero rectangles are unacceptable */ while (TRUE) { printf("\n\nEnter a number of rectangles: "); rectangles=GetInteger(); if (rectangles>1) break; printf("\nTo have area, this program needs to count a"); printf("number of rectangles ONE or GREATER"); } /* the midpoint of the 1st rectangle is necessary * for my calcs = 1/number of rectangles * it is easier and more readable to do this calc in main * rather than repeating it in each function */ mid1= (1/(double)rectangles); /* loops through rectangles, adding areas to pi estimate * area of quarter circle=sum of area of rectangles (h*b) */ for (i=1; i<=rectangles; i++) { pi+= (2*mid1*Height(mid1, i)); } /* printing output; * to do this through a function would be more convoluted */ printf("\nWith %d rectangles, \npi is approximated to be: %.10f\n\n", rectangles, pi); exit(0); } /* Function: Height * Usage: Height (double mid1, int i); * Purpose: calcs height of rectangle: h=(R^2-Rmidpoint^2)^.5 * ------------------------------------------- */ double Height(double mid1, int i) { return ( sqrt (pow(RADIUS,2)-pow(XDistance(mid1,(double)i), 2))); } /* end function: Height */ /* Function: XDistance * Usage: XDistance(double mid1, int i); * Purpose:calcs xdistance of midpoint of rectangle x=(2i-1)(midpoint 1st rect) * ----------------------------------------- */ double XDistance(double mid1, int i) { return ( ((2*(double)i)-1)*mid1); } /* end function: XDistance */ /* Function: PrintDirections * Usage: PrintDirections(); * ------------------------------- */ void PrintDirections() { printf("\n\nThis program estimates PI by taking the area of"); printf(" rectangles such that\ntheir midpoints lie on the perimeter"); printf("the circle.\nArea of 1/4 circle, 2 inches radius=PI"); } /* end function: PrintDirections */ /*------End of File: hw1-pi.c --------------------------*/ /* * File: hw1-bank.c * Author: David Blumstein 194-58-3662 * Class: CS113 Fall '97 * Assignment: HW1, Problem 3 (Interest) * Due Date: Mon, 9/15/97 * Last modified: Fri, 9/12/97 * ----------------------------------------------------- * * USAGE: hw1-bank (no commandline arguments) * ====== * * OVERVIEW: This program calculates and displays the time varied amount of * ========= money in an interest bearing account. It prints to screen a * table of balances, immediately after monthly payment, for the * first 12 months and a user defined number of years thereafter. * * Algorithm Notes: calculates months' balance by bal+= accrued interest and monthly payment * ================ * * Known Bugs: Huge values for 'years' and 'balance' may be unprovided for * =========== and may require "long" and "long double" (I am unsure) * I assumed this program would not be used for such complex operations * * Enhancements: none * ============= * * Error Handling: Negative or zero time span is looped out during input phase, * =============== GetReal/GetInteger loop out invalid entries otherwise. * * Other Comments: Negative monthly payment results in a withdrawl. * =============== Negative initial deposit results in a loan. * */ #include #include #include "genlib.h" #include "simpio.h" /* Constants */ #define MONTHS_PER_YEAR 12 /* Function Prototypes */ void DisplayInstructions(void); void DisplayTable(double interest, double deposit, double payment, int years); /* Main Program */ int main() { /* variable declarations go here */ double interest; /*interest rate*/ double deposit; /*initial deposit*/ double payment; /*monthly payment*/ int years; /*num of years*/ while(TRUE) { DisplayInstructions(); /* * this will repeat forever until the user enters a negative * interest rate (quitting) */ /* getting the input */ printf("\nEnter interest rate (negative value to quit): "); interest=GetReal(); if (interest<0) /* checks to see whether user wants to quit */ {break;} printf("\nEnter initial deposit: "); deposit=GetReal(); printf("\nEnter monthly payment: "); payment=GetReal(); /*checks that the number of years is positive */ while (TRUE) { printf("\nEnter number of years from now to which to calculate:"); years=GetInteger(); if (years>0) break; printf("\n\nThere is no negative time here!"); } DisplayTable(interest, deposit, payment, years); } /* end while(TRUE) */ exit(0); /* let the OS know that the program terminated normally */ } /* end main */ /* * Function: DisplayInstructions * Usage: DisplayInstructions(); * ----------------------------- * This function prints to standard output instructions for the user. */ void DisplayInstructions() { printf("\n\nThis program calculates the interest added to your account.\n"); printf("It will list your balance at the beginning of each of the first"); printf("\n12 months and the beginning of each year thereafter.\n\n"); printf("1. A negative interest rate quits,\n"); printf("2. A negative payment amount assumes that you withdraw that amount from your initial deposit(or loan).\n"); printf("3. A negative initial deposit assumes that you borrow that amount.\n") ; printf("4. A negative number of years is unacceptable.\n\n"); } /* end DisplayInstructions() */ /* * Function: DisplayTable * Usage: DisplayTable(double interest, double deposit, double payment, int years); * ----------------------------- * Displays a table of account balance for the first 12 months and each * year-beginning thereafter. */ void DisplayTable(double interest, double deposit, double payment, int years) { int month; /*current month*/ int presentYear=0; /*current year*/ double balance=deposit; /*balance*/ double monthsInterest; /*monthly interest*/ /* table header */ printf("\n\n\ninitial amount: %.2f; interest rate: %f; monthly payment: %.2f\n", deposit, interest, payment); printf("\n\nyrs\tmos\tbalance\n"); /*loops through years*/ for (presentYear=0; presentYear<=years; presentYear ++) { /* loops through months, calcs and prints balance at beginning of each * [first year only] or beginning of each year [each year thereafter] */ for (month=0; month