CS1003: Introduction to Computer Programming in C

advertisement
CS1003: Introduction to Computer Programming in C
Summer 2002
Lecturer: Carl Sable
FINAL SOLUTIONS
Below are answers to the questions from the final exam. Of course, some of these
questions have more than one possible answer!
Question #1:
Compiling converts source code (e.g. C) into machine code, which the computer
"understands". Linking combines machine code from one or more source code files with
the machine code from provided libraries to create an executable.
Question #2:
a) 5
b) 8
c) 155 (f(x) will always be 3 more than f(x-1) for positive x, so f(50) = 50 * 3 + 5)
Question #3:
bubble sort: {40, 119, 39, 80, 256}
selection sort: {39, 40, 256, 119, 80}
insertion sort: {40, 119, 256, 39, 80}
Question #4:
a) p1 = num; (or p1 = &num[0];)
b) p1 = num + 99; (or p1 = &num[99];)
c) 99
d) 396 (99 * 4)
Question #5:
1) You need an address-of operator (&) before the "x" in the "scanf".
2) The condition of the "do…while" should use an "||" instead of an "&&".
3) There should not be a semicolon after the right parenthesis of the first "for" loop.
4) The condition of the second "for" loop should be "z <= x".
5) Within the outer loop, you need the line "putchar('\n')" to end each row.
Question #6:
x=400, z=700, *p1=400, *p2=700
x=1100, z=200, *p1=1100, *p2=200
1
Question #7:
float compute_average(PNODE pList)
{
int sum = 0;
int count = 0;
if (pList == NULL)
return 0;
while (pList != NULL) {
sum = sum + pList->x;
count = count + 1;
pList = pList->next;
}
return (1.0 * sum / count);
}
Question #8:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char tmp_word[50];
int count = 0;
FILE *fpInput;
if (argc != 3)
{
printf("Usage: %s <filename> <word>\n", argv[0]);
return 1;
}
fpInput = fopen(argv[1], "r");
if (fpInput == NULL)
{
printf("Could not open input file!\n");
return 1;
}
while (fscanf(fpInput, "%s", tmp_word) != EOF)
{
if (strcmp(tmp_word, argv[2]) == 0)
count++;
2
}
printf("The word occurs %d times in the file.\n", count);
fclose(fpInput);
return 0;
}
Question #9:
I am presenting two possible solutions. The first solution accesses the string using a
pointer, and it uses "islower" and "isuppper" to help check for the four possible regions of
characters that require changes separately:
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))
{
if (*pc <= 'M')
*pc = *pc + 13;
else
*pc = *pc - 13;
}
}
return;
}
The second solution treats the string as an array, and it uses two "if" statements to check
for all cases for which letters need to be increased or decreased:
void rot13b(char str[])
{
int i, length = strlen(str);
for (i = 0; i < length; i++)
3
{
if (((str[i] >= 'a') && (str[i] <= 'm')) ||
((str[i] >= 'A') && (str[i] <= 'M')))
str[i] = str[i] + 13;
else if (((str[i] >= 'n') && (str[i] <= 'z')) ||
((str[i] >= 'N') && (str[i] <= 'Z')))
str[i] = str[i] - 13;
}
return;
}
Question #10:
Most students did well on this problem, although many had some small mistakes. I will
present a few possible solutions. We'll start with a straightforward one:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int die1, die2, sum1, sum2;
srand(time(NULL));
/* Do the first roll of the dice. */
die1 = rand()%6 + 1;
die2 = rand()%6 + 1;
sum1 = die1 + die2;
printf("You rolled a %d and a %d, the total is %d.\n",
die1, die2, sum1);
/* Check if first roll is a win. */
if ((sum1 == 7) || (sum1 == 11)) {
printf("Congratulations, you win!\n");
return 0;
}
/* Check if first roll is a loss. */
if ((sum1 == 2) || (sum1 == 3) || (sum1 == 12)) {
printf("You lose, sucker!\n");
return 0;
}
4
while (1) {
/* Keep rolling dice until player wins or loses. */
die1 = rand()%6 + 1;
die2 = rand()%6 + 1;
sum2 = die1 + die2;
printf("You rolled a %d and a %d, the total is %d.\n",
die1, die2, sum2);
/* Check if current roll wins. */
if (sum2 == sum1) {
printf("Congratulations, you win!\n");
return 0;
}
/* Check if current roll loses. */
if (sum2 == 7) {
printf("You lose, sucker!\n");
return 0;
}
}
/* We should never get here! */
return 1;
}
Here is another solution that uses functions to avoid repeated code, although the total
number of lines is longer:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int roll_dice(void);
void win(void);
void lose(void);
int main(void)
{
int sum1, sum2;
srand(time(NULL));
/* Do the first roll of the dice. */
sum1 = roll_dice();
/* Check if first roll is a win. */
5
if ((sum1 == 7) || (sum1 == 11))
win();
/* Check if first roll is a loss. */
if ((sum1 == 2) || (sum1 == 3) || (sum1 == 12))
lose();
while (1) {
/* Keep rolling dice until player wins or loses. */
sum2 = roll_dice();
/* Check if current roll wins. */
if (sum2 == sum1)
win();
/* Check if current roll loses. */
if (sum2 == 7)
lose();
}
/* We should never get here! */
return 1;
}
/* Perform a single roll of the dice and return the sum. */
int roll_dice(void)
{
int sum, die1, die2;
die1 = rand()%6 + 1;
die2 = rand()%6 + 1;
sum = die1 + die2;
printf("You rolled a %d and a %d, the total is %d.\n",
die1, die2, sum);
return sum;
}
/* Congratulate the player and exit the program! */
void win(void)
{
printf("Congratulations, you win!\n");
exit (0);
}
/* Mock the player for losing and exit the program! */
6
void lose(void)
{
printf("You lose, sucker!\n");
exit (0);
}
Here is a third solution that does not treat the first roll separately like the others:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int die1, die2, sum, goal;
int first_turn = 1; /* Boolean, only true for first roll
of dice. */
srand(time(NULL));
while (1) {
/* Keep rolling dice until player wins or loses. */
die1 = rand()%6 + 1;
die2 = rand()%6 + 1;
sum = die1 + die2;
printf("You rolled a %d and a %d, the total is %d.\n",
die1, die2, sum);
/* Check for all possible wins. */
if ((first_turn && ((sum == 7) || (sum == 11))) ||
(!first_turn && (sum == goal))) {
printf("Congratulations, you win!\n");
return 0;
}
/* Check for all possible losses. */
if ((first_turn &&
((sum == 2) || (sum == 3) || (sum == 12))) ||
(!first_turn && (sum == 7))) {
printf("You lose, sucker!\n");
return 0;
}
/* If this is the first turn, record the sum. */
if (first_turn) {
7
goal = sum;
first_turn = 0;
}
}
/* We should never get here! */
return 1;
}
8
Download