
1
Practice Midterm #2(Closed book and closed notes)
1. (a) What output values are displayed by the following while loop for a data value of 5? Of 6? Of 7?
printf("Enter an integer>" );
scanf("%d",&x);
product = x;
count = 0;
while (count < 4){
printf("%d\n",product);
product *= x;
count += 1;
}
(b) In general, for a data value of any number n, what does this loop display?
2. Evaluate the following expressions if xis 10.5,yis 7.2,mis 5, and nis 2. The value of the character ’A’ is 65.
(a) x/ ((double) m) (b) x/m
(c) (double)(n * m) (d) (double)(n / m ) + y
(e) (int)’D’ - (int)’A’ (f) (char)((int)’C’+2)
3. Write a function to dispense change. The user enters the amount due. The program determines how many
dollars, quarters, dimes, nickels, and pennies should be given as change. Write a function with four outputs
paramters that determines the quantity of each kind of coin.
4. (a) Declare an enumerated type month tand rewrite the following if statement, assuming that cur month is
type month t instead of type int. (b) Write the equivalent switch statement.
if (cur_month == 1)
printf("Happy New Year\n");
else if (cur_month == 6)
printf("Summer begins\n");
else if (cur_month == 9)
printf("Back to school \n");
else if (cur_month == 12)
printf("Happy Holidays\n");
5. Write a program that determins which player(s) have three in a row horizontally in a file that contains a tic-
tac-toe board. Your program must contain two functions besides main: (1) read file will ask for a filenames,
and then read the information from the file into a two-dimensional array of chars; and (2) check player()
takes two dimensional array, and a player’s char (’X’ or ’O’) and returns true of that character fill a row. You
must always use nested for-loop to access the array. Remember to include all necessary header files. Comments
are unnecessary.
[...@pc...] cat tictactoe1.txt [...@pc...] cat tictactoe2.txt
XXX XOX
OOX OOO
OXO XXO
[...@pc...] a.out [...@pc...] a.out
Enter the name of input file > tictactoe1.txt Enter the name of input file > tictactoe2.txt
X won. O won.
[...@pc...] cat tictactoe3.txt [...@pc...] cat tictactoe4.txt
XOX XOX
XXO OOX
OO X O
[...@pc...] a.out [...@pc...] a.out
Enter the name of input file > tictactoe3.txt Enter the name of input file > tictactoe4.txt
tie. tie.