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 X’s 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 X’s position. Instead ask the user for new data. This request for new data must occur in main, outside of the function.
Enter # rows and cols to move: 3 1
Enter # rows and cols to move: 6 2
Error: move requested would move X off grid - X not moved. [THIS IS EXTRA CREDIT]
Enter # rows and cols to move:
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 ;
fConversion = 4.0 ;
fConversion = 3.0 ;
fConversion = 2.0 ;
fConversion = 1.0 ;
fConversion = 0.0 ;
fConversion = -1.0 ;
} /* 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 )
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()
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 */
11 int Multiply( int a, int b ) ;
13 return a * b
14 } /* end Multiply Function */
1_1-2 Missing function prototype for Multiply_
2_6__ Missing % between d’s_
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):
iv. What does “void” mean when seen as the return type in a function prototype? (one sentence answer only):
_______function doesn’t return anything_____________________
v. Given the following code fragment, what is the final value of result?
int result = 0 ;
result *= result * 2 + 3 ;