Midterm 2, Fall 2004, CISC105, Post Exam Notes

Here are some notes I wrote while grading the exams.

This is not a complete answer key or rubric. For the questions that nearly every student got correct, I have not supplied answers here. Rather, I have supplied correct answers and comments on some incorrect answers only in cases where I think some additional comment might be helpful to the class' learning, or to CISC105 instructors in making up future exams on this material.

Questions 1-23

These were the multiple choice questions. Here are the correct answers, along with "common" incorrect answers. (For questions where there are no incorrect answers given, most students got the question correct, and none of the alternative answers was a "favorite".)

Question Correct Answer Common Incorrect Answers
1 D  
2 B D
3 C  
4 B  
5 A  
6 A  
7 D C,E
8 D  
9 A  
10 E A
11 A  
12 E D
13 B A
14 A B
15 C  
16 A  
17 D B,C
18 C D
19 B A,D
20 C E
21 A  
22 D B

Here are some additional comments on the questions where students had some difficulty:

  1. (2 pts) Which of the following are the actual parameters of inc2?

    (a) 3 and 4 (b) &a and b (c) r and s  
    (d) *r and s (e) none of the above  

    The answer is B because that is what appears in the function call. Answers C and D are incorrect, because r and s are the name sof the formal parameters, not the actual parameters.

  2. (2 pts) What is the output?

    /* Q6.c */
    
    #include <stdio.h>
    int main(void)
    {
      int a[6] = {13, 15, 17, 19, 20, 21};  
      printf("%d",a[6]);
      return 0;
    }
    
    (a) 0 (b) 19 (c) 20 (d) 21 (e) error

    The answer is E, "error", because the output cannot be predicted. The size of the array is [6], so therefore the indices of the array are [0] through [5]. Indexing into item [6] goes beyond the bounds of the array, and it is not possible to predict what will print. The value zero (answer A) is a very typical value that might be printed, but any other integer value is also possible, so A is not a correct answer.

 

Question 23

(20 pts) Write a complete C program that behaves as follows:

  1. prompts a user for ten integers
  2. uses scanf to get the integers and put the integers into an array
  3. prints out the array on a single line
  4. prints out the minimum value in the array

Sample script:

% a.out 
Please enter ten integers: 
9 3 2 4 5 6 7 3 8 6

array is: 9 3 2 4 5 6 7 3 8 6 
min is: 2

Answer: There are many possible correct answers to this problem. Here is just one of them:

/* q23.c

 Answer to Question 23, CISC105 Exam, Fall 2004 
 P. Conrad  

*/

#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 */
  
  min = a[0];
  for (i=1; i<10; i++)
    {
      if (a[i] < min)
        min = a[i];
    }

  printf("min is: %d\n",min);

  return 0;
}
    

Some remarks:

  1. Even though all the input is shown in the sample script a single line, you can (and should!) still use a for loop for reading in the values. For example, the following "works", but it is a poor approach to the problem. I didn't take a deduction for such solutions this time, but in the future I might:

      /* The following is a POOR way to do this, */
      /* although it does work.                  */
      /* A for loop is a better way, because it  */
      /* scales.                                 */
    
      scanf("%d %d %d %d %d %d %d %d %d %d",
            &a[0], &a[1], &a[2], &a[3], &a[4],
            &a[5], &a[6], &a[7], &a[8], &a[9]);
    
      /* Likewise, the following is a poor way to */
      /* print out the values; use a for loop instead! */
    
      printf("array is: %d %d %d %d %d %d %d %d %d %d \n",
            a[0], a[1], a[2], a[3], a[4],
            a[5], a[6], a[7], a[8], a[9] );
    
    	

  2. The following code to scan ten numbers into an array does NOT work.  This will read one integer into a location outside the array (the four bytes following array element a[9]).   To correctly read in the array, you need to scan each array element separately (e.g. in a loop, or in the tedious method listed above):

     scanf("%d", &a[10]);

  3. This doesn't work either, for a variety of reasons.  It's a creative solution, and it might work in some languge, but C is not one of them. First, you can't mix declarations and scanf statements; all declarations have to come first.  Second, you can't use variables in an array initializer.  Nice try, though.
    int a,b,c,d,e,f,g,h,i,j;
    scanf("%d %d %d %d %d %d %d %d %d %d", 
          &a,&b,&c,&d,&e,&f,&g,&h,&i,&j);
    int a[10]={a,b,c,d,e,f,g,h,i,j};
    		

  4. Likewise, the following code to print 10 numbers from an array does NOT work. This code only prints one integer, the one located at the 4 bytes following element array[9] (which happens to be outside the array!).   You need to scan each array element separately (e.g. in a loop, or in the tedious method listed above):

     printf("%d", a[10]);

 

