UP | HOME

HW19
COP-3223H

Submit your answers via webcourses

  1. Take the following program:

    #include <stdio.h>
    
    int main(int argc, char **argv) {
      int a[10];
      int b = 3;
    
      for (int i = 0; i < 12; i++) {
        a[i] = i + 2;
      }
    
      for (int i = 0; i < 12; i++) {
        a[i] = a[i] + b;
      }
    
      for (int i = 0; i < 12; i++) {
        printf("%d\n", a[i]);
      }
    }
    

    Using only your mind and/or pencil and paper (do not compile and run the program, what will be the output of running this program? How many times will printf be called in this program?

  2. Take the following program that is attempting to create an array.

    #include <stdio.h>
    
    int main(int argc, char **argv) {
      int a[8];
    
      for (int i = 1; i < 8; i++) {
        a[i] = 1;
      }
    
      for (int i = 0; i < 8; i++) {
        for (int j = 0; j < i; j++) {
          printf("a[%d] = %d\n", i, a[i]);
          a[i] = a[j - 1] + a[i];
          printf("a[%d] = %d\n", i, a[i]);
        }
      }
    
      for (int i = 0; i < 9; i++) {
        printf("%d\n", a[i]);
      }
    }
    

    Using only your mind and/or pencil and paper (do not compile and run the program, fix the bugs in this program, writing the corrected program as part of the answer to this question. Then write what the output of running this program will be.

  1. Using only your mind and/or pencil and paper (do not compile and run the program, write a program that computes the dot product of two arrays a and b. You can assume that the arrays are already given and that the array lengths are stored in alen and blen already. Just write the instructions that perform the dot product (no need for the complete C program), using for loops and the constructs seen in class so far. The dot product of two vectors of size n is taken by multiplying each corresponding element of the vectors, then summing those products, i.e,

    \[a \cdot b = \displaystyle \sum_{i = 1}^{n} a_i b_i\]

    What conditions must be true for the program to produce a correct dot product? Be sure to check these conditions and/or use an assert to enforce them.

Author: Paul Gazzillo

Created: 2026-02-27 Fri 09:27

Validate