Homework 1 (Revised)

Due: October 11, 2019 (Note extension)
Points: 100


UNIX/Linux Questions

Please submit the answers to these two questions in a text or PDF file.

  1. (10 points) What do the commands man(1) and apropos(1) do?
  2. (10 points) How do I print a text file with each line preceded by the line number, without changing the file?

C Programming

  1. (60 points) (Revised) You are to write three functions that print numbers in sequence. The sequence may be increasing or decreasing. The first function, do_for, will use a for loop; the second, do_while, will use a while loop, and the third, do_do, will use a do … while loop.

    Each function takes 2 arguments; call them m and n. m is the starting number; the n is the ending number.
    If m < n, then print m, m+1, …, n, each number on a separate line.
    If m = n, then just print m.
    If m > n, then print m, m-1, …, n, each number on a separate line.

    The file hw1_3.c is available on the class web site. It contains three functions with the above interface. Write each loop in the appropriate function. Remember to add comments!

    To turn in: The file hw1_3.c with the three loops added. You need not submit a Makefile for this assignment.

    Revision: The previous version had you write 3 functions. This version supplies the function framework; now you need only add the loops where indicated.

C Program Debugging

  1. (20 points) On the class web page is the source for a C program named vis.c. It is supposed to read characters from the standard input and print them on the standard output after expanding any non-printing characters to their C character escape sequence.

    The relevant characters, and the C escape sequences to be printed when those characters are encountered, are:

    characterprint ascharacterprint as
    newline\nbackslash\\
    horizontal tab\tvertical tab\v
    backspace\bcarriage return\r
    form feed\fbell\a
    NUL\0anything else\ooo

    The “anything else” entry means that any non-printing character other than the ones named in the table is to be printed as a sequence of three octal digits preceded by a backslash. When the escape sequence for a newline is printed, the program is to skip to the next line.

    Unfortunately, the program as saved in vis.c will not even compile, let alone run. And the programmer thoughtlessly left off all the comments. Hence, your mission: comment the program, and fix it so it works as described above! You are to turn in a corrected source program, with comments describing the changes you made to get it to work.

    To turn in: Turn in a corrected, fully commented vis.c. Your comments are to explicitly say what you changed to make the program compile and work correctly. You need not submit a Makefile for this assignment.

Extra Credit

  1. (20 points) Redo problem 1, with each function having a third argument. This argument is to be the increment or decrement. It must be positive. So, for example, if the function do_for is called as do_for(3, 10, 2), then it would print “3, 5, 7, 9” (without the quotes), and if it is called as do_for(10, 3, 2), then it would print $10, 8, 6, 4”. If it is called as do_for(3, 10, -2), the function would print Error: invalid increment -2.

    The file hw1_ex.c is available on the class web site. Like hw1_3.c, it contains three functions with the above interface. The third parameter, d, is the increment. Write each loop in the appropriate function. Remember to comment them!

    To turn in: The file hw1_ex.c with the three loops added. You need not submit a Makefile for this assignment.

    Revision: The previous version had you write 3 functions. This version supplies the function framework; now you need only add the loops where indicated.


UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: [email protected]
ECS 36A, Programming and Problem Solving
Version of September 30, 2019 at 3:23PM

You can also obtain a PDF version of this.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh