CISC105 Final Exam Fall 2004, Sample Questions (1)

Here are several questions inspired by various incorrect answers to Question 23 on midterm 2.

  1. Here is some code:


    #include <stdio.h> int main(void) { int a[5]={3, 2, 5, 4, 8}; printf("Here is the array: "); printf("%d",a[5]); printf("\n"); return (0); }

    Two of these are actual output from a compile and run of this program on strauss. One of them is fake. Which one is the fake?

    a.
    > gcc q1_1.c
    > ./a.out
    Here is the array: -4196028
    >

    b.
    > cc q1_1.c
    > ./a.out
    Here is the array: 0
    >

    c.
    > cc q1_1.c
    > ./a.out
    Here is the array: 3 2 5 4 8
    >

  2. Here is an actual script of a program from strauss of an incorrect solution to Question 23 on midterm 2 (a program to read 10 integers into an array, print the array, and find the minimum element.)  

    Fix this program. You may make (at most) two changes. 

    Adding one line of code in the correct spot is enough to fix the program, but once you add that line of code, you'll probably want to change one other line of code as well to make things clean.

    > cat q23c.c
    /* q23.c
    
     Incorrect Answer to Question 23, CISC105 Exam 2, Fall 2004 
     P. Conrad  
    
    */
    
    #include <stdio.h>
    
    int main(void)
    {
      int i; /* loop counter */
      int a[10]; /* array with 10 elements */
      int min = a[0]; /* minimum element in array */
    
      /*  prompt user for ten integers */
    
      printf("Please enter ten integers: ");
    
      /* use scanf to get the integers and put the integers into an array */
      for (i=0; i<10; i++)
        scanf("%d",&a[i]);
    
      /* print out the array on a single line */
      printf("array is: ");
      for (i=0; i<10; i++)
        {
          printf("%d ",a[i]);
          if (a[i] < min)
            min = a[i];
        }
      printf("\nmin is: %d\n",min);
    
      return 0;
    }
    
    
    > gcc q23c.c -o q23c
    > ./q23c
    Please enter ten integers: 3 2 5 4 8 2 7 8 3 4
    array is: 3 2 5 4 8 2 7 8 3 4 
    min is: -12791248
    > 
    

  3. Question 23 required you to input 10 numbers (all integers between 0 and 10) into an array, print the numbers, and then print the minimum element.  Here is a correct answer to Question 23 from midterm 2, except that one line has been accidentally erased and replaced with @@@.   Which of the following should be inserted to correctly complete the program? 

    (a) min = a[0];
    (b) min = a[i];
    (c) min = 0;
    (d) min = 10;

    #include <stdio.h>
    
    int main(void)
    {
      int i; /* loop counter */
      int a[10]; /* array with 10 elements */
      int min; /* minimum element in array */
    
      /*  prompt user for ten integers */
    
      printf("Please enter ten integers: ");
    
      /* use scanf to get the integers and put the integers into an array */
      for (i=0; i<10; i++)
        {
          scanf("%d",&a[i]);
        }
    
      /* print out the array on a single line */
      printf("array is: ");
      for (i=0; i<10; i++)
        {
          printf("%d ",a[i]);
        }
      printf("\n");
    
      /* print the minimum value in the array */
      
      @@@@@ /* initialize min = ??? */
      
      for (i=1; i<10; i++)
        {
          if (a[i] < a[min])
            min = i;
        }
    
      printf("min is: %d\n",a[min]);
    
      return 0;
    }