UP | HOME

HW17
COP-3223H

Setup

See HW7's Setup for how to prepare for performing and submitting your homework.

Homework file names

  • hw17-1.c
  • hw17-2.c

Questions

  1. (hw17-1.c) Hand-copy yourself without copy-and-paste the following program into a file on eustis and compile and run it.

    #include <stdio.h>
    #include <unistd.h>
    #include <assert.h>
    
    int main(int argc, char **argv) {
      int width = 40;
      int height = 20;
      int x = 0;
      int y = 0;
      int dx = 1;
      int dy = 1;
      while (1) {
        printf("\033[2J");
        printf("\033[H");
        printf("\033[%d;%dH", y, x);
        switch ((x * y) % 7) {
        case 0:
          putchar('.');
          break;
        case 1:
          putchar('*');
          break;
        case 2:
          putchar('%');
          break;
        case 3:
          putchar('#');
          break;
        case 4:
          putchar('@');
          break;
        case 5:
          putchar('-');
          break;
        case 6:
          putchar('+');
          break;
        default:
          assert(0);
          break;
        }
        putchar('\n');
        if ((0 <= (x + dx)) && ((x + dx) < width)) {
          dx = dx;
        } else {
          dx = dx * -1;
        }
        if ((0 <= (y + dy)) && ((y + dy) < height)) {
          dy = dy;
        } else {
          dy = dy * -1;
        }
        printf("\033[%d;%dH", height + 1, 0);
        printf("y: %d\n", y);
        printf("x: %d\n", x);
        x = x + dx;
        y = y + dy;
        usleep(50 * 1000);
      }
    }
    

    Describe in a comment what the effect of the switch statement is in this program.

  2. (hw17-2.c) Write a program, using putchar's to write characters and newlines '\n' to print the word "computer" in a vertical "line" of characters, using a while loop and a switch statement. Loop over the values of x for 0 to 8 (exclusive), and use a switch statement that tests the value of x. Use putchar to print each character of "computer" and a newline using a switch statement according to the following table:

    Case Output
    0 'c'
    1 'o'
    2 'm'
    3 'p'
    4 'u'
    5 't'
    6 'e'
    7 'r'

    The output should look like this:

    c
    o
    m
    p
    u
    t
    e
    r
    

Submission

See HW7's Submission instructions for adding, committing, and pushing your homework to the assignments' repo.

Self-check

See HW7's Self-check for instructions on checking what you have submitted to the remote grading repository.

Author: Paul Gazzillo

Created: 2026-02-23 Mon 06:13

Validate