final exam

advertisement
final exam ver A (*** SOLUTION ***) V22.0380-001, sp00, Dan Barrish, 5/8/00
Be sure to put your name on EVERY page of the exam; sometimes the staples come
loose. Do not unstaple your exam! Read the instructions and exam questions carefully.
You do not need to include comments in the code that you write for this exam. But you
still need to indent properly, use good variable names (they don’t have to be terribly
long), write neatly, etc. Do not hand in the exam booklets, only the exam itself. Please
put all totally unused booklets on the table in front. When you have handed in your exam
to me personally, you are free to leave. Thank you for your participation in this course :)
Question ??? [0 points, sorry].
Write your name on each page.
soln: duh!
Question ??? [10 points]. Recall that a string in C is a null-terminated array of
characters. Consider the following declaration and initialization of a string, followed by
the user being prompted to enter the value for the string.
/* You can declare variables here (but finish reading the
question first! */
soln: (these two lines are part 1 of soln)
int i, count;
char ch;
char str[80];
printf("Enter a string: "); /*assume the string will fit in the array*/
gets(str); /* user enters string from keyboard */
Now, at this point, the string is of unknown length with unknown content. Your job is to
write a small, simple code fragment to count the number of lower-case letters in the
string (lower-case letters are 'a', 'b', ..., 'z'). At the end of your code fragment, use printf()
to report how many lower-case letters were present in the string. Be sure to declare any
necessary variables just above the char str[80] declaration above. Recall the name of the
string is str. Note: you may not use any functions from the C libraries!
soln: (this is the second and last part of the soln)
i = 0;
count = 0;
ch = str[0]; /* use of ch not really needed */
while(ch != '\0') {
if (ch >= 'a' && ch <= 'z')
count++; /* found a lowercase letter */
i++;
ch = str[i];
} /* while */
printf("Number of lowercase letters: %d\n", count);
Question ??? [10 points]. Short answer array stuff
?) The elements of an array are related by the fact(s) that they
_________________________________
soln: have the same name and type
?) The contents of a particular element of an array is called the ___________ of that
element.
soln: value
?) In a double-subscripted array, the first subscript (by convention) identifies the
_________ of an element, and the second subscript (by convention) identifies the
_________.
soln: row, column
?) (true/false) To refer to a particular location or element within an array, we specify the
name of the array and the value of the particular element.
soln: (false) we specify the subscript/index (not value) of the element.
?) (true/false) To indicate that 100 locations should be reserved for int array p, we write
the declaration:
int p[100];
soln: (true)
?) (true/false) A C program that totals the elements of a double-subscripted array must
use nested for loops.
soln: (false) Can just spell out the elements individually.
?) Given an int array c of 1000 elements, and int variable sum (sum has been declared but
not initialized), and int variable i (also declared but not initialized), write a short, simple
code fragment which will output (using printf() ) the sum all the elements of the array c.
You may not use any additional variables.
soln:
sum = 0;
for (i=0; i<1000; ++i)
sum += c[i];
printf("The sum is %d\n", sum);
?) Consider a 2-row by 5-column int array named t.
i. Write a declaration for t (without any initialization).
soln:
int t[2][5];
ii. Write the names of all the elements in the last column of t.
soln:
t[0][4] and t[1][4]
iii. Using nested for-loops, write a short, simple code fragment to input the values of the
elements of t from the terminal. You may assume that int variables r and c have been
declared for your use. You may not use any additional variables.
soln:
for(r=0; r<2; r++)
for(c=0; c<5; c++){
printf("Enter an integer: ");
scanf("%d", &t[r][c]);
}
Question ??? [5 points]. What does the following code fragment print?
int y, x = 1, total = 0;
while(x <= 3) {
y = x * x;
printf("%d\n", y);
total += y;
++x;
}
printf("Total is %d\n", total);
soln:
1
4
9
Total is 14
Question ??? [15 points]. Write a code fragment (not an entire program, not a function)
to do the following: Prompt the user to enter 5 ints from the keyboard. Then output the
largest and second largest of those 5 ints. To simplify matters, assume that the user will
enter only positive ints, and that no duplicate values will be entered. You must write a
loop to do this problem. If your solution is correct, it should be trivial to change the
code fragment to work with 5000 ints as easily as with 5. Be sure to declare all variables
that you use.
A sample interaction with this code fragment might look this this:
Enter
Enter
Enter
Enter
Enter
first number: 5
next number: 4
next number: 7
next number: 2
next number: 6
Largest is 7
Second largest is 6
soln:
int counter, number, largest, secondLargest = 0;
printf("Enter the 1st number: ");
scanf("%d", &largest);
counter = 2;
while (counter <= 5) {
printf("Enter %dth number: ", counter);
scanf("%d", &number);
if (number > largest) {
secondLargest = largest;
largest = number;
}
else if (number > secondLargest) {
secondLargest = number;
}
++counter;
} /* while */
printf("\nLargest is %d\n", largest);
printf("Second largest is %d\n", secondLargest);
Question ??? [5 points]. Given the equation y = ax3 + 7, which of the following, if any,
are correct C statements for this equation?
a) y
b) y
c) y
d) y
e) y
f) y
=
=
=
=
=
=
a * x * x * x + 7;
a * x * x * (x + 7);
(a * x) * x * (x + 7);
(a * x) * x * x + 7;
a * (x * x * x) + 7;
a * x * (x * x + 7);
soln: A, D, E
Question ??? [10 points].
Write a swap function. The prototype for this function is:
void swap(int *, int *);
When this function is called, say, from main(), the values of the actual parameters are
swapped. For example, in main(), you might see:
int i = 3, j = 5;
printf("\n\nBefore swap(), i is %d and j is %d\n", i, j);
swap(&i, &j);
printf("\nAfter swap(), i is %d and j is %d\n", i, j);
and the output for this small code fragment would be:
Before swap(), i is 3 and j is 5
After swap(), i is 5 and j is 3
Your job is to write the function definition (header and body) of the swap function:
soln:
void swap(int *n1, int *n2) {
int temp;
temp = *n1;
*n1 = *n2;
*n2 = temp;
} /* swap() */
Question ??? [10 points]. Write a function to round a double to the nearest int, and
return that ronded value. This function should not print anything. The function should
have one formal parameter of type double, and a return value of type int. Write the
function definition (the function header and function body). Name your function
anything reasonable. Hint: feel free to use functions from the Math library; you do not
need to write #include <math.h>. This function should be extremely short if done
correctly; the body of my solution is exactly one line, but it's OK if you use more.
soln:
int round_it(double d) {
return (int) floor(d + 0.5);
}
Question ??? [10 points]. In the country of Woogle, there is a game played on a 5 x 5
board, called the Woogle Game. The most important piece in this game is called a
woogle. In the below diagram, you see that the woogle can move to any adjacent square
(including diagonals). The reachable squares (there are 8) for the woogle in the below
diagram are marked with an x; the other squares (the hyphens) are not reachable.
-
x
x
x
-
x
W
x
-
x
x
x
-
–
-
Your job is to declare two int arrays, one called row_look, and one called col_look.
These are arrays that could be used as lookup tables in a program that simulates the
movement of a woogle, in much the same way you used lookup tables in the knight's tour
homework. All you have to do is declare (and initialize) these two arrays. For each array,
the declaration and initialization should be done on one line. That means you should
write a total of two lines.
soln:
int row_look[8] = { 0, -1, -1, -1,
int col_look[8] = {-1, -1, 0, 1,
0,
1,
1,
1,
1, 1};
0, -1};
Question ??? [10 points]. This problem is based on the stock simulation homework.
Your task here, however, is very simple. You are to write a code fragment (not a
function) to display three rows of pound (#) signs. Each row should have between 1 and
10 (inclusive) pound signs, where the number between 1 and 10 is chosen randomly.
Note that each row should generate its own random number between 1 and 10. In other
words, each row should (in all likelihood) have a different number of pound signs. To
simplify matters, do not call srand(). Do not worry about #include's or #define's. Just
write a short fragment of code. Of course you will need to use rand() in your solution.
Remember to declare any variables you use.
soln:
int row_num, num_pounds;
for(row_num=1; row_num<=3; row_num++) {
num_pounds = rand() % 10 + 1; /* 1 .. 10 */
for(i=1; i<=num_pounds; i++) {
printf("#");
}
printf("\n");
}
Question ??? [15 points].
Task: determine if 10,001 is prime (a prime number has no divisors except 1 and itself).
You have to write the code to do this, even if you already know the answer :-) You are to
write a code fragment, not an entire program, no functions, no main(), just a fragment.
Remember to declare any variables that you use. Do not worry about efficiency for this
problem. Do not bother with #include's or #define's.
The output of this fragment should be either 10001 is prime, or 10001 is NOT prime. Use
printf() to create the desired output.
soln:
int i, is_prime = 1; /* assume prime */
for(i = 2; i <= 10001/2; i++) {
/* is i a factor of 10001?*/
if (10001 % i == 0) {
/* found a factor, not prime! */
/*printf("A factor is %d\n", i);*/
is_prime = 0;
} /* if */
} /* for */
if (is_prime) /* or:
... is_prime == 1 ... */
printf("%d is prime", 10001);
else
printf("%d is NOT prime", 10001);
Download