Name: Zhang, Qia Log created: Thu Sep 18 18:01:37 EDT 1997 EXECUTION TESTS ========= ===== ============================================================ hw1-factor < hw1-factor.input Enter the number to be factored: The primary factors of 27302748 is 2*2*3*11*17*23*23*23 ============================================================ hw1-pi < hw1-pi.input Enter the number of boxes: By calculating one fourth the area of a radius 2.0 inch circle using 50 boxes, we evaluted pi to be 3.142566. ============================================================ hw1-bank < hw1-bank.input This program calculate the balance of a bank account. It will require you to enter the percentage interest rate of the account, the initial deposit amount, the monthly payment and the number of years for it. The program will then display the monthly balance for the first year, and the annual balance thereafter. To quit the program, just enter a negative value for the interest rate. A value smaller than zero for the initial deposit will mean that a loan was taking, and the program will treat it so. Same thing for the monthly payment, negative payment means that withdrawal occurs every month, and the balance would decrease. The number of years for the account has to be a positive intger, otherwise you will be asked again for it. Enter interest rate (negative value to quit): Enter initial deposit: $ Enter monthly payment: $ Enter number of years: Initial deposit: $1000.00; interest rate: 10.0000%;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 calculate the balance of a bank account. It will require you to enter the percentage interest rate of the account, the initial deposit amount, the monthly payment and the number of years for it. The program will then display the monthly balance for the first year, and the annual balance thereafter. To quit the program, just enter a negative value for the interest rate. A value smaller than zero for the initial deposit will mean that a loan was taking, and the program will treat it so. Same thing for the monthly payment, negative payment means that withdrawal occurs every month, and the balance would decrease. The number of years for the account has to be a positive intger, otherwise you will be asked again for it. Enter interest rate (negative value to quit): Enter initial deposit: $ Enter monthly payment: $ Enter number of years: Initial deposit: $-1000.00; interest rate: 18.0000%;monthly payment: $50.00 yrs mos balance 0 0 -1000.00 0 1 -965.00 0 2 -929.47 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 calculate the balance of a bank account. It will require you to enter the percentage interest rate of the account, the initial deposit amount, the monthly payment and the number of years for it. The program will then display the monthly balance for the first year, and the annual balance thereafter. To quit the program, just enter a negative value for the interest rate. A value smaller than zero for the initial deposit will mean that a loan was taking, and the program will treat it so. Same thing for the monthly payment, negative payment means that withdrawal occurs every month, and the balance would decrease. The number of years for the account has to be a positive intger, otherwise you will be asked again for it. Enter interest rate (negative value to quit): SOURCE FILES ====== ===== /* File: hw1-factor.c * ====== ============ * Author: Carolyn Zhang * Class: CS113 * Assignment: ch1, exercise 8 * Due: September 16, 1997 * ----------------------------------------------------------------------- * * USAGE: hw1-factor.c * ====== * * OVERVIEW: * ========= * This program takes a positive integer as input, and display it in the * form of its prime factors. * * Algorithm Notes: * ================ * Uses simple remainder function and division function, that when * the remainder of number/loop variable is zero, the new number is * given the value of number/loop variable. * * Known Bugs: None * =========== * * Enhancements: * ============= * When a prime number is entered, the program would recognize it, and * display so. * * Error Handling: * =============== * 1. User enters a non-integer value * Error message: display why input is invalid. * Error action: tell user to try again. * */ #include #include "genlib.h" #include "simpio.h" int main() { long num; /* Declare input as a long variable */ int i=2, /* Looping variable for the factor, assigned * value of 2, which is the first prime number.*/ j; /* Second looping variable, continueous from i */ printf("Enter the number to be factored: "); num=GetInteger(); /* Takes input,and do the first error handling */ while(num<1) /* Second error handling */ { printf("\nThis is not a valid input. \n"); printf("The number has to be a positive integer! \n\n"); fflush(stdin); /* Erase previous input from memory */ printf("Enter the number to be factored: "); num=GetInteger(); } if(num==1) { printf("\nThis is a prime number. \n"); } else { while(i<=num) /* Loop until either loop conditions are met */ { if (i==num) printf("\nThis is a prime number. \n"); else if(num%i==0) /* Condition of prime factor */ { printf("\nThe primary factors of %ld is %d",num,i); /* Print the first factor alone to * achieve the desired output without * an extra multiplication sign. */ num=num/i; for(j=i; j<=num; j++) /* Second looping variable takes * effect, this will handle the rest * of the factorization. */ { while ( num % j == 0 ) { num = num/j; printf ("*%d",j); } } /* end for statement */ } /* end else statement */ i++; } printf("\n\n"); }return 0; } /* -------------- End of File: hw1-factor.c ---------------------- */ /* File: hw1-pi.c * ====== ======== * Author: Carolyn Zhang * Class: CS113 * Assignment: ch1, exercise 11 * Due: September 16, 1997 * ------------------------------------------------------------------ * * USAGE: hw1-pi * ====== * * OVERVIEW: * ========= * This program estimate the value of pi by calculating one fourth the * area of a radius 2 circle. It accepts a positive integer as the * input for the number of boxes used to calculate the area, and returns * the estimate pi value. * * Algorithm Notes: * ================ * use width = radius/box * base = width/2 + i*width * height^2 = radius^2 + base^2 * area = height * width * * Known Bugs: none * =========== * * Enhancements: * ============= * A power function to calculate the height. * * Error Handling: * =============== * 1 User enters a non-integer number. * Error message: display why input is not acceptable. * Error action: Let user try again. * 2 User enter a zero or negative integer. * Error message: display why input is not acceptable. * Error action: let user try again. * * Other Comments: * =============== * Since the estimate for pi is based on the areas of the boxes, so the * more boxes used, the more accurate the value is going to be. * */ #include #include #include "genlib.h" #include "simpio.h" /* Constant is used for the radius of the circle */ #define R 2.0 /* Function prototypes */ /* * Function: AreaOfBox * Usage: AreaOfBox(x,width) * -------------------------- * This function calculate the area of a specific box. */ float AreaOfBox (float, float); /* * Function: Power * Usage : Power(val, degree) * -------------------------- * This program calculate a given value which is to be raised * to a given degree. */ float Power (float, int); /* Main program */ int main() { int box; /* assume input is an integer */ float x, /* base of the triangle formed from the origin to * the further side of the box */ i, /* Loop variable for box */ wid, area=0; /* assign initial value of zero for area */ printf("Enter the number of boxes: "); box = GetInteger(); while( box<1 ) /* second error handling */ { fflush(stdin); printf("Invalid Response! \n"); printf("Enter a positive integer as the number of boxes: "); box = GetInteger(); } wid = R/box; for(i=0; i #include #include"genlib.h" #include"simpio.h" #include /* Constant: the number fo months per year as 12 */ #define MONTHS 12 /* Function Prototypes */ /* * Function: Instruction * Usage: Instruction() * ------------------------ * This procedure display the instruction for the user */ void Instruction(void); /* * Function: DisplayTable * Usage: DisplayTable(year, deposit, interest, payment) * ----------------------------------------------------- * This program display the monthly and annual balance in a table form */ void DisplayTable(int, double, double, double); /* * Function: BalanceYear * Usage: BalanceYear(year, deposit, interest, payment) * ---------------------------------------------------- * This program calculate the account balance after the indicated year. * It simply loops through the balances start from year one. */ double BalanceYear(int, double, double, double); /* * Function: BalanceMonth * Usage: BalanceMonth(balance, interest, payment) * ------------------------------------------------- * This program calculate the balance after one month. */ double BalanceMonth(double, double, double); /* main program */ int main() { int year; double deposit, interest, payment; do { Instruction(); printf("Enter interest rate (negative value to quit): "); interest=GetReal(); if(interest>0) /* condition for continuing(positive input * for interest) */ { printf("\nEnter initial deposit: $"); deposit=GetReal(); printf("\nEnter monthly payment: $"); payment=GetReal(); do{ printf("\nEnter number of years: "); year=GetInteger(); }while( year<1 ); DisplayTable(year,deposit, interest, payment); } /* end if statement */ }while( interest>0 ); /* while loop to continue program until * quit condition (negative interest)*/ return 0; } /* end main */ /* * Function: Instruction * Usage: Instruction() * ------------------------ * This procedure display the instruction for the user */ void Instruction(void) { printf(" This program calculate the balance of a bank account.\n"); printf("It will require you to enter the percentage interest rate\n"); printf("of the account, the initial deposit amount, the monthly\n"); printf("payment and the number of years for it. The program will\n"); printf("then display the monthly balance for the first year, and\n"); printf("the annual balance thereafter. \n\n"); printf(" To quit the program, just enter a negative value for \n"); printf("the interest rate.\n"); printf(" A value smaller than zero for the initial deposit will\n"); printf("mean that a loan was taking, and the program will treat it \n"); printf("so.\n Same thing for the monthly payment, negative payment \n"); printf("means that withdrawal occurs every month, and the balance would\n"); printf("decrease.\n The number of years for the account has to be\n"); printf("a positive intger, otherwise you will be asked again for it.\n\n"); } /* end Instruction() */ /* * Function: DisplayTable * Usage: DisplayTable(year, deposit, interest, payment) * ----------------------------------------------------- * This program display the monthly and annual balance in a table form * INPUT: integer for year * double for deposit * double for interest * double for payment * OUTPUT: prints a table consists of the year the month and the account * balance at that time. */ void DisplayTable(int year, double deposit, double interest, double payment) { int i, /* loop variable for the year */ j; /* loop variable for the month */ double balance=deposit; /* assign initial balance as the deposit */ printf("\nInitial deposit: $%.2f; interest rate: %.4f%%;",deposit, interest); printf("monthly payment: $%.2f\n\n",payment); printf("yrs mos balance\n"); interest = interest/MONTHS/100; /* calculate the monthly interest */ for(i=0; i<=year; i++) /* loop through each year */ { j=0; if (i==0) /* for the first year only */ { printf("%3d %3d %10.2f\n",i,j,balance); /*Display the original * balance, before any payments. */ for(j=1; j