// Prob 1 int factorial(int num) { int fact = 1; int i; for (i = 1; i <= num; i++) { fact = fact * i; } return fact; } // Prob 2 void compute_mean() { double input_num; double sum = 0; int num_numbers = 0; bool done = false; while (!done) { cout << "Type a number: "; cin >> input_num; if (input_num != 0) { num_numbers++; sum = sum + input_num; } else { done = true; } } if (num_numbers == 0) { cout << "I can't compute that average!" << endl; } else { cout << "The mean is " << sum/num_numbers << endl; } } // Prob 3 bool is_color_on_screen(int c) { int px, py; for (px = 0; px < S; px++) { for (py = 0; py < S; py++) { if (getpixel(px, py) == c) { // We found a spot of that color! return true; } } } return false; } // Prob 4 bool is_color_all(int c) { int px, py; for (px = 0; px < S; px++) { for (py = 0; py < S; py++) { if (getpixel(px, py) != c) { // We found a spot that's not that color! return false; } } } return true; } // Prob 5 // This is a different solution than the one given in class. void chess_board(int n) { int row, col; int numRows = 8; int numCols = 8; int px, py; int x_size = n/8; int y_size = n/8; cleardevice(); for (row = 0; row < numRows; row++) { for (col = row % 2; col < numCols; col += 2) { // Fill square (could use the BGI bar function instead of // these two for loops) for (px = x_size*col; px < (x_size*col + x_size); px++) { for (py = y_size*row; py < (y_size*row + y_size); py++) { putpixel(px, py, WHITE); } } } } } // Prob 6 void quadratic(double a, double b, double c) { double first_root; double second_root; if (a == 0) { cout << "Be careful with zeros" << endl; } else if ((b*b - 4*a*c) < 0) { cout << "Don't want to deal with complex numbers!" << endl; } else { first_root = (-b + sqrt(b*b - 4*a*c))/(2*a); second_root = (-b - sqrt(b*b - 4*a*c))/(2*a); cout << "1st root = " << first_root << endl; cout << "2nd root = " << second_root << endl; } } // Prob 7 void print_name(int n) { int px, py; for (px = 0; px < S; px += S/n) { for (py = 0; py < S; py += S/n) { bgiout << "Mike"; outstreamxy(px, py); } } } // Prob 8 void move_ant(int& px, int& py, int& h) { if (getpixel(px, py) == BLACK) { putpixel(px, py, WHITE); // Turn 90 degrees counterclockwise. How does it work? An an example, // suppose that h is 2. Then (h + 3) % 4 is 5 % 4, which is 1. h = (h + 3) % 4; } else if (getpixel(px, py) == WHITE) { putpixel(px, py, BLACK); // Turn 90 degress clockwise h = (h + 1) % 4; } // Move forward one pixel: switch (h) { case 0: py--; break; case 1: px++; break; case 2: py++; break; case 3: px--; break; } } // Problem A // Note: Can you do this more efficiently? bool is_prime(int x) { assert (x > 1); int i; for (i = 2; i < x; ++i) { if (x % i == 0) { // Not a prime return false; } } return true; } // Problem B: Return sum of factors of x up to but not including x // Note: Can you do this more efficiently? int sum_of_factors(int x) { assert (x > 1); int i; int answer = 1; // Start with the factor 1 right away for (i = 2; i < x; ++i) { if (x % i == 0) { // Found a factor! answer += i; // Add the factor } } return answer; } // Problem C: Returns a number within 0.1 of the cube root of x double cuberoot(double x) { assert(x >= 1); double low = 1; double high = x; double mid; while (high - low > 0.1) { mid = (low + high)/2; if (mid*mid*mid < x) { low = mid; } else { high = mid; } } return (high+low)/2; } // Problem D: void flip_vertical( ) { int px, py; int c1, c2; for (px = 0; px < S; ++px) { for (py = 0; py < S/2; ++py) { c1 = getpixel(px, py); c2 = getpixel(px, S-py-1); putpixel(px, py, c2); putpixel(px, S-py-1, c1); } } }