Question 24

(20 pts) Write a complete program that reads 100 integers from a file named data.txt into an array.

All the integers will be in the range 0 - 10 inclusive.

Your program should include a function to find the maximum value in an array, given the array and the array size as parameters.

Have your main() call the function and print the result.

Answer: There are many possible correct answers to this problem. Here is just one of them, followed by some comments.

/* Q24.c  P. Conrad CISC105 Midterm 2 Fall 2004 */
/* Read 100 integers into array, find the max */

#include <stdio.h>
#include <stdlib.h>

int maxOfArray (int array[], int size);

int main(void)
{
  int nums[100];
  int i;
  int max;
  FILE *infile;

  infile = fopen("data.txt","r");

  if (infile==NULL)
    {
      printf("Error opening file\n");
      exit(-1);
    }
	  for (i=0; i<100; i++)
    fscanf(infile,"%d",&nums[i]);

  printf("Max value is %d\n", maxOfArray(nums, 100));

  return 0;
}

int maxOfArray (int array[], int size)
{
  int i;
  int max = array[0];
  for (i=1; i<size; i++)
    if (max < array[i])
      max = array[i];
  return max;
}  

    

A few remarks:

Question 25

  1. (10 pts) One of the reasons we teach Unix in CISC105 is because the CIS dept and the Electrical and Computer Engineering departments have asked us to.

    They want to be able to assume that their students know how to use Unix when they take later courses.

    So, assume that in a future semester, having already mastered CISC105, you go on to take CPEG202.

    Your goal is to make a report available at the following URL (assuming that userid is your own userid):

      http://~userid/cpeg202/reports/myReport.html
    

    Assume the following:

    Your assigment is to

    Give the entire sequence of Unix commands that you use to accomplish this task. Note that even though I've listed four steps above, you might NOT need exactly four commands.

    For each command spell out the entire command line (e.g. don't just say ``cp'', say ``cp foo bar''.) You don't have to give the editor commands, but you do need to show the command you use to ``startup'' the editor on your file.

First, there was a typo in the URL: it should have had the letters udel.edu/ in it, as follows:

  http://udel.edu/~userid/cpeg202/reports/myReport.html

However, I doubt this threw anyone off.

There are several possible answers to the question. Here is one:

 

> cd ~/public_html/cpeg202
> mkdir reports
> cd reports
> cp ~smith/public_html/cpeg202/sampleReport.html .
> mv sampleReport.html myReport.html
> emacs myReport.html
> cd ..
> chmod -R a+rx reports

The essential elements:

  1. If you assumed a starting point OTHER THAN your home directory, you lost points. To be absolutely clear, you should have started out with a "cd" command to put yourself in a particular directory.
  2. It was necessary for you to create a new directory called reports under your cpeg202 directory. I deliberately left this out of the instructions to see if you would pick up on it, but I gave a big hint, that is, the fact that ~/public_html/cpeg202 is an empty directory.
  3. You had to copy the file somehow. You needed to either cd into smith's cpeg202 directory and copy to your home directory (specifying the full pathname), or you needed to cd into your reports directory, and specify the full pathname of the source. Copying from sampleReport.html to myReport.html is NOT sufficient if the full pathname of either the source or target is not specified.
  4. You could use either vi or emacs to edit the file. It was ok to edit either before or after renaming.
  5. There is no such command as rn in Unix! The rename command is mv. The mv command is also used to "move" files.
  6. You needed to chmod on both the new directory AND the new file. Doing a chmod on one without the other resulted in losing points.

Phillip T Conrad