CMSC 246 Systems Programming
Spring 2021

Assignment #1
Due Wednesday, Feb 24 before 11:59 PM

Create a directory Assignment1 in your cs246 directory. This is where you will put all of the files for this assignment.

  1. Adding two numbers

    Write a program in file add2.c that asks the user for two numbers (just use int variables) and then prints their sum. A session trace for this program might be:

            [gtowell@powerpuff HW1]$ gcc -o add add.c
            [gtowell@powerpuff HW1]$ add
            Enter an integer: 4
            Enter another integer: 5
            4 + 5 = 9
            [gtowell@powerpuff HW1]$ add
            Enter an integer: 44
            Enter another integer: -2
            44 + -2 = 42        
  2. Computing an Amortization Table

    Write a program to calculate (and display) a loan amortization table.
    The program should ask users for 3 floats: the loan amount, the annual interest rate, and the monthly payment. Assume that payments are made on the last day of the month, interest accrues monthly, every month has exactly the same number of days and that a year has exactly 12 months.
    For example, a trace of your program might look like:

    [gtowell@powerpuff HW1]$ gcc amort.c
    [gtowell@powerpuff HW1]$ a.out
    Enter the loan amount: 100
    Enter the interest rate: 10
    Enter the payment: 30
    Period Balance Interst   Paymt
         1  100.00    0.83   30.00
         2   70.83    0.59   30.00
         3   41.42    0.34   30.00
         4   11.76    0.09   11.85
         5    0.00    0.00    0.00

    The interest accrued in period N is equal to the balance in period N-1 * annual interest rate / 12. So, in the above table, the interest in period 1 equals 100 * (10/100) / 12 = 0.83.
    Note that the payment in the last period may (will probably) be less than the payment in the preceding periods.

    Your program should check user inputs for validity (e.g., the loan amount should not be negative).
    You program should never enter an infinite loop.
    Your program should accrue interest in whole pennies, no fractional cents. You can round or truncate.

    NB: When you use scanf, for example, scanf("%d",...), you will read only the number that the user types in, not the newline after the number. So if you read a character later on, you’ll read the newline by mistake. The solution is to put a space in the format string before the %c. As the first bullet on page 45 of the King book explains, this space character instructs scanf to skip any whitespace, including a newline.

  3. Reading from stdin

    The Unix utility program wc is handy; it counts the number of characters, words and lines in a file (or from stdin). You will write a variation of wc that counts the number of upper or lower case characters and lines it receives only through stdin. That is, unlike wc, your program will ignore all non alphabetic characters other than newline. Suppose that you have a file
        The quick brown fox 17 22
        jumps over ^&*
        the    lazy
    then output of your program should be
    35 4
    (By contrast, the output of wc on this same file is "3 12 56". 3 is the number of lines; wc counts only newline characters so it does not count the final line unless that line has a newline character. 56 is the character count, it includes everything.)

    Further suppose that the above file is named sample.txt and the compiled version of your program is named counter. Then either of the following two commands should produce the same, "35 4" result
        cat sample.txt | counter
        counter < sample.txt
    This program should

Scripting in Linux
Once done, you will need to show some sample runs of the three programs. In order to record the runs, you can use the Linux command script.
Go to your Assignment1 directory and enter the command: script FILENAME where FILENAME is some obvious name. For instance: add2_script1

You will get your command prompt back. Next, run each of the above programs as you normally would. Once done, enter the command: exit

This will end your script session. If you now look a the contents of the session file, you will see that the entire interaction has been recorded in it (as a script!).

What to hand in:

All submission will be done using the subit script. Before submitting collect all of the following in a single directory. There shoudl be NOTHING else in the directory.

Do not submit compiled code Be sure to delete any compiled code before following the directions below

When you have everything ready in this directory go to the directory containing the one with the information you want to submit. For instance, suppose that the 10(ish) files specified above are in /home/YOU/cs246/a1. Then you should go to /home/YOU/cs246.
Use the submit program as follows:

  /home/gtowell/bin/submit -c 246 -p 1 -d a1
This will package up everything in the directory a1 and submit it under your login name along with a timestamp of your submission date.