This homework will help you review some basic C things that you should
remember from CS102. If you find it too easy, excellent.
If you find it hard, start brushing up on your C!
- Write a program that prints out the numbers from one
to 10. This is a simple for loop. Name your program 1to10.c.
- Modify your program so that it reads in an integer from standard input
and prints out all the
numbers from one to that integer. Remember scanf()? This lets
you read an integer from standard input. Don't worry about bad input. Name
your program 1ton.c.
- You may notice that if you hit < CNTL-D > before typing an
integer to 1ton.c, or if you don't type in an integer,
it doesn't do what you think it should.
Write a program that works like 1ton.c, but exits immediately
if the user types < CNTL-D > or a non-number instead of a number.
if you don't know how to do this, read the man page on scanf()
and look at its return value. Call this program 1ton2.c.
- Now, tweak 1ton2.c so that it repeatedly prompts the user
for a number n, and prints out the numbers from 1 to that number,
until the user types < CNTL-D >. If the user types a non-number,
exit the program.
Name this program 1tonrepeat.c.
- Write a program that reads in two words from standard input, and
prints them in reverse order. This makes you use scanf()
to read strings. You may assume that no word is greater than 200
Name this program twowords.c.
Note that if s is a char array and you give it to scanf in order
to read in a string, then you pass s and not &s.
This is because s is an array.
- Write a program that reads in ten integers, and prints them out in
reverse order. This gives you practice reading into an array.
If the user types < CNTL-D > before entering all 10, or
enters a non-number, simply exit.
Name this program read10.c.
- Tweak read10.c so that if the user types < CNTL-D > before
entering all 10 integers, or
enters a non-number, then the program prints the numbers that it has
read in already in reverse order.
Name this program read10b.c.
- Write a program that prints out the sum of all arguments on the
command line. Assume that they are integers. If any argument is
not a number, assume that it is zero.
Name your program sumcl.
Hint 1: the command line is obtained with argc and argv.
Hint 2: to convert the string s to an integer,
use sscanf(s, "%d", &num).
- Do the same thing, only now assume that the number can be a floating
point value. Use a double instead of a float.
Name your program sumcld.c.
Note, to convert the string s to a double d, do
sscanf(s, "%lf", &d).