// Copyright 1999 // College of Computer Science // Northeastern University Boston MA 02115 // This software may be used for educational purposes as long as this copyright // notice is retained at the top of all files // Should this software be modified, the words "Modified from Original" must be // included as a comment below this notice // All publication rights are retained. This software or its documentation may // not be published in any media either in whole or in part. /////////////////////////////////////////////////////////////////////////////// // Recitation 3: Using and writing counted loops // STUDENT NAME: ¥?¥?¥ // STUDENT ID: ¥?¥?¥ // DATE: ¥?¥?¥ /////////////////////////////////////////////////////////////////////////////// // The standard include files that include traditional C and C++ headers and // many other Core Tools headers ... see CHeaders.h for additional details #include "IOTools.h" #include "Graphics.h" #include "Random.h" // Enter project specific include files here as well as classes and functions // that you choose to define in the main shell rather than in separate files // prototypes void Exercise1(); void Exercise2(); void Exercise3(); void Exercise4(); void Exercise5(); void Exercise6(); void Exercise7(); void Exercise8(); void Exercise9(); void Exercise10(); void Exercise11(); void Exercise12(); void Exercise14(); // Recitation 3: EXERCISES // definitions void Exercise1(){ // Write a program that will print numbers n, n*n, and n * log(n) // for the values of n from 1 to 10 cout << "Print n, n*n, and n*log(n)" << endl; for (int n = 1; n <= 10; n++){ // count from one to ten cout << setw(2) << n << setw(5) << n * n ; cout << setw(11) << setprecision(8) << n * log(n) << endl; }; }; void Exercise2(){ // Write a program that will print the values of n and n! // for the values of n from 1 to 8 cout << "Print n and n!" << endl; int nfac = 1; // base of the compound product for (int n = 1; n <= 8; n++){ // count from one to eight nfac = nfac * n; // multiply the compound product by n cout << setw(2) << n << setw(8) << nfac << endl; }; }; void Exercise3(){ // Write a program that will simulate ten rolls of a die. To do this use RandomLong function. // Print the result of each roll SetRandomSeed(); cout << "Print ten rolls of a die" << endl; for (int n = 1; n <= 10; n++){ // count from one to ten cout << RandomLong(1, 6) << endl; // pick a random number between 1 and 6 }; }; void Exercise4(){ // Write the program that will simulate twenty tosses of a coin // and print 0 for heads, 1 for tails SetRandomSeed(); cout << "Toss a coin 20 times: 0 = heads, 1 = tails" << endl; for (int n = 1; n <= 20; n++){ cout << RandomLong(0, 1) << endl; }; }; void Exercise5(){ // Write a program that will count the number of tails in 100 tosses of a coin. SetRandomSeed(); cout << "Count tails in 100 tosses of a coin" << endl; int count = 0; // set up a counter for (int n = 1; n <= 100; n++){ // count from one to twenty count += RandomLong(0, 1); // add one it tail }; cout << "There were " << count << " tails in 100 tosses" << endl; }; void Exercise6(){ // Print one column of a multiplication table. // User types in the multiplier // Program prints the first ten multiples of the given multiplier int mul; // declare variable for the multiplier int result; // declare variable for the result // ask for user input; use multiplier 1 if no input given mul = RequestInt("Select a multiplier (1 to 9)", 1); for (int i = 1; i <= 10; i++){ // count from one to ten result = i * mul; cout << " i: " << setw(2) << i; cout << " result: " << setw(4) << result << endl; } // post-mortem cout << "End of multiplication table." << endl; }; void Exercise7(){ // Frame ten circles // Use random number generator to select the center (between 100 and 200) // Use random number generator to select the radius (between 10 and 50) cout << "Frame ten random circles" << endl; int x; // declare horizontal coordinate int y; // declare vertical coordinate int radius; // declare variable for radius ClearDrawing(); // clear the graphics window SetForeColor(255, 0, 0); // set the color to red for (int i = 1; i <= 10; i++){ // count from one to ten x = RandomLong(100, 200); // set value of horizontal coordinate y = RandomLong(100, 200); // set value of vertical coordinate radius = RandomLong(10, 90); // set value of variable for radius FrameCircle(x, y, radius); // frame the circle cout << " x: " << x; cout << " y: " << y; cout << " radius: " << radius << endl; } // post-mortem cout << "Ten circles done." << endl; }; void Exercise8(){ // Print the price of each purchase for five purchases // User types in number of items purchased and the price per item // Program prints the total for each set of items // For simplicity, all prices are in whole dollars cout << "Print the price for five purchases" << endl; cout << "Each time user enters number of items and the unit price" << endl; cout << "Total price of the purchase is not computed" << endl; int unitPrice; // declare variable for unit price int noItems; // declare variable for number of items int price; // declare variable for price for the set of items for (int i = 1; i <= 5; i++){ // count from one to five noItems = RequestInt("Enter number of items"); unitPrice = RequestInt("Enter price per item $"); price = noItems * unitPrice; cout << "$" << price << endl; } // post-mortem cout << "Thank you for shopping with us." << endl; }; void Exercise9(){ // Modify Exercise 1 so that the user first specifies how many sets of items will be purchased cout << "Print the price for purchases" << endl; cout << "User first specifies the number of purchases" << endl; cout << "Each time user enters number of items and the unit price" << endl; cout << "Total price of the purchase is not computed" << endl; int unitPrice; // declare variable for unit price int noItems; // declare variable for number of items int price; // declare variable for price for the set of items int count; // specifies how many different sets of items will be priced count = RequestInt("How many price requests?"); for (int i = 1; i <= count; i++){ // count from one to the value of count noItems = RequestInt("Enter number of items"); unitPrice = RequestInt("Enter price per item $"); price = noItems * unitPrice; cout << "$" << price << endl; } // post-mortem cout << "Thank you for shopping with us." << endl; }; void Exercise10(){ // Modify Exercise 4 so that it also computes the total price for the whole purchase cout << "Print the price for purchases" << endl; cout << "User first specifies the number of purchases" << endl; cout << "Each time user enters number of items and the unit price" << endl; cout << "Total price of the purchase IS computed" << endl; int unitPrice; // declare variable for unit price int noItems; // declare variable for number of items int price; // declare variable for price for the set of items int count; // specifies how many different sets of items will be priced int totalPrice; // variable to hold the running sum of purchase // and the total price at the end totalPrice = 0; // before any purchase is made the total is zero count = RequestInt("How many price requests?"); for (int i = 1; i <= count; i++){ // count from one to the value of count noItems = RequestInt("Enter number of items"); unitPrice = RequestInt("Enter price per item $"); price = noItems * unitPrice; cout << "$" << price << endl; totalPrice = totalPrice + price; // add the price of current set of items to the price } // post-mortem cout << "The total is $" << totalPrice << endl; cout << "Thank you for shopping with us." << endl; }; void Exercise11(){ // Paint ten circles in a row. All of them have the same size and touch each other. // User selects the radius of the circles and the location of the first circle cout << "There are four variations of this exercise" << endl; cout << "The program paints ten circles in a row adjacent to each other" << endl; cout << "In each case user selects the location of the first circle" << endl; cout << " and the radius of the circles." << endl; ClearDrawing(); int x; // declare horizontal coordinate int y; // declare vertical coordinate int radius; // declare variable for radius ClearDrawing(); // clear the graphics window SetForeColor(255, 0, 0); // set the color to red radius = RequestInt("Radius:"); // select the size of the radius cout << "Type in the coordinates of the center of the first circle" << endl; x = RequestInt("x:"); // initial horizontal coordinate of the center y = RequestInt("y:"); // initial vertical coordinate of the center // VERSION I // Count by ones and compute the coordinates of the i-th circle relative to the first one { cout << "Count by ones and compute the coordinates of the i-th circle"; cout << " relative to the first one" << endl; for (int i = 0; i < 10; i++){ // count from zero to nine // Compute horizontal coordinate of the next circle: // Each next circle is 2*radius to the right of the first one int xCenter = x + 2 * i * radius; // current horizontal coordinate of the center PaintCircle(xCenter, y, radius); // frame the circle // Print horizontal coordinate of the center of this circle cout << " x: " << xCenter << endl; } // post-mortem PressReturn("This was VERSION I"); } // END OF VERSION I // VERSION II // Count by ones and compute the coordinates of the i-th circle relative to the previous one { cout << "Count by ones and compute the coordinates of the i-th circle"; cout << " relative to the previous one" << endl; SetForeColor(0, 255, 0); // set the color to blue radius = RequestInt("Radius:", radius); // select the size of the radius cout << "Type in the coordinates of the center of the first circle" << endl; x = RequestInt("x:", x); // initial horizontal coordinate of the center y = RequestInt("y:", y); // initial vertical coordinate of the center int xCenter = x; // horizontal coordinate of circle to paint // initialized to the location of the first circle for (int i = 0; i < 10; i++){ // count from zero to nine // First paint the circle PaintCircle(xCenter, y, radius); // frame the circle // Print horizontal coordinate of the center of this circle cout << " x: " << xCenter << endl; // Compute the horizontal coordinate of the next cirle: // Next circle is 2*radius to the right of the previous one xCenter = xCenter + 2 * radius; } // post-mortem PressReturn("This was VERSION II"); } //END OF VERSION II // VERSION III // Use the horizontal coordinate of the circle as the counting variable // Start with xCenter = coordinate of the first circle // Increment by 2 * radius // Continue as long as the horizontal coordinate is less than 20 * radius off from the first center { SetForeColor(0, 0, 255); // set the color to green cout << "Use the horizontal coordinate of the circle as the counting variable"; radius = RequestInt("Radius:", radius); // select the size of the radius cout << "Type in the coordinates of the center of the first circle" << endl; x = RequestInt("x:", x); // initial horizontal coordinate of the center y = RequestInt("y:", y); // initial vertical coordinate of the center for (int xCenter = x; xCenter < x + 20 * radius; xCenter = xCenter + 2 * radius){ PaintCircle(xCenter, y, radius); // frame the circle // Print horizontal coordinate of the center of this circle cout << " x: " << xCenter << endl; } // post-mortem PressReturn("This was VERSION III"); } // END OF VERSION III }; void PartA() { // (a) Ask the user to type in the low and high end of the interval (in whole degrees) // and the increment. // For example, you may want to plot the values of sin(x) from 30 to 90 degrees // in 20 degree intervals. cout << "Ask the user to type in the low and high end of the interval, "; cout << "and the increment." << endl << endl; int low = RequestInt("Type in the lower bound:"); int high = RequestInt("Type in the upper bound:"); int delta = RequestInt("Type in the increment:"); cout << endl; // For parameters from 30 to 90 degrees in 20 degree intervals // angle will be: 30, 50, 70, 90 for (int angle = low; angle <= high; angle+= delta){ cout << "sin(" << setw(3) << angle << ") = " ; cout<< setw(10) << setprecision(7) << sindeg(angle) << endl; }; PressReturn("Part (a) completed."); }; void PartB(){ // (b) Ask the user to type in the low and the high end of the interval (in whole degrees) // and the number of values desired. // Note, that if the interval is divided into n segments you need to print n+1 values. cout << "Ask the user to type in the low and high end of the interval, "; cout << "and the number of segments into which the interval should be divided." << endl << endl; int low = RequestInt("Type in the lower bound:"); int high = RequestInt("Type in the upper bound:"); int num = RequestInt("Type in number of segments:"); cout << endl; // compute the increment delta as a real number double delta = (high - low)/ (double) num; // double type can be used as counting variable // For parameters from 30 to 90 degrees with three segments // delta will be 20, and the angle will be: 30, 50, 70, 90 for (double angle = low; angle <= high; angle+= delta){ cout << "sin(" << setw(3) << angle << ") = " ; cout<< setw(10) << setprecision(7) << sindeg(angle) << endl; }; PressReturn("Part (b) completed."); }; void PartC(){ // (c) Ask the user to type in the low end of the interval, the increment, and the number of segments. cout << "Ask the user to type in the low end of the interval, "; cout << "the increment, and the number of segments." << endl; cout << "Increment angle by delta" << endl << endl; int low = RequestInt("Type in the lower bound:"); int delta = RequestInt("Type in the increment:"); int num = RequestInt("Type in number of segments:"); cout << endl; int high = low + num * delta; // For parameters from 30 degrees with four segments and increment delta // the angle will be: 30, 50, 70, 90 for (int angle = low; angle <= high; angle+= delta){ cout << "sin(" << setw(3) << angle << ") = " ; cout<< setw(10) << setprecision(7) << sindeg(angle) << endl; }; PressReturn("Part (c) completed."); }; void PartCVariation(){ // (c) Ask the user to type in the low end of the interval, the increment, and the number of segments. // VARIATION cout << "Ask the user to type in the low end of the interval, "; cout << "the increment, and the number of segments." << endl; cout << "Compute angle from iteration count" << endl << endl; cout << endl; // count the segments and compute the angle each time int low = RequestInt("Type in the lower bound:"); int delta = RequestInt("Type in the increment:"); int num = RequestInt("Type in number of segments:"); // For parameters from 30 to 90 degrees with four segments // delta will be 20, and the angle will be: 30, 50, 70, 90 for (int i = 0; i <= num; i++){ // num values for num segments int angle = low + i * delta; cout << "sin(" << setw(3) << angle << ") = " ; cout<< setw(10) << setprecision(7) << sindeg(angle) << endl; }; PressReturn("Variation of Part (c) completed."); }; void Exercise12(){ // Print the values of the function sin(x) cout << "Print the values of the function sin(x)" << endl; PartA(); PartB(); PartC(); PartCVariation(); }; void Exercise14(){ // THIS IS A VARIATION OF EXERCISE 14!!! // Write a program that will request one number between 1 and 9 from the user // and will print the following pattern of stars // (user input determines the number of rows and the number of stars in the first row). // * // * * // * * * // * * * * // * * * * * int n = RequestInt("Select a number between 1 and 9: "); // user input for (int row = 1; row <= n; row++){ // count rows for (int col = 1; col <= row; col++){ // r stars in row r cout << "* "; }; cout << endl; }; }; int main(int argc, char* argv[]) { // Use the following line if you choose NOT to open any graphics windows // InitializeConsole(); // Build graphics window 0 GraphicsWindow GW0(300, 300); // Move the console below graphics window 0 ConsolePlaceBelow(0); // Give the console the focus for user interaction MakeConsoleForeground(); ////////// // Enter the main program here int i = 1; while (Confirm("Another exercise?", true)){ // loop to run another exercise i = RequestInt("Exercise number: ", i); // determine which exercise to run // the default moves you to the next one cout << endl << "Exercise " << i << endl; // notify user of the accepted selection switch (i){ // un the appropriate exercise case 1: { Exercise1(); break; } case 2: { Exercise2(); break; } case 3: { Exercise3(); break; } case 4: { Exercise4(); break; } case 5: { Exercise5(); break; } case 6: { Exercise6(); break; } case 7: { Exercise7(); break; } case 8: { Exercise8(); break; } case 9: { Exercise9(); break; } case 10: { Exercise10(); break; } case 11: { Exercise11(); break; } case 12: { Exercise12(); break; } case 14: { Exercise14(); break; } default: Exercise1(); }; i = (i+1); // next time, continue with the next exercise if (i > 14) // we only have 10 exercises - cycle through again i = 1; cout << endl; } ////////// // The lines below make sure that the graphics windows remain open just // before the program terminates PressReturn("\nThe main program is about to terminate\n"); return 0; }