UP | HOME

HW18
COP-3223H

Setup

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

Homework file names

  • hw18-1.c
  • hw18-2.c

Questions

  1. (hw18-1.c) Replace the while loops (except while (1)) in the following bounce animation with for loops using the idiom(s) shown in class.

    #include <stdio.h>
    #include <unistd.h>
    
    int main(int argc, char **argv) {
      int width = 40;
      int height = 20;
      int x = 1;
      int y = 1;
      int dx = 1;
      int dy = 1;
      while (1) {
        printf("\033[2J");
        printf("\033[H");
        int j = 1;
        while(j <= height) {
          int i = 1;
          while (i <= width) {
            if ((i == x) && (j == y)) {
              putchar('o');
            } else {
              putchar(' ');
            }
            i++;
          }
          putchar('|');
          putchar('\n');
          j++;
        }
        for (int i = 0; i < width; i++) {
          putchar('^');
        }
        putchar('\n');
        if ((1 <= (x + dx)) && ((x + dx) <= width)) {
          dx = dx;
        } else {
          dx = dx * -1;
        }
        if ((1 <= (y + dy)) && ((y + dy) <= height)) {
          dy = dy;
        } else {
          dy = dy * -1;
        }
        x = x + dx;
        y = y + dy;
        usleep(50 * 1000);
      }
      return 0;
    }
    
  2. (hw18-2.c) Write a program that uses a for loop to print the numbers from 1 to 100 (inclusive of both) that detects which numbers are multiples of 4 and which are multiples of 7. Whenever a number is a multiple of 4, print "fizz\n" next to the number and whenever it is a multiple of 7, print "buzz\n" next to the number. There should only be one line per number. Use only a for loop and the constructs shown in class so far.

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-27 Fri 13:20

Validate