Introduction to C - Program 1



Introduction to C - Programming Assignment #2

Assigned: 9/13/05 (Tuesday)

Due: 9/25 (Sunday) at 11:55pm WebCT time

Objective

1. How to write and use an if statement.

2. How to use the math library.

3. How to utilize compiler messages and printf statements for debugging.

4. How to look up information to aid solving a problem.

Problem A: Aid to an age calculator

Consider the problem of determining which of two people who lived in different times lived longer. At first glance, this seems like a trivial problem. Just look at how old they were when they died. Whoever was older lived longer. But, then there are cases of "ties." It's possible that both people lived to the age of 84. In that case, the tie can be broken by looking at who lived more days. This would be simple if there were always the same number of days in a year, but we know that there are not, because of leap years. To simplify your task a bit, your goal will be to determine the number of days in between January 1st of two different years (A.D.) For example, since 2004 was a leap year, the number of days in between January 1, 2003 and January 1, 2005 was 731 instead of 730. You will ask the user to enter two positive integers indicating the starting and ending years in the duration respectively. You will output the number of days in the specified duration. To simplify your task further, the range you will be given will contain at most one leap year. Assume that the current rules for leap years apply to any time period inputted.

Input Specification

1. The first integer entered by the user will be positive corresponding to the starting point of the duration in question.

2. The second integer entered will be greater than the first one, corresponding to the ending point of the duration in question.

3. The duration specified by the two inputs will contain at most one leap year.

Output Specification

Output the total number of days in the duration of time specified as follows:

There are X days in between 1/1/Y and 1/1/Z.

where X represents the number of days in the duration of time, Y represents the starting year of the duration, and Z represents the ending year of the duration.

Output Sample

Here is one sample output of running the program. Note that this test is NOT a comprehensive test. You should test your program with different data than is shown here based on the specifications given. The user input is given in italics while the program output is in bold.

Sample Run

What is the starting year of your time period?

2003

What is the ending year of your time period?

2005

There are 731 days in between 1/1/2003 and 1/1/2005.

Problem B: Where to study?

Your COP 3223 study group consists of you and two of your best friends. Each week you gather to review the wonderful course notes and discuss various C constructs shown in the text. But the most difficult part of the whole process is deciding who's house to study at! Your goal is to minimize the total travel distance. Thus, the goal is the minimize the sum of the distances from the two students' houses who are traveling to the other house. You will be given the x-y coordinates of each of the students' houses (in yards) and assume that students can reach each other's houses in straight lines. Your goal will be to determine at which student's (1, 2, or 3) house the study session should be held.

Input Specification

1. Each x and y coordinate of each student's house will be an integer. The unit represented by the coordinates will be yards.

2. No two students' houses will be separated by more than 10000 yards.

3. You will be guaranteed that there will exist a unique student's house that requires a shorter travel distance for the other two students compared to the other two possible arrangements.

Output Specification

Output who's house to study in the following format:

You should study at person X's house.

where X is either 1, 2 or 3.

Output Samples

Here is one sample output of running the program. Note that this test is NOT a comprehensive test. You should test your program with different data than is shown here based on the specifications given. The user input is given in italics while the program output is in bold.

Sample Run

What are x and y coordinates of the first person's house?

3 4

What are x and y coordinates of the second person's house?

-5 -2

What are x and y coordinates of the third person's house?

3 -2

You should study at person 3's house.

Problem C: Debugging

Your goal will be to debug the following program that attempts to print out a list of how much you owe on student loan after a certain number of months. Here is how the process works:

In order to make the calculation, you need to know the following values:

1) Initial Loan Amount

2) Yearly Interest Percentage

3) Monthly Payment

In particular, what happens is that interest accrues for a month and gets added to the initial loan amount. Then, the monthly payment gets subtracted from this total, yielding the new amount that is now due after the first monthly payment. Subsequent payments are determined in the same manner. The last payment is simply whatever is still owed (which will be less than or equal to the typical monthly payment).

For example, given an initial loan of $20,000 at a yearly interest rate of 6% (this translates to a monthly rate of 6/12 = .5%), and a payment of $300 a month, at the end of the month, $100 of interest will accrue (since this is .5% of $20,000) for a total of $20,100, but the monthly payment of $300 brings the total loan amount down to $19,800 dollars at the end of the first month. In a similar fashion, for this example, we can figure out that the amount of the loan is the following after each of the first three months:

|Month |Loan Value |

|1 |$19800 |

|2 |$19599 |

|3 |$19396.995 (exactly) |

For this part of the assignment, take the code below and try to compile it. Fix the syntax errors. Once you do this, run the program. Test and debug the program to remove logical errors. Keep a brief account of how you fix the program. For each mistake you find, write down:

1) How you found the mistake

2) What you did to fix the mistake

Store these in a list and turn this list in a Word document for this part of the assignment. Also, turn in the corrected code.

#include

int main(void) {

double loanamt, irate, payment;

int month=1;

printf("Enter the amount of the loan\n")

scanf("%lf", &loanamt);

printf("Enter the interest rate as a yearly percentage.\n");

scanf("%lf", irate);

printf("Enter the amount of the monthly payment.\n");

scanf("%lf", &payment);

printf("Month \t Amount Left\n");

while (loanamt < 0) {

loanamt = loanamt + irate/100*payment;

if (loanamt < payment) {

loanamt = loanamt - payment;

else

loanamt = 0;

}

printf("%lf \t %lf\n", month, loanamt;

return 0;

}

You may use this version of the code, or the file posted on the course website and WebCT.

Deliverables

You must submit four files total. Three files will be .c files and the fourth file will be a Word document documenting the changes you made to debug the faulty file given in part C of the assignment. Please submit the following three source files:

1) leap.c, for your solution to problem A

2) study.c for your solution to problem B

3) loan.c for your corrected solution to problem C.

Call the Word document that accompanies part C Debug.doc. All files are to be submitted over WebCT.

Restrictions

Although you may use other compilers, your program must compile and run using cygwin or gcc. Please use either your olympus account or jGRASP to develop your program. Your program should include a header comment with the following information: your name, course number, section number, assignment title, and date. Also, make sure you include ample comments throughout your code describing the major steps in solving the problem.

Grading Details

Your program will be graded upon the following criteria:

1) Your correctness

2) Your programming style and use of white space. (Even if you have a plan and your program works perfectly, if your programming style is poor or your use of white space is poor you could get 10% or 15% deducted from your grade.)

3) The detail given in your description of how you found each bug in part C and how you fixed it.

3) Compatibility to either cygwin in Windows or gcc under olympus. (If your program does not compile in either of these environments, you will get a sizable deduction from your grade.)

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download