CSCE 150A (Fall 2009) Homework 4

Assigned Friday, October 30
Due Friday, November 6 at 11:59pm
Total points: 100


When you hand in your results from this homework, you should submit the following:

  1. The source code of your programs, in text files, named as specified below in Problems 3–6.
  2. An optional text file called README that contains any notes about your programs that you want the graders to know.
  3. A single .pdf file with your writeup of the results for all homework problems. Only pdf will be accepted, and you should only submit one pdf file, with the name username.pdf, where username is your username on cse. We will not accept any hard copy submissions of your homework!
  4. Every problem requres a writeup in this homework. For Problems 3–6, you need to decompose each problem just like we requested in previous homeworks. For an example of how to decompose a problem, see Question 5 on Homework 2, where we give you the problem decomposition. This is what is expected for the decompositions for this homework.

Put your name in all your files, and submit everything by the due date and time using the web-based handin program.

On this homework, you must work on your own and submit your own results written in your own words.


  1. (10 pts) For each of the following, write an equivalent expression using a compound assignment operator.
    1. x = x – 3;
    2. z = s * y + z;
    3. m = m * (z + 3);
    4. x = x – (a + b – c);
    5. total = 5 * total;

  2. (10 pts)
    1. Write a for loop to compute the following: given two integers, x and y, compute the sum of all integers between x and y, not including x and y (for example, if x = 5 and y = 9, the result would be 21). The result should be stored in an integer variable called sum. If x and y are such that there are no integers between them, then sum should contain zero.
    2. Reimplement your for loop from Part (A) as a while loop.

  3. (15 pts) Write a program called multiplication.c that asks the user to enter a positive integer. If the input is invalid, keep asking for valid input until the user enters a positive integer. Once the user has input a positive integer n, output a multiplication table for values up to n. For example, if n = 4, your output should look something like this:
       1  2  3  4
    1  1  2  3  4
    2  2  4  6  8
    3  3  6  9 12
    4  4  8 12 16
    
    Be sure to format your output so that the columns line up nicely. (For formatting purposes, you may assume that n < 1000.) In your program, you need to implement a function that both prompts the user to enter n and reads in n. You also need to implement a function that prints the table.

  4. (20 pts) Write a program called population.c that determines how long it will take a town's population to reach a certain size. Your program will ask the user for two values: a starting population and an ending population. Assuming that the population increases by 10 percent each year, your program should use a loop to determine how many years it will take for the population to surpass the specified ending population. Output this result to the user. Your program should have a function that prompts the user for input and a function that computes the number of years.

  5. (20 pts) The ancient Greek mathematician Euclid developed a method for finding the greatest common divisor of two integers, a and b. His method is as follows:
    1. If the remainder of a/b is 0 then b is the greatest common divisor.
    2. If it is not 0, then find the remainder r of a/b and assign b to a and the remainder r to b.
    3. Return to step (A) and repeat the process.
    Write a program called gcd.c that takes a and b as inputs from the user and uses a function to perform Euclid's procedure. After the function returns the greatest common divisor, in main, display a and b and the greatest common divisor.

  6. (25 pts) Write a program called amortization.c to create a table containing a customized loan amortization table. Your program will prompt the user to enter the amount borrowed (the principal), the annual interest rate (format: 5.05 is 5.05%), and the number of monthly payments (n). To calculate the monthly payment, use the formula given in Programming Project 1 in Chapter 3 of your text. This payment must be rounded to the nearest cent. After the payment has been rounded to the nearest cent, the program will create a table that resembles the following.
    Principal: $1000.00
    Annual Interest Rate: 9.00%
    Term: 6 months
    Monthly Payment: $171.07
     Payment    Interest    Principal     Balance
           1       $7.50      $163.57     $836.43
           2       $6.27      $164.80     $671.63
           3       $5.04      $166.03     $505.60
           4       $3.79      $167.28     $338.32
           5       $2.54      $168.53     $169.79
           6       $1.27      $169.79       $0.00
    Final Payment: $171.06
    
    Each month, part of the payment is applied to the interest and the rest is applied to the principal. Because the payment and each month's interest are rounded, the final payment will be a bit different and must be calculated as the sum of the final interest payment and the final principal balance. In your program, write a function to prompt the user for the input and a function to print the table.