How to subtract space using for-loop in letter pattern?

65 Views Asked by At

I am new to c++, and I have a problem displaying a pattern of the letter k in c++.

The letter displays pattern depending on the input size. Here is the sample display of two different sizes of pattern:

sample display input size: 4

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

input size: 2

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

And I am now writing the upper part of the pattern of the letter K Here is my code:

#include <iostream>
using namespace std;

int main() {
  int s, t, i;
  cout << "Enter the size: ";
  cin >> t;

  if (t <= 0) {
    return 0;
  }
  for (int i = 1; i <= t; i++) {
  for (int i = 1; i <= t; i++) {
    cout << "*"
         << "";}
    for (int i = 1; i <= t; i++) {
    cout << " "
         << "";
  }
      for (int i = 1; i <= t; i++) {
      cout << "*"
          << "";
  }
  cout << endl;
}

  return 0;
}

If I input: the size: 4 In this code, its output :

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

And I don't know how to write the code to subtract one space in each line after printing the first line. Can someone help me, please?

I want the result of my upper part of letter pattern should look like this: Size: 4

****    ****
****   ****
****  ****
**** ****
1

There are 1 best solutions below

0
PaulMcKenzie On

As mentioned in the comments, you should break up the writing of the K into three logical operations. Also mentioned is using the std::string constructor that takes a character count and a single character can be used instead of so many cout statements, outputting a single character each time.

The three logical operations are as follows:

  1. Writing the top third of the letter 'K'
  2. Writing the middle section of the letter 'K'
  3. Writing the lower third of the letter 'K'

Note that each operation can be made generic if you look closely at the pattern that is associated with that section of the letter.

For the top-third, it has a pattern of:

N stars X spaces, N stars, where N is the number of stars, and X is the number of spaces. You see that X is reduced by one for each line, and there are a total of N lines.

If you were to write a function to do this, it would look something like this:

#include <iostream>
#include <string>

void drawTop(int nStars)
{
    std::string stars(nStars, '*');
    for (int spaceCount = nStars; spaceCount > 0; --spaceCount)
        std::cout << stars << std::string(spaceCount, ' ') << stars << "\n";
}

The nStars is the number of stars (in your first example, nStars would be 4), and the stars is just a string of nStars stars. The spaceCount is the number of spaces between the 4 stars. Note how spaceCount is reduced by 1 each time.

That function effectively writes the top third of the K.

For the middle portion, it is simply 2 * N stars, repeated for N lines.

void drawMiddle(int nStars)
{
    std::string s(nStars * 2, '*');
    for (int i = 0; i < nStars; ++i)
        std::cout << s << "\n";
}

For the bottom third, it is similar to the top-third of the K, except the number of spaces between the stars increases:'

void drawTop(int nStars)
{
    std::string stars(nStars, '*');
    for (int spaceCount = 1; spaceCount <= nStars; ++spaceCount)
        std::cout << stars << std::string(spaceCount, ' ') << stars << "\n";
}

Putting this all together, you get the final program:

#include <iostream>
#include <string>

void drawTop(int nStars)
{
    std::string stars(nStars, '*');
    for (int spaceCount = nStars; spaceCount > 0; --spaceCount)
        std::cout << stars << std::string(spaceCount, ' ') << stars << "\n";
}

void drawMiddle(int nStars)
{
    std::string s(nStars * 2, '*');
    for (int i = 0; i < nStars; ++i)
        std::cout << s << "\n";
}

void drawBottom(int nStars)
{
    std::string stars(nStars, '*');
    for (int spaceCount = 1; spaceCount <= nStars; ++spaceCount)
        std::cout << stars << std::string(spaceCount, ' ') << stars << "\n";
}

void drawAllStars(int nStars)
{
    drawTop(nStars);
    drawMiddle(nStars);
    drawBottom(nStars);
}

int main()
{
    drawAllStars(4);
    std::cout << "\n\n";
    drawAllStars(2);
    std::cout << "\n\n";
    drawAllStars(5);
    std::cout << "\n\n";
    drawAllStars(3);
}

Live Example