Introduction to Computer Science

Take-home Midterm

Due: 5/04/05 at noon


Instructions:

This midterm is due at noon on Wednesday, May 4. Late exams will be 10% for every 24 hours after the due date.

This exam is to be worked on ALONE. You are not allowed to work on the exam with anyone else, to consult with anyone else on the exam, to consult on-line references or web sites or chat rooms or whatever. The only resources you are allowed to use for this exam are the textbook, Visual Studio, the previous programs you yourself have written, and the knowledge in your head.

Along with turning in the exam, everyone is required to sign the honor agreement and bring it to class on Wednesday, May 4. I will not grade your exam or give you credit for having done it until I get your signed honor agreement.

To turn the exam in, you should e-mail me four files - a text file containing your analyses of the loops and programs in problems 1-4 and the functional decomposition in problem 5, and the source files for the programs you write in probelms 6 and 7.

Problems 1-4 should be done BY HAND, not by compiling the code and then running it. I'm trusting you here. Don't let me down.

Good luck. If you have any questions, you know where to find me, but it should be pretty straightforward.



For problems 1 and 2, create a table showing how many times the loop runs, and what the values of a and x are for each time through the loop. (7 pts each)

1.

	a = 256;
        for(x=0;x<a;x++)
        {
                if(x%2 == 1)
                {
                        x = x*x;
                }
                else
                {
                        a = a/2;
                }
        }

2.
	int x=1;
	int a=10000;
	while(a > 0)
	{
		a = a/x;
		x++;
	}

For problems 3 and 4, tell me what the output of the program will be. (10 pts each)

3.

#include <iostream>

using namespace std;

int main()
{
	int	num, x, y, total;

	cout << "Please enter a number:  ";
	cin >> num;
	cout << endl;

	for(x=1; x<=num;x++)
	{
		total=0;
		for(y=x;y>0;y--)
		{
			total = total + (y*y);
		}
		cout << x << "  ----->  " << total << endl;
	}
}

4.
#include <iostream>

using namespace std;

#define MAX 10

void crunch(int a[MAX], int b[MAX]);

int main()
{	int	x[MAX], y[MAX];
	int	i;

	cout << "Please enter " << MAX << " numbers." << endl;
	for(i=0;i<MAX;i++) 
	{
		cin >> x[i];
	}
	cout << endl << endl;
	crunch(x, y);

	for(i=0;i<MAX;i++)
	{
		cout << x[i] << "  ----->  " << y[i] << endl;
	}
}

void crunch(int a[MAX], int b[MAX])
{
	int i, j;

	for(i=0; i<MAX; i++)
	{
		b[i] = 1;
		for(j=1;j<=a[i];j++)
		{
			b[i] = j * b[i];
		}
	}
}

5. Write a functional decomposition of a program that I can play checkers against. Be as specific as possible. (15 pts)
6. Write a program that asks the user to enter two integers at a time. The program should read in pairs of integers either until one of the numbers is a negative number or until the user has entered the MAX number of pairs. These pairs of integers should be stored in two arrays. Your program should then go through these two arrays and determine whether the second integer in a pair is a factor of the first integer, and print out that result. (25pts)

For example, if the user enters the pairs:
25 5
13 4
66 22
144 9
15 -3

The program should output:
5 is a factor of 25
4 is not a factor of 13
22 is a factor of 66
9 is a factor of 144


7. Write a program that will read in information from a file about the class softball team and generate statistics about the team. Your program should use a class for storing information about individual players and an array for storing the team. Your program should allow me to, at a minimum, query the following information:

Along with simply judging it based on whether it works or not, your program will also be graded on whether you make intelligent choices about which functions should be part of the class and which ones should be generic functions.

A sample input file is here. The file is organized such that, from left to right the columns are: name, number, position, batting average, home runs, ERA.

(25 pts)