Lab 03
Lab 03
Installing
Copy the following files from the directory /course/cs900/day03: isbn.c, calculator.c and adder.c.
You can do the following:
cd
mkdir day03
cd day03
cp /course/cs900/day03/*.c .
Where d1 through d10 are the 10 digits of the ISBN. (In reality, this is a bit more complicated, since
the last digit may also be a ‘X”, but we simplify this here for our purposes.) Your program should
take in a 10- digit ISBN and determine whether or not the number is valid. Two sample runs are
shown below (user input is in italics):
Example 1:
Please enter an ISBN number (no dashes or spaces):
> 0393969452
VALID
Example 2:
Hint: You can use a generalization of the following placeholder semantics for scanf to read in a
single digit (into an int variable dig):
scanf(“%1d”,&dig);
Compile using gcc -Wall -o isbn isbn.c. and run by typing isbn. Remember to fill in
the header and to properly comment your program.
Extension
Extend the program in the following way. The user enters a 10 digit number and specifies a
position where the digit has been corrupted. Correct the corrupted digit so that the resulting number
is a valid ISBN number and output the number.
Example 1:
Enter a 1 for +
2 for -
3 for *
4 for /
Entry: 1
Example 2:
Enter first number: 1
Enter second number: 3
Enter a 1 for +
2 for -
3 for *
4 for /
Entry: 5
Hint: You could use if/else statements to do the decision making for this program. However, you
will have a cleaner program if you use a switch statement.
Implement your program in the calc.c file. To compile, use gcc -Wall -o calc calc.c,
and to run, type calc. Be sure to fill in the header and to properly comment your program.
Extension
Finally read two 4 bit numbers from the keyboard and compute their sum using the implemented
functions. A user interaction might look like this
Extension
Read the lecture slides about the NAND function. NAND gates can be used to implement AND,
OR, and NOT gates. Implement a function NAND in C
int nand (int A, int B);