Introduction to Computer Science

Take-home Midterm

Due: 5/09/07 at 11:00 AM


Instructions:

This midterm is due at 11:00am on Wednesday, May 9, at the start of class. 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. Should there be any evidence that you have violated this restriction, you will get a 0 on the exam. Remember that I am judge, jury, and executioner in this matter, and there will be no appeals.

Along with turning in the exam, everyone is required to sign the honor agreement and bring it to class on Wednesday, May 5. 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 problems 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. Don't forget, you get one "Get Out Of Jail Free" card for specialized assistance during the weekend.



For problems 1 and 2, what is the output of the loop, and how many times does it run (7pts each)?

1.

	a = 1024;
	for(x=1; x<a; x=x*2)
	{
		cout << a << "	" << x;
	}

2.
	int x=1, y=8192;

	while(y > x)
	{
		if(x%2 == 0)
		{
			x = 1 + x * 2;
		}
		else
		{
			a = a/x;
			x++;
		} 
		cout << a << "	" << x << endl;
	}

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;

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

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

4.
#include <iostream>

using namespace std;

#define MAX 10

int crunch(int Z, int Q);

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

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

	for(i=0; i<MAX; i++)
	{
		y[i] = crunch(x[i], i);
	}

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

int crunch(int Z, int Q)
{
	int	i, answer=1;

	for(i=0; i<Q; i++)
	{
		answer = answer * Z;
	}
	return answer;
}

5. Write a functional decomposition of the software that operates a Roomba.

(15 pts)


6. Write a program that asks the user for a number and a character. Your program should then print out a pair of mirrored triangles with a width of whatever number the user enters. For example, if the character is '&' and the number is 9, the output of the program should be:
&       &
&&     &&
&&&   &&&
&&&& &&&&
&&&&&&&&&
&&&& &&&&
&&&   &&&
&&     &&
&       &

You will be evaluated along the usual lines of your homeworks and labs.

(20 pts)


7. I have a file with a set of test scores for my Heritage class. There are 20 people in the class and each of them has three test scores. Thus, the file is laid out like this:
Homer		65	70	73
Marge		98	93	99
Bart		85	82	77
Lisa		90	93	92
Maggie		88	70	77
etc.....

Write a program that figures out each person's average test score, and also calculates the class average for each exam (so there should be three averages - one for each test) and the overall class average for everyone's performance on all the exams.

You will be evaluated along the usual lines of your homeworks and labs for the first 20 points. The final 10 points will be based on your appropriate use of variables and functions. (Hint: You should probably use arrays and at least one function for performing a calculation you'll be doing repeatedly.)

(30 pts)

You can also receive 10 bonus points for appropriately using one of the following concepts that we didn't discuss in class (but which are covered in the book): an array of structs, a two-dimensional array, or a class.

I would recommend that you not become so obsessed with getting the bonus points that you fail to reasonably complete the assignment. You can't get the bonus points if your program doesn't work at all.