CMSC 106 Project #2 Spring 2001


Due date: Tuesday, February 27

1. Purpose

In this project you will write a program using various conditional statements.

For this project you will be writing a tool that will help the person who deternmines if a mortgage application should be approved and what mortgage rate the customers will receive for that loan. These decision are based on the person's credit rating, the amount of the loan and the length of time they elect to have that loan.

2. Project description

As you read this section, you may also want to refer to the ``Sample output'' section below.

The input read and output which should be produced is discussed below.


  1. The program must first read a client's application for a mortgage. The application will be given to the program thorugh standard input redirection from an ASCII text file. As the programmer, you can assume the lines of the input file will be exactly as described here. You do no need to do any checking of the validity of the input.

    1. The first line must contain the id number of the person submitting. This ID number will be an integer. This ID will be less than 1000 in numeric value.
    2. The second line will contain a single integer telling the amount of loan being requested. The amount represents a whole dollar amount. (There will not be any cent portion in the loan amount.)
    3. The third line will contain the three pieces of information which will help the program figure out what that person's credit rating is. These three pieces of information will all appear on this one line and they will be delimited by white space. The three pieces of information will be a (1) Payment History (number of late payments on previous loans): a positive integer, (2) an annual income : a positive float, and (3) Length of Credit History (in years): a positive integer. These will be used to determine a credit rating number. This number will be an integer between 1 and 5 inclusively. A lower number for the credit rating means the person has a better credit rating than a higher number.
    4. The fourth line will contain a number of years the person would like to have in order to pay back the loan. This value will be a floating point number where the fractional portion tells the part of a year (e.g. 0.5 means 6 months, 0.333 means 4 months, etc.).

  2. The first line of the program's output must have the line ``Mortgage Application for XXX submitted''. Where the XXX is replaced by the cutomer ID as read from the loan application file. The ID must be printed as a 3 digit integer (with preceding zeros if necessary to make it 3 digits).

  3. The second line of the program's output must tell the other information read or derived from the application in the following order: (1) credit rating, (2) loan amount - printed as a monetary amount with a dollar sign the dollar amount and 2 decimal places beyond the decimal point, (3) the amount of time - printed as the integer number of years followed by the word ``years'' and the integer number of months followed by the word ``months''. All of these must be printed even if the value is zero. The number of months must be calculated so that it is rounded up (if any portion of a month is needed, the whole month must be counted).

  4. The third line of output must contain either the word ``approved'' or the word ``rejected''. If this line says ``rejected'', there will be no further numerical output given. If this line says ``approved'', it must be followed by all of the lines described below.

  5. The fourth line of output (for an ``approved'' loan) must tell the rate of the mortgage as calculated based on the rules specified below.

  6. The fifth line of output (for an ``approved'' loan) must tell the amount of interest that will be due for the first payment period (the amount of the loan times the rate of interest).

  7. The last line of output must say either that we look forward to serving you using the exact word ``serving'' or that we are sorry we could not help you using the exact word ``sorry''.

2.1 Rules on which Credit Rating is Determined

  1. Each of the 3 pieces of information can earn the applicant points. If the points earned are over 100, the person receives a credit rating of 1; not over 100, but over 80 leads to a credit rating of 2; not over 80, but over 60 leads to a credit rating of 3; not over 60, but over 50 leads to a credit rating of 4; and any total of 50 or fewer points leads to a credit rating of 5.

  2. The Payment history can earn the person up to 30 points. If they have never had a late payment, they earn all 30 points. They lose one point for each late payment they have had in their history. If they have had 30 or more late payments they receive zero of these points.

  3. The Income can earn the person up to 50 points. They earn 1 point for every $10,000 of yearly income (to a maximum of 50 points). In other words, if they earn $20,000 they get 2 points and if they earn $200,000 they get 20 points. Any part of $10,000 does not give them any extra points so someone who earns $209,000 also receives only 20 points. The maximum is 50 points so anyone who earns $500,000 or over receives 50 points. If the income is less than $10,000, they will earn none of the 50 points available in this section.

  4. The Length of Credit History can earn a person up to 30 more points. If they have 20 or more years, they receive all 30 points. If they have less than 20, but at least 15 years they get 15 points. If they have less than 15, but at least 10 years they get 10 points. If they have less than 10 years they get 0 points.

