Review for CS150 Final

The final is comprehensive in the sense that everything builds on itself. Possible questions will center around (but not be limited to) the following:


As always, you will be required to write program segments, functions, programs, or combinations of all three. Let's take a look at some example problems you could be expected to solve and their difficulty:

P#1 (Easy): Write a function computeven that accepts an array values and the number of values currently in the array, howmany, and returns through the function name the number of values in the array that are even. Your function might be called by a statement of the form:

cout << "Number of evens = " << computeven (values, howmany) << endl;
P#2 (Medium): Rework P#1 but return the number of values in the array that are prime.

P#3 (Easy): An integer array values has been defined and filled beginning at subscript 0 with howmany elements in the array.

a) Write a prototype for a void function insert that will accept the array values, howmany elements in the array, and a new integer value to be inserted at the beginning of the array.

b) Write the actual function insert described in a) completely.

P#4: (Difficult): Assume the array in P#3 is sorted from smallest to largest. Insert the value into the array in the proper position of the array.

P#5: (Medium): What will the following C++ program print?

	#include <iostream.h>
	void change (int&, int, int&);
	void main()
	{
	  int i, j, k;
	  i = 22;
	  j = 4;
	  k = 7;
	  change (i, j, k);
	  cout << i << "  " << j << "  " << k << "  " << endl;
	  change (k, i, j);
	  cout << i << "  " << j << "  " << k << "  " << endl;
	}
	void change (int& i, int j, int& k)
	{
	  i = i + k;
	  j = i / 3;
	  k = i % j;
	}
computeslope (multiple, howmany);
P#6 (Difficult): Many infinite series have sums equal to pi. One formula goes as follows:
pi = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + ....
This sequence consists of alternating positive and negative terms. You are to write a single C++ function which estimates pi using the above formula. The function might be called by a statement of the following form:
  cout << "Pi = " << computepi(numterms) << endl;

where numterms is an integer value containing the number of terms to be used from the above sequence.

P#7: (Easy): Given the following prototypes and declarations, place a check by only the illegal procedure calls in the following list AND specify what caused the error.


	int compute (int, int&, int);
	float doit (float&, int, float, char&);

	void main()
	{
	  int i,j;
	  int nums[10];
	  float x,y;
	  char ch;


_____ a) compute (i, j, i + j);


_____ b) compute (i, nums[1], char ch);


_____ c) doit (x, i, y, 'c');


_____ d) doit (x + y, nums[i], y, ch);


_____ e) doit (x, 3, x, static_cast<char>( static_cast<int>('a') + 1)));

© Douglas J. Ryan/ryandj@pacificu.edu