Homework 2 Revision 1

Revision 1, April 30, 2025: A typographical error in the output of the program in problem 2 was corrected. The statement in the problem (above the sample run) is correct.

Due: April 30, 2025
Points: 100

All programs are to be submitted through Gradescope. Gradescope will compile and execute your program, sometimes with varying inputs or invocations. You can see your score once executions are done. You can submit your program multiple times, up to the deadline — at that point, the score you have will usually be the grade for that problem. I say “usually” because we reserve the right to look at your submission and deduct points if you do not use proper programming style. You have to name your program as given in the problem. Otherwise you will get an odd error message indicating there is a problem with Gradescope. Also, your output must match Gradescope’s exactly, including blanks and tabs — so pay attention to the example output!
  1. (25 points Write a program that prints a tic-tac-toe board. Each square is to be 7 × 7, with “#” marks. The “X” is to be 5 × 5, composed of “X”s, and placed in a square the coordinates of which are given as input.
    Your program first prompts for, and reads, two integers separated by blanks or tabs between 1 and 3 inclusive; if either is not, print an error message on the standard error giving both numbers, stating it is not a valid square, and prompting for two numbers again. If you encounter an end-of-file, end the program. For example (the user types what is in red and what the computer outputs is in black):
    
    > 2 4
    2,4 is not a valid square; the numbers must be between 1 and 3 inclusive
    > 2,4
    Illegal character in input ","
    >
    
    The upper left square has co-ordinates (1, 1) and the lower right corner has coordinates (3,3). If there are no errors, your program’s exit status code is to be 0; if there is an error, your program’s exit status code is to be 1.

    Here is an example input and output:

    
    > 1 1
           #       #
     X   X #       #
      X X  #       #
       X   #       #
      X X  #       #
     X   X #       #
           #       #
    #######################
           #       #
           #       #
           #       #
           #       #
           #       #
           #       #
           #       #
    #######################
           #       #
           #       #
           #       #
           #       #
           #       #
           #       #
           #       #
    
    Call your program “tac.c”.

  2. (25 points) Define the function:
    Collatz function
    The Collatz conjecture says that, if you iterate this sequence for any initial value of n, then eventually the sequence will reach the number 1.

    For a given number n, let k be the least number of iterations needed to reach the number 1 (excluding the initial value). Then k is called the total stopping time of n. For example, if n = 29, then the sequence is:

    29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
    and so the total stopping time of 29 is 18.

    Write a program that takes as input a positive integer and prints both the sequence and the total stopping time for that integer. Prompt the user with “> ” (note the trailing space and no newline). The output should look like (user input is in red):

    
    > 29
    29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
    The total stopping time for 29 is 18
    
    If the input is not an integer, print the following error message on the standard error:
    input: not a valid integer.
    replacing input with the input. If the input is an integer but is not positive, print the following error message on the standard output:
    input: input must be a positive integer.
    replacing input with the input.

    Call your program “collatz.c”.

  3. (30 points) A string is said to be reverse alphabetic if the letters in it, regardless of case, are in the reverse of dictionary order. So, for example, “miff” and “pooka” are reverse alphabetic, and “willow” and “computer” are not. In the following, assume the arguments contain only alphabetic characters.

    1. Write a program that uses a recursive function to determine whether a single command- line argument is reverse alphabetic. The program prints
      argument: True
      if its argument is reverse alphabetic, and
      argument: False
      otherwise, where argument is the given command-line argument. For this version, assume the argument is either all lower case or all upper case alphabetic characters. Call your program “abcde1.c”.
    2. Change your program so that the letters can be any case, not all upper or lower case. Again, you can assume all characters in the argument are alphabetic. Call your program “abcde2.c”.
    3. Finally, modify your program to take any number of arguments. Each argument must produce one line of the type above. Call your program “abcde3.c”.

  4. (20 points) On the Canvas assignment 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:
    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.

    Call your fixed program “show.c”.