2.2 Rules on which loans are approved

  1. Since we are a company that does not take risks, we will not grant a loan to any person who has a credit rating of 4 or 5.

  2. If the person's credit rating is a 3, we will only grant a loan of less than $100,000. The person will also be charged the highest loan rate for that loan which is 10%. Any request (made by a person with this credit rating) for a loan which is $100,000 or more will be denied.

  3. If the person's credit rating is a 2, we will grant a loan if the amount is less than $500,000. If the person is planning to take 5 years or over to pay that money back they will be charged the highest loan rate of 10%. If they plan to take less than 5 years, but the amount is more than $200,000, they will be charged 7%, but if the amount is less than or equal to $200,000, they will be charged 6%.

  4. If the person's credit rating a a 1, we will not reject their loan application. They can be charged a higher rate based on their requested amount of time, but their loan application will be approved. If you divide the loan amount by the total number of months they will be making payments (this value is equivalent to what is printed on the second line of output in the form of years and months) - you calculate the amount they would need to payback each month. (This calculation assumes no interest is being paid - that the amount of interest due that month would need to be paid in addition to the amount calculated here.) If the person's monthly payback amount is $10,000 or over, they will be granted the 5% interest rate. If it is less than $10,000 and it is $5,000 or over, they will be charged 6%. If their planned payback amount is less than $5,000, they will be charged 7%.

3. Project requirements

All your C programs in this course should be written in ANSI C with trapping of unintialized variables, which means they must compile and run correctly with cc -std1 -trapuv on the OIT UNIX Class Cluster. Even if you already know what they are, you may not use any C language features other than those introduced in Chapters 1 through 5 of your textbook. Note that as a result loops, arrays, and user-defined functions may not be used. In addition, your program must contain only one single return statement at its end. Using C features not in these chapters will result in losing credit.

Your program must have a comment near the top which contains your name, login ID, student ID, your section number, your TA's name, and an original description of the action and operation of the program. Do not put your alias in this comment! Your program should be written using good programming style and formatting, as discussed in class and throughout your textbook. For this project, style is considered to consist of:


4. Developing your program

You may want to skip this section at first, read the rest of the project, and come back to study it carefully when you are about to begin writing your program.

4.1 Possible development steps

It is very important to continue to type in only a part of your program at a time and verify that it's correct, before going on. Any mistake is much easier to locate this way. The idea is that at all times you have a correct program, which may be incomplete and solve only part of the problem but at least the part you've written so far works.

In other words, do not attempt to write your program to perform all the necessary tests or produce completely correct output at first. Add code to perform only one processing step at a time and produce its output, and then add another until your program is complete.

For example, here is one way to begin developing the program one part at a time. Write the code for each of the following steps, and compile and test at each stage to make sure the correct results are printed for different inputs before going on to the next step listed.


  1. First you could write code which reads the loan application. To be sure they are being read correctly, add statements which echo each one immediately after reading.

  2. If your code so far works, write the output lines which correctly format the echo of the information for the first lines of output as described above.

  3. When you have verified these input and output statements work properly, you could determine which set of rules will be used for this loan application based completely on the credit rating given.

  4. Then go through the different cases - one for each credit rating and make sure the output produced is correct for that situation.

  5. Finish the remaining cases one at a time, and thoroughly test your program using a variety of input combinations.

4.2 Finding compilation errors

  1. As before, if you get the error ``Invalid statement'' and the compiler identifies the very last line in your program, it usually means you have mismatched braces. Carefully match up all of your braces, and make sure that there is a closing brace to match every opening brace. This is much easier to do using a printout, and is far easier when your program is formatted neatly using proper indentation and placement of braces. Draw a line pairing up each set of matching braces until you can tell where one is missing.

  2. If you get the error ``Invalid statement'' and the compiler points right to an else, the else is not placed so it's inside an if statement. Only one statement can appear as the subsidiary statement after an if, and only one statement can appear after an else. If you want more than one statement to appear in a place where one statement belongs, you can use braces to group them into a compound statement. You may have forgotten braces around the statements which were supposed to be executed if the condition in the if/else was true (the first group of subsidiary statements). Alternatively, it's possible you made a mistake and have more elses than ifs. You are much less likely to make this mistake when your code is properly formatted and indented, with each else lined up to clearly indicate which if statement it belongs to.

4.3 Program debugging

If your program doesn't produce the results you think it should, the debug printf statement is the best tool you can use. If, for instance, you're not sure why one branch of an if/else statement is being executed when you think the other should be, put a debug printf statement right before the whole if statement which prints the values of all the variables used in the condition. Maybe one of them doesn't have the value which you think it should have at that point. On the other hand, if you run your program and see that the variables all have correct values, then the condition itself is probably not expressed correctly. Or perhaps nothing prints in some case where you think something should. Try adding an else to every if statement which doesn't already have one, which prints a message indicating what must be true for that statement to be printed. Also in other cases, add as many printfs as you need to determine what is true about the data currently stored.

Make sure the debug printfs you add are descriptive in nature to tell you any information you may need to determine what your program is doing, such as what line in in the program is producing this output or what the values of all relevant variables are at that point. This is much more useful than printing something like ``I am here'' in your debug printfs. Also, make sure the debug printfs do not change what statements are controled by the if or the else.

Also for technical reasons not worth explaining, it is very important that every debug printf ends with the newline character '\n'. Although these debug printfs will make your output look incorrect, they will help you develop your program correctly.

Make sure that all debug printfs are removed before you submit your source code file for grading. After removing them, before submitting your source file, make sure that you have compiled, run and tested your project again to be sure you did not accidentally change it when you were removing those lines.

If you can't find the problem after trying to debug your program using these techniques, bring printouts of your program and any compiler errors or execution results to office hours. To help us find the mistake as quickly as possible, be prepared to demonstrate and explain how you have tested your program, and what results you got.

4.4 Program testing

It is very important to test your program against all different possibilities of input data, not just the values shown in the sample output section below. Doing this correctly involves thinking of what other input values are possible based on the project description, and what results the program should produce in those cases.

As an example, say you had to write a program which reads three integers into variables a, b, and c, and prints the name of the variable which had the largest value. Imagine that you wrote the following program:



#include <stdio.h>

main() {
  int a, b, c;
  scanf("%d%d%d", &a, &b, &c);

  if (a <= b) {

    printf("b");

    if (b <= c)
      printf("c");

  } else printf("a");

  printf("\n");
  return 0;
}


Try typing, compiling, and executing this program.

If you run the program and enter 2 6 4 it prints ``b'', which is correct.

If you run it and enter 6 4 2 it prints ``a'', which is correct.

You might assume the program works since you ran it twice with different input values, but it is not correct.

Try entering 4 2 6 and it prints ``a'', which is wrong.

Try entering 2 4 6 and it prints ``bc'', which is also wrong.

There are other possibilities of input values which cause incorrect results as well.

The conclusion is that it is always necessary to think of all the different possible combinations and possibilities of input values which might appear in a program's input, and test it so you are certain it works correctly in all cases, not just for one or two different inputs.

If you are curious, one correct program to perform this task is as follows. The program could also be written using the logical operators.



#include <stdio.h>

main() {
  int a, b, c;
  scanf("%d%d%d", &a, &b, &c);

  if (a <= b)

    if (b <= c)
      printf("c");
    else printf("b");

  else

    if (a <= c)
      printf("c");
    else printf("a");

  printf("\n");
  return 0;
}


4.5 Helpful hints

