Homework 1

Due: April 24, 2023
Points: 100


Gradescope

  1. (10 points) The goal of this problem is to have you use Gradescope. Please write a program that prints the line “Hello, world!” followed by a newline.

    Call your program “hello.c”.

C Programming

  1. (60 points) You are to write three loops that print numbers in sequence. The sequence may be increasing or decreasing. The file loopy.c contains three functions. 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. Write each loop within its respective function.

    Each function has 2 arguments; call the first m and the second n. m is the starting number; 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 loopy.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!

    Call your program “loopy.c”

C Program Debugging

  1. (30 points) On the class web page is the source for a C program named show.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:

    character  print as  characterprint as
    newline\n backslash\\
    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 show.c will not even compile, let alone run. And the programmer thoughtlessly left off all the comments. Hence, your mission: fix the program 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.

    Call your fixed program “show.c”.


UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: mabishop@ucdavis.edu
ECS 36A, Programming & Problem Solving
Version of April 12, 2023 at 10:07AM

You can also obtain a PDF version of this.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh