CS1003/1004 Homework #1
Due by Thursday, February 12,
at 5:00pm
There are two parts to this homework assignment: a theory
portion, worth 10 points, and a programming portion,
worth 15 points. Please be sure to review the submission instructions in
advance.
Theory questions
- (2 points) Name one advantage and one disadvantage of assembly
languages.
- (4 points) You're given the following code snippet:
int i = 5;
int j = 1;
while(i > 0) {
j = j * i;
i = i - 1;
}
Explain, in one or two sentences of English, what mathematical
operation is being done to the initial value of i to produce
the final result of j (i.e., j = {some operation on i}).
State the final results of i and j after the while loop
finishes.
- (4 points) You're given the following if clause:
if(i || (j && !k))
- (2 points) Draw the logic diagram for this boolean condition, similar to the examples in
Brookshear 1.1, but with three inputs and one output.
- (2 points) Assume i is false (zero), j is
true (one) and k is true (one). Will this if clause
execute, and (one-two sentences max) why or why not?
- (0 points; this subproblem cancelled)
You're given the following number in binary form:
11011011.
- (1 point) Assuming you're not using two's complement,
give the decimal (base 10) form of this number. Use standard
binary-to-decimal conversion.
- (2 points) Assuming you are using two's complement,
give the decimal form of this number.
- (2 points) What's the positive equivalent in two's
complement (i.e., if we were to multiply the decimal
equivalent by -1)?
Programming assignment
Write a program, Calculator.java
(for Java students)
or calculator.c
(for C students), that takes two
non-zero integer command line parameters, a and b, performs the
following calculations, and prints out the result of each
operation. The operations which you need to implement
are:
- a+b (addition)
- a*b (multiplication)
- a/b (division)
- a mod b (modulus)
- ((a-1)*(b-1))+(b+1)+(b+2)
Please return the results in the above order and clearly label
each result. See below for a sample output.
Hints:
-
To compile your program, you will type
javac
Calculator.java
(for Java students) or gcc
calculator.c
(for C students) at your UNIX prompt.
- To run your program, you will type
java Calculator a
b
(for Java students) or ./a.out a b
(for C
students) at your UNIX prompt, where a
and
b
are the numbers you actually want calculated (i.e.,
don't actually type "a" and "b" there).
- In order to access the "command-line" arguments, you need to
use argv[1] and argv[2] in C, and args[0] and args[1] in Java
(within the main method).
- The labs will discuss what needs to be done in detail. If
you miss the labs, please be sure to catch up with the
instructor or the TAs.
- Don't write your entire program at once! Start simply;
for example, just get multiply working, and for hardcoded
numbers at that -- and then add each of the additional
functionalities if you get the simple ones working first.
- You may assume that invalid input will not be given; that is,
exactly two non-zero command-line parameters will be supplied.
Sample Output for java Calculator 2 4
and
./a.out 2 4
:
The sum of 2 and 4 is 6
The product of 2 and 4 is 8
2 divided by 4 is .5
2 % 4 is 2
I computed (((2-1)*(4-1))+(4+1)+(4+2) to equal 14