1

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

****    ****
****   ****
****  ****
**** ****
Johnson
  • 21
  • 4
  • 1
    you're using `i` as the variable name in multiple loops. – dwcanillas Mar 10 '23 at 17:47
  • also, your top level declarations of `s` and `i` are unused. – dwcanillas Mar 10 '23 at 17:59
  • 1
    As for your issue, try and solve it yourself. work through the problem, read your code, and try to understand what it is currently doing vs. what you want it to do. A small hint would be that your inner loops (stars and spaces) should depend on your outer loop's (the line number) value. – dwcanillas Mar 10 '23 at 18:01
  • Okay, thank you for the hins. I will try to solve it myself :) – Johnson Mar 10 '23 at 18:09
  • Step 1: use an IDE. Don't write code on the command line. The IDE will help you with the formatting of the code. Step 2: rename the single letter varaiables. Give them meaningful names. The IDE will help you with renaming and with autocompletion. You will understand your own code much better if you use variables with proper names. Nobody will find `t` a useful name for `size`. – Thomas Weller Mar 10 '23 at 18:15
  • *And I don't know how to write the code to subtract one space in each line after printing the first line* -- If you take a look at both patterns of `K` (2 and 4), they follow the same general formula/pattern for each section of the `K`. You have 2/4 top lines, you have 2/4 middle sections of the same length, and you have 2/4 lower lines. The top lines get "squeezed" together on each row. The bottom lines "expand" away from each other for each row. All you have to do is write one set of code, with the only difference being the values `2` and `4`. – PaulMcKenzie Mar 10 '23 at 18:29
  • Another helpful tip might be to use the `std::string` constructor taking a size and char. This allows replacing many of the loops. – SebastianWilke Mar 11 '23 at 12:56

1 Answers1

1

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

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45