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.
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:
| (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 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:
scanf to get the integers and put the integers into an
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:
/* 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] );
|
scanf("%d", &a[10]); |
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};
|
printf("%d", a[10]); |
(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:
printf("Max value is %d\n", maxOfArray(nums[],
100));
printf("Max value is %d\n", maxOfArray(nums, 100));
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:
~/public_html/cpeg202 in your
account. This directory contains a few files, but it currently contains no subdirectories
(hint: that is an important point.)
~smith/public_html/cpeg202/ contains the file sampleReport.html. Your assigment is to
sampleReport.html, myReport.html, 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:
rn in Unix! The rename command
is mv. The mv command is also used to "move"
files.