Sie sind auf Seite 1von 3

pppCSPP50101

Homework 2
Due Monday August 5, 2002
There are ten problems. You have two weeks to complete as many as you
can. They range from very simple to challenging, increasing (roughly)
with difficulty from 1 to 10. Do each problem in a separate file,
named according to our conventions: p2_1.c, p2_2.c, ... p2_10.c. None
of these should require any techniques that we haven't studied in the
first two weeks. However, you are welcome to read ahead and use
anything that we haven't studied if you feel it makes the program
easier. I will be adding hints/tips/techniques throughout the course
of the two weeks. Please consult the course web page.
1. Write a program which computes the modulus of two ints entered
at the command line.
ex.
prompt > mod 6 4
The answer is: 2
(program ends)

#this means "6 mod 4"

references: see add_to_numbers.c

2. Write a program which computes n factorial, where n is an arbitrary


integer. Get input from argv.
ex.
prompt > factorial 3
The answer is: 6
(program ends)
references: see add_n_numbers.c

3. Write a program that finds and prints the largest integer given
an arbitrary number of inputs (using argc/argv).
prompt > max 1 2 3 1 2 5 1 2
5
(program ends)
references: see ex1.c, add_n_numbers.c

4. There is a stdlib function sscanf(...) which behaves exactly as scanf,


except that instead of reading from stdin, it reads from a variable
of type char*. The precise calling sequence is similar to scanf
sscanf(string, fmt, &var1, &var2 ...)
where string is the char* variable that is read from according to
the format statement fmt with the results converted and stored in
var1, var2, etc.
Use sscanf to write a program that converts an arbitary input

string to an integer (just like atoi). Don't worry about error


handling.

5. Given a simple text file as input, write a program that


uses unix redirection to reformat the textfile in the following
way: every period is followed by exactly two spaces
and then a capitalized letter.
hint: simpler than it appears: use getchar/putchar
use toupper in ctype.h to uppercase char.

6. Write a program that runs continuously and allows the user to add
subtract, multiply, divide, and take the modulus of two integers, or
take the factorial of a single number. The program must parse
standard operators for these operations:
ex.
prompt > ./adding_machine
$ 3 + 2
5
$ 3!
6
$ 3 % 2
1
$ edaf
input error
$ end
(program ends)
additional details:
* Your program should allow zero or more white spaces. So,
3+ 2, 3 + 2, 3!, 3 !, etc., should all work as well.
* You only need to handle one operation at a time. So, I DO NOT
expect you to parse, 3! + 2!, 3 + 2/2, etc.
7. Write a program which returns only the unique values of an input
list of integers.
prompt > uniq 1 2 -1 1 3 3
1 2 -1 3
(program ends)

8. Write a program that computes the number of days between any two
dates (inclusive). You must take into account leap years. The rule
for computing a leap year is as follows:
If the year is divisible by 4 it is a Leap Year ...
Unless the year is divisible by 100, then it is _not_ a Leap Year ...
Unless the year is divisible by 400, then it _is_ a Leap Year.
During a Leap year, an extra day is added at the end of the month
of Feb.

ex. 1000 is not a Leap Year


2000 is a Leap year
Your program must be run as follows:
PROMPT> count_days 1/1/2000 12/31/2000
366 days
(program ends)
Error checking. You must handle the following cases only:
- if there is too few or too many input parameters
- if the date strings are not in the proper format
(simply echo usage statement);
- if date 1 is not earlier than date 2

9. Starting with our pick_student program from class, add the


following capabilities:
a. remove the need to input size of list to program
b. design some way to have program keep track
of selected students and reduce the probability
of them being chosen again (by 30%, say)
each time they are chosen.

10. Edit our class example tic-tac-toe application to play one


person versus the computer. The computer must make intelligent
moves. You should let it play either X or O.

Das könnte Ihnen auch gefallen