COM 1100 Sergey L. Bratus Homework Exercises 1 Due Thu, Feb 19

You don't have to run the code you write for this assignment, although you may choose to do so. Please submit your answers to programming questions in printed form (Notepad or SimpleText, or any other words processors are OK).

Use the windows on the next page to show the output of the following code:
(The grid boxes are 20 pixels apart.)

// function prototypes
int F(int& x, int& y);
void G(int N, int& M);
void Boxes(int A, int B);

// function definitions
void main()
{	// open text and drawing windows
	BigSquarePair();
	
	//Problem 1-----------------------
	for (int L = 0; L <= 360; L = L+40)
		DrawLine(L, 400, L+20, 360);        
		
	Boxes(10, 20);
	Boxes(40, 120);

	//Problem 2-----------------------	
	int x = 5;   
	cout << "Problem 2" << endl;
	cout << "x = " << x << endl;
	G(3, x);
	cout << "x = " << x << endl;
	
	x = 3;
	cout << "x = " << x << endl;
	G(5, x);
	cout << "x = " << x << endl;
	cout << endl;

	//Problem 3-----------------------
	{	
	int x = 1, y = 5, z;			
	cout << "Problem 3" << endl;	
	// setw(5) is to make columns line up.
	cout << setw(5) << "j" << setw(5) << "x" << setw(5) << "y" << setw(5) << "z" << endl;
	for (int j = 1; j <=5; j++){
		z = F(x, y);
		cout << j << setw(5) << x << setw(5)  << y << setw(5) << z << endl;
	}
	cout << endl;
	}

	//Problem 4-----------------------
	{
	int	x = 1;   
	int	y = 3;
	cout << "Problem 4" << endl;
	cout << setw(5) << "j" << setw(5) << "x" << setw(5) << "y" << endl;
	for (int j = 1; j <=5; j++){
		x = F(x, y);
		cout << j << setw(5) << x << setw(5) << y << endl;
	}
	}
	PressReturn();
} // end main


int F(int& x, int& y){
	if (x < y){
		x = 2*x;
		return y;
	}
	else{
		y = 2*y;
		return x;
	}
}

void G(int N, int& M){
	int P = 1;
	for (int k = 1; k <= N; k++)
		P = M*P;
	M = P;
}

void Boxes(int A, int Y){
	int W = 400 / A;
	int X = 0;
	
	for (int I = 0; 
		   X + W < 400; 
		   X += 2*W){
		PaintRect(X, Y, X+W, Y+W);
		W += 20;
	}
}

Problem 5: Assume the following declarations:

int 	score, number;
double 	angle;
char 	response, ch;
bool 	tired, done;
Construct a logical expression to represent each of the following conditions:

Problem 6: Write the code segment that paints the 200 x 200 graphics window in a checkerboard pattern of squares 20 x 20. The black and white squares must alternate both vertically and horizontally (there will be 10 squares in each row and in each column).

Problem 7: Each positive integer can be factored into a product of prime numbers. Write a function PrintPrimeDivisors that takes an integer n and prints all prime numbers that divide n evenly. For example, PrintPrimeDivisors(220) will print 2, 5, 11 . If the number n is negative or zero, the function must print an error message. The function need not return any value, i.e. its return type can be void. You may assume that the function bool IsPrime(int n) is available.

Extra credit: Write a function PrintPrimeFactors that takes an integer parameter n and prints all prime divisors of n, each divisor as many times as it appears in the factorization. For example, PrintPrimeFactors(220) will print 2, 2, 5, 11 .

Problem 8: Write a C++ function Max3 that takes three integers as arguments and returns the largest one of them.

Problem 9: Write a C++ function GetCircleData that:

This function may be used if you know that at some point the user will need to enter the data, but you don't want to specify as yet in what form the input will occur (via cin, or via a dialog box, or in some other visual way). For now you assume that the user is using the keyboard. So you will declare variables to hold the data and then call GetCircleData to fill them it. If the function returns true, you will know that the users input was valid.

Problem 10: Write a function that returns through its reference parameters both the min and max of the first n items stored in the array A.

void ExtremesOfArray(int& min, int& max, int A[], int n);