Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 17th, 2010 | Syntax: C++ | Size: 1.80 KB | Hits: 33 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. //Rewrite your trapezoid program from Assignment 4 to incorporate the following functions:
  6. //void drawTrapezoid (int numRows, int topWidth, char fill);
  7. //void drawRow(int offset, int numChars, char fill);  // offset = number of leading blanks, numChars = number of fill characters to print
  8. //main() calls drawTrapezoid() which in turn calls drawRow().
  9. //hint: drawrow(4,8,'*') would produce this output (including four leading spaces and an endl):
  10. //    ********
  11.  
  12.  
  13.  
  14. void drawRow(int offset, int numChars, char fill)
  15. {
  16.  
  17.         for (int i = 0; i < offset; i++)
  18.  
  19.             cout << " ";
  20.  
  21.         for (int i = 0; i < numChars; i++)
  22.  
  23.             cout << fill;
  24.  
  25.         cout <<endl;
  26.  
  27. }
  28.  
  29. void drawTrapezoid (int numRows, int topWidth, char fill)
  30. {
  31. int numChars = topWidth, numSpaces = numRows - 1;
  32.  
  33. for (int line = 1; line <= numRows; line++) {
  34.  
  35.                         drawRow(numSpaces,numChars,fill);
  36.  
  37.  
  38.             numSpaces--;
  39.  
  40.             numChars +=2;
  41.  
  42.                         }
  43.  
  44.  
  45. }
  46.  
  47. int main() {
  48.     int  topWidth, height;
  49.     const int MAX_WIDTH = 80;
  50.     char fill;
  51.     bool keepPlaying = true;
  52.     while (keepPlaying) {
  53.  
  54.         cout << "Please type in the top width: ";
  55.         cin >> topWidth;
  56.  
  57.         cout << "Please type in the height: ";
  58.         cin >> height;
  59.  
  60.         cout << "Please type in the character: ";
  61.         cin >> fill;
  62.  
  63.         topWidth = min(topWidth,MAX_WIDTH);
  64.  
  65.         height =  min(height, (MAX_WIDTH - topWidth + 2) / 2); // limit height
  66.  
  67.        
  68.  
  69.                 drawTrapezoid(height,topWidth,fill);
  70.  
  71.        
  72.         cout << "Play again ('y' or 'n')? ";
  73.  
  74.         char response;
  75.        
  76.                 cin >> response;
  77.        
  78.  
  79.                 keepPlaying = response == 'y' || response == 'Y';
  80.     }
  81.  
  82.     cout << "Goodbye!" << endl;
  83.  
  84.     return 0;
  85. }