1. (25 points) Write a program that prints an 8 by 8 grid with a background of dots and one X which starts in the upper left corner (row 1 column 1) and which can be moved by having the user enter the number of columns and the number of rows to move. The program runs continuously (i.e. until a user enters a CTRL-C).

 

YOU MUST have a separate function called ChangeXPosition which takes two integer parameters, one for the number of rows to move and one for the number of columns to move, and adjusts the internal coordinates of the X. A positive row number moves the X down that many rows and negative row number moves it up. A positive column number moves the X to the right and a negative column number moves it left.

 

Hints: - Use global variables for Xs current row and current column. That way the function can modify them and the main program can use them to print the X.

- The main program will, in each iteration, be drawing the grid and placing the X where the variables indicate it should go.

Note: The user is NOT entering the new position, but the number of rows columns to move.

 

Extra Credit (5 points): If a user enters numbers that would move the X off the grid from wherever it is, do not change the Xs position. Instead ask the user for new data. This request for new data must occur in main, outside of the function.

Sample Run:

 

X.......

........

........

........

........

........

........

........

Enter # rows and cols to move: 3 1

 

........

........

........

.X......

........

........

........

........

Enter # rows and cols to move: 6 2

Error: move requested would move X off grid - X not moved. [THIS IS EXTRA CREDIT]

 

........

........

........

.X......

........

........

........

........

Enter # rows and cols to move:

SEE SEPARATE LINK FOR MOVE.C FILE

 


2. (25 Points) Write a function that takes in a letter grade of A, B, C, D or F and returns the numerical equivalent. A = 4.0, B = 3.0, C = 2.0, D = 1.0 and F = 0.0. You are NOT to write a whole program, etc. with main(). You are to write only the code for the function.

 

 

float ConvertGrade( char cGradeLetter )

{

float fConversion = -1.0 ;

 

switch (cGradeLetter)

{

case 'A':

fConversion = 4.0 ;

break;

case 'B':

fConversion = 3.0 ;

break;

case 'C':

fConversion = 2.0 ;

break;

case 'D':

fConversion = 1.0 ;

break;

case 'F':

fConversion = 0.0 ;

break;

default:

fConversion = -1.0 ;

break;

 

} /* end switch */

 

return fConversion ;

} /* end ConvertGrade */


 


3. (20 points) What output does the following code fragment produce?

 

int i, j, result ;

for (i = 8; i >= 6; i--)

{

for (j = 1; j <= 3; j++)

{

result = i / j 1 ;

 

printf( %d, result );

 

} /* end for j */

} /* end for i */

if ( result % 2 == 0 )

printf(-) ;

else

printf(+) ;

 

printf(\n);

 

Answer: 731621521+

 

4. (15 points) There are at least 5 errors in the code below. Comments, formatting or lack thereof are not any of the errors. Identify line numbers and errors in the space provided.

 

1 #include <stdio.h>

2 int main()

3 {

4 int iFirstOperand = 0, iSecondOperand = 0;

5 printf( "Please enter two numbers to multiply\n");

6 scanf ("%dd", &iFirstOperand, &iSecondOperand );

7 printf(The answer is %d\n, Multiply ( a, b) );

8 return 0 ;

9 } /* end program */

10

11 int Multiply( int a, int b ) ;

12 {

13 return a * b

14 } /* end Multiply Function */

Answer

1_1-2 Missing function prototype for Multiply_

2_6__ Missing % between ds_

3_7__ a and b should be iFirstOperand and iSecondOperand_

4_11_ Line should not end with a semicolon_

5_13_ Missing semicolon at end_


 

5. (15 points)

i.                 What is the value of result after the following statements?

 

result = 35;

result -= result * 2 2 + (5 % 2); Answer:_-34_

 

ii.               If a = 12 and b = 15, does the following expression in parentheses evaluate to true or false?

( a >= 13 || b < 8 || b < a) Answer: _False_

 

iii.              Do you write the body of a function in the function definition or in the function prototype? (indicate which one on the line below):

 

_______function definition______________________________________

 

iv.              What does void mean when seen as the return type in a function prototype? (one sentence answer only):

 

_______function doesnt return anything_____________________

 

v.               Given the following code fragment, what is the final value of result?

 

int result = 0 ;

result *= result * 2 + 3 ;

 

Answer:__0___