Untitled
By: a guest | Mar 17th, 2010 | Syntax:
C++ | Size: 1.80 KB | Hits: 33 | Expires: Never
#include <iostream>
#include <cmath>
using namespace std;
//Rewrite your trapezoid program from Assignment 4 to incorporate the following functions:
//void drawTrapezoid (int numRows, int topWidth, char fill);
//void drawRow(int offset, int numChars, char fill); // offset = number of leading blanks, numChars = number of fill characters to print
//main() calls drawTrapezoid() which in turn calls drawRow().
//hint: drawrow(4,8,'*') would produce this output (including four leading spaces and an endl):
// ********
void drawRow(int offset, int numChars, char fill)
{
for (int i = 0; i < offset; i++)
cout << " ";
for (int i = 0; i < numChars; i++)
cout << fill;
cout <<endl;
}
void drawTrapezoid (int numRows, int topWidth, char fill)
{
int numChars = topWidth, numSpaces = numRows - 1;
for (int line = 1; line <= numRows; line++) {
drawRow(numSpaces,numChars,fill);
numSpaces--;
numChars +=2;
}
}
int main() {
int topWidth, height;
const int MAX_WIDTH = 80;
char fill;
bool keepPlaying = true;
while (keepPlaying) {
cout << "Please type in the top width: ";
cin >> topWidth;
cout << "Please type in the height: ";
cin >> height;
cout << "Please type in the character: ";
cin >> fill;
topWidth = min(topWidth,MAX_WIDTH);
height = min(height, (MAX_WIDTH - topWidth + 2) / 2); // limit height
drawTrapezoid(height,topWidth,fill);
cout << "Play again ('y' or 'n')? ";
char response;
cin >> response;
keepPlaying = response == 'y' || response == 'Y';
}
cout << "Goodbye!" << endl;
return 0;
}