Review for CS150 Final
Final is Friday, December 11, 2009 8:30am-11:00am
The final is comprehensive in the sense that everything builds on
itself. Possible questions will center around (but not be limited to)
the following:
Writing or explaining program segments dealing with:
if statements
switch statements
while loops
for loops
do-while loops
Rewriting if blocks as switch blocks and visa versa
Rewriting loops in one form to another
Correcting syntax, logic, and/or runtime errors
Writing or explaining data file segments
Tracing function calls with various parameter passing options( your favorite)
Make sure you understand the various data types and type casting
Arrays
Let's take a look at some examples and their complexity:
P#0 (Easy): Write a function GCD which accepts two positive integer
values and returns the greatest common divisor of both numbers. An
example call to your function might be:
cout << gcd (8, 20) << endl;
The above would print 4 since 4 is the greatest common divisor of 8 and 20.
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. A prime number is only divisible by 1 and the number
itself.
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 end 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): A char variable ch
contains a char value in the range of 'A' to 'Z' inclusive. Write
exactly one C++ assignment statement that will find what position this
char value is in the alphabet and place that value in the int variable position. As an example, if the value of ch was 'C', then position would be assigned the value of 3.
P#6: (Medium): What will the following C++ program print?
#include <iostream>
using namespace std;
void change (int&, int, int&);
int 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;
return 0;
}
void change (int& i, int j, int& k)
{
i = i + k;
j = i / 3;
k = i % j;
}
P#7 (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#8: (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], int (x));
_____ c) doit (x, i, y, 'c');
_____ d) doit (x + y, nums[i], y, ch);
P#9) (Hard) What is the output produced by executing the following program?
void changeit (int, int&, int&);
void main()
{
int i = 3;
int j = 5;
int k = 7;
changeit (i, j, k);
cout << i << j << k << endl;
changeit (k, j, i);
cout << i << j << k << endl;
}
void changeit (int i, int& j, int& a)
{
int k = 2;
i = j + a++;
j = i % k;
}
P#10) (Easy): (a) What is the difference between a float and a double?
When would you use a double? (b) What is type casting? Give an example
and a reason for using type casting.
P#11: (Medium): Write a C++ program that reads one character at a time
from a data file message.txt and (a) changes all uppercase characters
to lowercase and (b) changes all multiple occurrences of a space to
exactly one space. Your the changed text is to be saved in a file
called "newmessage.txt". As an example, the file:
This Computer Science stuff
sure is a lot of fun.
would become
this computer science stuff
sure is a lot of fun.