// 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. /////////////////////////////////////////////////////////////////////////////// // Project : Recitation 2 // STUDENT NAME: Yuhong Yin // Sequence Num: 8 // DATE: 01/24/99 /////////////////////////////////////////////////////////////////////////////// // 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 "RGBNames.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 // constant declaration const int QUARTER = 25; const int DIME = 10; const int NICKLE = 5; const int PENNEY = 1; // prototypes void Exercise23(); void MakeChange(int c, int& q, int& d, int& n,int& p); void MakeChange_2(int c, int& q, int& d, int& n,int& p); void MakeChange_3(int c, int& q, int& d, int& n,int& p); void FindRemain(int amount, int standard, int& quotient, int& remain); void MakeChange(int c, int& q, int& d, int& n,int& p) { int remain; remain = c; if (remain >= 25) { q = remain / 25; // find quarters remain = remain % 25; // find remains } if (remain >= 10) { d = remain / 10; // find dimes remain = remain % 10; // find remains } if (remain >= 5) { n = remain / 5; // find nickles remain = remain % 5; // find remains } p = remain; // all left will be pennies } void MakeChange_2(int c, int& q, int& d, int& n,int& p) { int remain; remain = c; if (remain >= QUARTER) { q = remain / QUARTER; // find quarters remain = remain % QUARTER; // find remains } if (remain >= DIME) { d = remain / DIME; // find dimes remain = remain % DIME; // find remains } if (remain >= NICKLE) { n = remain / NICKLE; // find nickles remain = remain % NICKLE; // find remains } p = remain; // all left will be pennies } void MakeChange_3(int c, int& q, int& d, int& n,int& p) { int remain; remain = c; FindRemain(remain, QUARTER, q, remain); FindRemain(remain, DIME, d, remain); FindRemain(remain, NICKLE, n, remain); p = remain; } void FindRemain(int amount, int standard, int& quotient, int& remain) { if (amount >= standard) { quotient = amount / standard; // find quotient remain = amount % standard; // find remains } } void Exercise23(){ // Read in amount // Print how many quarters, dimes, nickels and pennies // variable declaration // number of quarters int quarters, dimes, nickels, pennies; // variable initialization quarters = dimes = nickels = pennies = 0; // ask user to type in cents int amount = RequestInt("Enter amount ( 1 ~ 99) :"); // function call MakeChange(amount, quarters, dimes, nickels, pennies); // print out the results cout << "You got " << quarters << " quaters" << endl; cout << " " << dimes << " dimes" << endl; cout << " " << nickels << " nickles" << endl; cout << " " << pennies << " pennies" << 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 = 23; 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 23: { Exercise23(); break; } default: Exercise23(); }; i = (i+1); // next time, // continue with the next exercise if (i > 10) // we only have 10 exercises i = 1; // - cycle through again cout << endl; } // done ////////// // 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; }