One development policy which is extremely important is to use proper indentation from the beginning. Many students type in their programs any way at all, with poor or no indentation, figuring once their project works correctly they will clean it up before submitting it. This is a very bad idea, because it is much harder to find certain errors in a program with poor indentation, and it's much easier to make these types of mistakes. You are much more likely to have problems such as mismatched braces, or misplacing an else, or misunderstanding which statements are subsidiary to which if statements if your code is improperly formatted or indented, or the braces are incorrectly placed. In the Q+A sections your textbook shows several popular styles of indentation. It doesn't matter which you use, as long as your code is clear and your indentation is consistent, subsidiary statements are all indented, and statements at the same level are all indented the same way. If you come to office hours for help with a program which has missing or very poor indentation, you will be asked to correct and reformat your program before we are able to help you further with it.

5. Academic integrity statement

Any evidence of unauthorized use of computer accounts or cooperation on projects will be submitted to the Student Honor Council, which could result in an XF for the course, suspension, or expulsion from the University. Projects are to be written INDIVIDUALLY. For academic honesty purposes, projects are to be considered comparable to a take-home exam. Any cooperation or exchange of ideas which would be prohibited on an exam is also prohibited on a project assignment, and WILL BE REPORTED to the Honor Council.


VIOLATIONS OF ACADEMIC HONESTY INCLUDE:


  1. failing to do all or any of the work on a project by yourself, other than assistance from the instructional staff.

  2. using any ideas or any part of another student's project, or copying any other individual's work in any way.

  3. giving any parts or ideas from your project, including test data, to another student.

  4. having programs on an open account or on a PC that other students can access.

  5. transferring any part of a project to or from another student or individual by any means, electronic or otherwise.


IT IS THE RESPONSIBILITY, UNDER THE UNIVERSITY HONOR POLICY, OF ANY STUDENT WHO LEARNS OF AN INCIDENT OF ACADEMIC DISHONESTY TO REPORT IT TO THEIR INSTRUCTOR.

6. Submitting your project

Your project must be electronically submitted by the date above, before 11:00pm, to avoid losing credit as indicated on the syllabus. No projects more than two days late will be accepted for credit without prior permission or a valid medical excuse, as described on your syllabus. Only the project which you electronically submit, according to the procedures provided, can be graded; it is your responsibility to test your program and verify that it works properly before submitting. Lost passwords or other system problems do not constitute valid justifications for late projects, so do not put off working on your program or wait to submit it at the last minute!

Turn in your assignment using the ``submit'' program as before, except using ``2'' for the project number. You are to submit only the .c file containing your source code, not the executable version of your program! If your program is in a file named ``loan.c'', submit would be run as shown.


% submit  2  loan.c

Before you submit your project, you must exactly follow the specific submission checklist in the ``Testing projects before submitting'' handout separately posted by your instructor!


7. Sample output

Assuming the name of the executable version of the program is ``loan.x'', here are three executions for three different sets of input values. Note that the input file is shown separately before each output set, and nothing is typed by the user as the program is run.

Be sure to test your program against a variety of inputs, so you are sure it works in all circumstances!



% cat input1
964
10000
0 100000.00 30
1.5

% loan.x < input1
Mortgage Application for 964 submitted
Rating: 3, Amount: $10000.00, Time: 1 years and 6 months
Your loan application is accepted.
Your loan will be granted at the 10% rate.
You will owe $1000.00 in interest.
We look forward to serving you with this matter.

% cat input2
007
500000
20 1234.56 0
1.1

% loan.x < input2
Mortgage Application for 007 submitted
Rating: 5, Amount: $500000.00, Time: 1 years and 2 months
Your loan application is rejected.
We are sorry we could not help you at this time.

% cat input3
14
1000000
0 1000000.00 20
3.0

% loan.x < input3
Mortgage Application for 014 submitted
Rating: 1, Amount: $1000000.00, Time: 3 years and 0 months
Your loan application is accepted.
Your loan will be granted at the 5% rate.
You will owe $50000.00 in interest.
We look forward to serving you with this matter.

  



Steve Scolnik
2001-02-17

Web Accessibility