Solutions in Word 97 format.

advertisement
CS1003: Introduction to Computer Programming in C
Fall 1999
Lecturer: Carl Sable
EARLY FINAL SOLUTIONS
Below are answers to the questions from the early version of the final exam. Of course,
most of these questions have more than one possible answer!
Answer #1:
gcc -c a.c
gcc -c b.c
gcc -c c.c
gcc -o run_me a.o b.o c.o
(You could combine the first three commands into one if you prefer.)
Answer #2:
bubble sort - Repeatedly loop through the array exchanging adjacent elements if they are
out of order. If an entire pass through the array doesn't swap any elements, stop.
selection sort - Find the smallest element in the array and move it to the beginning of the
array, then find the second smallest and move it to the second position of the array, etc.
until the entire array is sorted.
insertion sort - Take one element at a time and stick in the correct place in the part of the
array that's already been sorted.
merge sort - Recursively sort the left half of the array and right half of the array and then
merge the two sorted halves together.
Merge sort is most efficient when dealing with large arrays in random order.
Bubble sort is most efficient the each element starts out at most a few positions from its
correct location.
Answer #3:
x = rand() % 101 + 100;
Answer #4:
Recursively visit the left subtree, then the current node, then the right subtree:
20, 80, 5, 50, 5, 10, 1, 20
Answer #5:
1
1)
2)
3)
4)
Need to initialize count to 0.
Need "FILE *fpInput" to make fpInput a pointer to a FILE struct.
Shouldn't have quotes around "filename" in the fopen statement.
Get rid of the first of the two calls to fscanf, you should only call it once per word!
Answer #6:
x = 200, w = 25, *p1 = 200, *p2 = 150.
x = 150, w = 197, *p1 = 197, *p2 = 150.
Answer #7:
a) recursive solution:
int compute_f(int x)
{
if (x <= 0)
return 0;
return 2*compute_f(x - 1) + 3*compute_f(x - 2) + 5;
}
b) non-recursive solution:
int compute_f2(int x)
{
int prev1, prev2, cur, y;
if (x <= 0)
return 0;
prev1 = 0;
prev2 = 0;
for (y = 1; y <= x; y++)
{
cur = 2*prev1 + 3*prev2 + 5;
prev2 = prev1;
prev1 = cur;
}
return cur;
}
(If you initialize cur to 0, you can leave out the "if" statement. Also, the initializations
and updates of prev1 and prev2 could be added to the sections of the "for" statement.)
2
c) The iterative (non-recursive) solution is much more efficient. In addition to requiring
extra time to do function calls, the recursive solution will wind up calculating certain
values of f(x) multiple times, repeating work redundantly. In fact, it is an exponential
time solution, whereas the iterative solution is linear!
Answer #8:
float path_length(PNODE pList)
{
PNODE pPrev, pCur;
float length = 0;
if ((pList == NULL) || (pList->next == NULL))
return 0;
pPrev = pList;
pCur = pList->next;
while (pCur != NULL)
{
length = length + sqrt(pow(pCur->x - pPrev->x, 2)
+ pow(pCur->y - pPrev->y, 2));
pPrev = pCur;
pCur = pCur->next;
}
return length;
}
Answer #9:
void rot13(char *str)
{
char *pc;
for (pc = str; *pc != '\0'; pc++)
{
if (islower(*pc))
{
if (*pc <= 'm')
*pc = *pc + 13;
else
*pc = *pc - 13;
}
if (isupper(*pc))
{
3
if (*pc <= 'M')
*pc = *pc + 13;
else
*pc = *pc - 13;
}
}
return;
}
You can avoid using isupper and islower all together if you simply check for the four
ranges that require changes ('a' … 'm', 'n' … 'z', 'A' … 'M', 'N' … 'Z') with character
comparisons. You may also treat the string as an array instead of a pointer and loop
through indices with the loop (e.g. loop while str[index] != '\0').
Answer #10:
#include <stdio.h>
int main(void)
{
int x, row, col;
do
{
printf("Enter an integer from 3 to 20: ");
scanf("%d", &x);
} while ((x < 3) || (x > 20));
for (row = 1; row <= x; row++)
{
for (col = 1; col <= x; col++)
{
if ((row == 1) || (row == x) || (col == 1) || (col == x))
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
(It is also OK if you have separate sections of the program to print out the first row, the
middle rows, and the last row.)
4
Download