Practice Midterm 2 - Introduction to Programming and Problem Solving | ECS 030, Exams of Computer Science

Material Type: Exam; Class: Intro Prog&Prob Solving; Subject: Engineering Computer Science; University: University of California - Davis; Term: Unknown 1989;

Typology: Exams

Pre 2010

Uploaded on 07/30/2009

koofers-user-67m
koofers-user-67m 🇺🇸

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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.

Partial preview of the text

Download Practice Midterm 2 - Introduction to Programming and Problem Solving | ECS 030 and more Exams Computer Science in PDF only on Docsity!

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?

  1. Evaluate the following expressions if x is 10.5, y is 7.2, m is 5 , and n is 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)
  2. 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.
  3. (a) Declare an enumerated type month t and 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");

  1. 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.