This homework will help you:

  1. review some basic C++ things that you should remember from CS102
  2. give you practice with scanf and printf, and
  3. 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:

  1. In all of the following problems, use scanf to read from stdin and printf to print to stdout.

  2. 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.

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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).

  9. Do the same thing, only now assume that the number can be a floating point value. Use a double instead of a float.

  10. Copy the files print_sum.c and sum_it.c from ~bvz/cs140/hw to your homework directory.

    1. 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.

    2. 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).

    3. Try compiling each of the two files. You should get one error for each file. What are each of the errors?

    4. What are the global variables in print_sum.c?

    5. Write an include file called sum.h that declares any global variables and external functions for print_sum.

    6. 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.