// CSCI 1300 -- Practice for the Week 5 Programming Quiz // File: lab-4.cxx // Student Name: // Complete the program as directed in the comments. Test your work. // Show your work to your TA. Oh, yeah: and have fun! #include #include #include #include "graphics.h" using namespace std; int main( ) { // Please declare any variables that you need here. // Choose meaningful names and put a comment with each // variable indicating its purpose. int n; // User's input int i, j; // Loop control variables int sum; // The sum for question 1. double z; // User's input int count; // Number of times we cut z in half int x, y; // Pixel coordinates // 1. Write a bit of C++ code that asks the user to type // a positive integer n. The program then uses a loop // to compute the sum 1+2+3+4+...n. This sum is then // printed. // Michael's Solution: cout << "Please type a positive integer: "; cin >> n; sum = 0; i = 1; while (i <= n) { sum = sum + i; ++i; } cout << "The sum is " << sum << endl; // Or as a for-loop: cout << "Please type a positive integer: "; cin >> n; sum = 0; for (i = 0; i <= n; ++i) { sum = sum + i; } cout << "The sum is " << sum << endl; // 2. Write a piece of code that asks the user to type any // double z that is >= 1.0. The program then uses a loop to // determine how many times z can be cut in half before // the value drops to 1.0 or less. This number is then // printed (it is approximately log base 2 of z). // Michael's solution: cout << "Please type a number >= 1.0: "; cin >> z; count = 0; while (z >= 1.0) { z = z/2; ++count; } cout << "The approx log base 2 is " << count << endl; // 3. Write a piece of code that prints a simple multiplication // table. The table should list i*j for every value of i // and j from zero to 9. The easiest solution uses // two nested loops! // Michael's solution: // This could also be done with while-loops, but the for-loops // seem more natural to me. Notice that I am printing a // heading across the top and down the left side: // Print the top heading: cout << " 0 1 2 3 4 5 6 7 8 9" << endl; cout << "__|------------------------------" << endl; // Each iteration of the i loop prints one row of the table: for (i = 0; i <= 9; ++i) { // Print the label on the left edge: cout << i << " |"; // Each iteration of the j loop prints one i*j: for (j = 0; j <= 9; ++j) { cout << setw(3) << i*j; } // Print the end of line number i: cout << endl; } // 4. Complete the C++ code using a pair of nested loops. initwindow(100, 100, "Lab Quiz"); // Write code that colors all the pixels on the screen // using the following formula for a pixel at coordinates // x (in the range 0 to 99) and y (in the range 0 to 99): // If x < 50, then the compute the color for the point (x,y) // as COLOR(x,50-y,y). If x >= 50, then compute the color for // the point (x,y) as COLOR(x,y-50,y). For each x and y, you // may call putpixel(x, y, ...) where the ... is the COLOR // formula shown above. for (x = 0; x < 100; ++x) { for (y = 0; y < 100; ++y) { if (x < 50) putpixel(x, y, COLOR(x, 50-y, y)); else putpixel(x, y, COLOR(x, y-50, y)); } } getch( ); return EXIT_SUCCESS; }