This homework will help you:
- review some basic C++ things that you should
remember from CS102
- give you practice with scanf and printf, and
- give you practice with organizing and compiling a program that
consists of multiple files.
If you find it too easy, excellent.
If you find it hard, start brushing up on your C++!
Here are some things to keep in mind:
- In all of the following problems, use scanf to read from stdin
and printf to print to stdout.
- Print out listings for each of the program files and label them with
their file name (e.g., 1ton.c). Hand in the listings. For the last
problem you will also need to answer the questions.
- Write a program that prints out the numbers from one
to 10. Each number should be printed on a separate line and each
number should be printed in a field three chracters wide.
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 in the same manner that you printed
them in the previous problem. 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.
Hint:
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.
- Name this program twowords.c.
- You may assume that no word is greater than 200 characters.
- Print the strings in 20 character wide fields and left justify them.
- 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, one per line. 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.
- Print the result in a field that is 6 characters
wide and has two decimal digits.
- Note, to convert the string s to a double d, do
sscanf(s, "%lf", &d).
- Copy the files print_sum.c and
sum_it.c from ~bvz/cs140/hw to your homework
directory.
- Write a command to compile and link
the two files into an executable called
print_sum. Use gcc. You will not do separate
compilation so you will only need a single line for your command.
- Show how you would separately compile the two files and then
link them together into an executable named print_sum
(hint: you will need three commands).
- Try compiling each of the two files. You should get one error
for each file. What are each of the errors?
- What are the global variables in print_sum.c?
- Write an include file called sum.h that declares any
global variables and external functions for print_sum.
- Add sum.h to each of your two files and add any other
system include files that are required to get rid of the errors you
saw when you tried to compile the two files. When you finish
you should be able to execute print_sum. Print out the
two files and hand in the listings.