Home > other >  I need to print this shape in c but I am having issue with last row
I need to print this shape in c but I am having issue with last row

Time:11-27

image of shape

I have no issues with first 4 rows. 5th row is the problem. I am required to use loops but dont know how i am suppose to print 6 (-.*) with 0 spaces when all rows above follow a pattern.

CodePudding user response:

Something like this?

std::string repeat(std::string s, int n) {
  std::string repeat;
  for (int i = 0; i < n;   i)
    repeat  = s;
  return repeat;
}

int main()
{
    int MAX = 6;
    for (int i = 1; i < MAX;   i) {
        std::string padding(3*(MAX-i), ' ');

        std::cout << padding << repeat("-.*", i)  << std::endl;
    }
}

Live demo:

http://cpp.sh/4c3dn

CodePudding user response:

Something like this should work for you

for (int i = 1; i <= 6; i  ) {
    for (int j = 28 - i * 3; j >= 0; j--) {
        std::cout << " ";
    }
    for (int j = 0; j < i; j  ) {
        std::cout << "-.*";
    }
    std::cout << std::endl;
    if (i == 4) i  ;
}

Basically check when you are on the 4th row and just have it skip a row by incrementing your row index loop.

  • Related