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.
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
Computing an Amortization Table
Write a program to calculate (and display) a loan amortization table.[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
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.
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 fileThe quick brown fox 17 22 jumps over ^&* the lazy dogthen 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.txtThis 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.add2.c, amort.c, and counter.c)
/home/gtowell/bin/submit -c 246 -p 1 -d a1This will package up everything in the directory a1 and submit it under your login name along with a timestamp of your submission date.