CSCI 1300 Exam 2 Practice

These are practice problems for the second exam. In each case, your answer should be a function (not a complete program). For the problems that involve graphics, you may always use these items:


Practice Problems to Do in Recitation
  1. Write a function that computes and returns the factorial of a number. The number is a parameter to the function and is guaranteed to be a non-negative integer.

  2. Write a void function which asks the user for one number at a time until it sees a zero. This function will then compute the arithmetic mean (sum-of-numbers/numer-of-numbers) of these numbers (excluding the zero) and display it to the user.

  3. Write function with one parameter, an int called c. The function returns a bool that is true if there is at least one pixel on the screen with color c; otherwise the function returns false.

  4. Write function with one parameter, an int called c. The function returns a bool that is true if all pixels on the screen have color c; otherwise the function returns false.

  5. Write a void function that will make the screen look like a chess board:

    The size of the chessboard is n by n (where n is a parameter to the function).

  6. Write a void function which solves a quadratic equation a*x^2+b*x+c=0. The double numbers, a, b and c, are parameters to the function. Use formulas (-b+sqrt(b*b-4*a*c))/(2*a) and (-b-sqrt(b*b-4*a*c))/(2*a) for the 1st and the 2nd root respectively.

    There are three cases to worry about: (1) When a=0 the function should print "Be careful with zeros!" (2) When b*b-4*a*c<0 it should print "Don't want to deal with complex numbers!". (3) Otherwise the function must compute and print the two roots of the equation.

  7. Write void a function which prints your name in the graphics window n*n times. The number n is a parameter to the function. The n*n different printings of your name should be evenly spaced in n columns and n rows on the screen.

  8. An ant is sitting on a graphics screen at location (px, py) and it is facing a direction that is described by an integer h as follows:
    1. h=0 means the ant is facing toward the top of the screen
    2. h=1 means the ant is facing toward the right edge of the screen
    3. h=2 means the ant is facing toward the bottom of the screen
    4. h=3 means the ant is facing toward the left edge of the screen
    Write a function called move_ant with three reference parameters, px, py and h, which describe the ant's position and direction at some time. The function then makes these changes to the parameters and the pixels on the screen:

    If the ant is starting on a black pixel, then it will do these things: Change the pixel to white, turn 90 degrees counterclockwise, and move forward one square.

    If the ant is starting on a pixel square, then it will do these things: Change the pixel to black, turn 90 degrees clockwise, and move forward one square.

    P.S. If you write a main program that calls this function repeatedly, you'll get very interesting patterns described as Langton's Ant.


Additional Problems That We Have Already Done in Class:
  1. Write a function with one parameter, an int called x. Precondition: x > 1. The function returns a bool: true if x is a prime number and false otherwise.

  2. Write a function with one parameter, an int called x. Preconditon: x > 0. The function returns an int that is equal to the sum of all the factors of x (not including x itself).

  3. Write a function that computes and returns an approximation of the cube root of x (where x is a parameter that is guaranteed to be greater than or equal to 1). Use the bisection method discussed in class.

  4. Write a void function that flips all pixels on the screen from top to bottom. In particular, after the function finishes its work, the color of a pixel at location (px, py) is equal to the original color of the pixel at location (px, S-py-1). So, if the screen looks like this when the function begins: , then it will look like this when the functioin finishes: . Do not use any double-buffering.