CMSC 223 Systems Programming
Fall 2023
Assignment#1
Due in Class on Monday, September 20, 2023
Create a directory Assignment1 in your cs223 directory. This is where you will create all your programs from 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. Here is an example session, with user-entered numbers in boldface:
Enter a number:
4
Enter a number:
7
4 + 7 = 11
This program can be compiled with gcc -o add2 add2.c
and run with ./add2
Adding lots of numbers
Write a program in file adds.c
to write a program that adds many numbers together. It continues accepting numbers until the user enters a 0. (Who would need to add a 0, anyway?) It then prints the sum of the numbers. It does not need to repeat back the numbers entered. (That would require an array, and we haven’t learned about them yet.)
Here is an example session:
Enter a number:
4
Enter a number:
7
Enter a number:
1
Enter a number:
18
Enter a number:
0
Sum: 30
A calculator
Write a program in file calc.c
to write a very simple calculator program. It should ask the user for an operation, which is +
, -
, *
, or /
. After asking for the operation, the program then asks for 2 numbers. The program then prints out the operation applied to the two numbers. It loops until the user chooses 0 as the operation.
You should read in the command as a char
. This will make your life simpler.
Here is an example session:
Enter an operation:
-
Enter a number:
6
Enter a number:
2
6 - 2 = 4
Enter an operation:
*
Enter a number:
4
Enter a number:
9
4 * 9 = 36
Enter an operation:
/
Enter a number:
10
Enter a number:
4
10 / 4 = 2
Enter an operation:
0
Good-bye.
Note that your program should do integer division. If you store your numbers as int
, this should be the default behavior. (That is, integer division is the natural, easy interpretation of this task.)
Your program should act appropriately (issue a sensible error and continue running) if the user types in the character for an operation that does not exist (for example, '#'
) or tries to divide by 0. You are not expected to be able to handle when the user types a word instead of a number, however.
NB: When you use 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 (using %c), 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.
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 session
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:
add2.c, adds.c, and calc.c)