开发者

C++ program: Printing a trapezoid using input redirection

开发者 https://www.devze.com 2023-02-20 02:05 出处:网络
I\'m trying to print a trapezoid, but for some reason it\'s either I keep getting a triangle, or the size of my trapezoid is wrong. Can someone please tell me what\'s wrong or what\'s missing? Your he

I'm trying to print a trapezoid, but for some reason it's either I keep getting a triangle, or the size of my trapezoid is wrong. Can someone please tell me what's wrong or what's missing? Your help is greatly appreciated. Is there a way I can input my own values for the top row/base, and the height? I input my height, and I don't get开发者_高级运维 right height.

for (i = 1; i < trapeH; i++)
{
    for (k = spacesT-1; k > 0; k--)
    {
        cout << " ";
    }

    spacesT = spacesT - 1;

    for (j = 1; j <= (2*i + 1); j++)
    {
        cout << "*";
    }

    cout << endl;
}


for (j=1; j <=(2*i+1); j++)

This line guarantees your trapezoid will always start at width 3 and grow by two *'s each line. If you want a variable width trapezoid, you need to introduce an additional variable.

But as MacGucky pointed out, you do have a trapezoid.

EDIT:

The height of your trapezoid will be trapeH - 1. If you set trapeH = 10, you'll go through the loop 9 times since i = 10 will cause the loop to quit.


What are your input-values for trapeH and spacesT?

I used 10 for both and got following output:

         ***
        *****
       *******
      *********
     ***********
    *************
   ***************
  *****************
 *******************

So it seems to work - it's an trapezoid


Here is the corrected source there your parameters are the height and the width of the top row:

void trapezoid (int width, int height)
{
   for (int row = 0; row < height; ++row) {
      for (int col = height - row; col > 0; --col) {
         cout << " ";
      }
      for (int col = 0; col < (width + 2 * row); ++col) {
         cout << "*";
      }
      cout << endl;
   }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消