[2000 character limit] In this question you are going write another program that prints a pyramid similar to that in Q4, but in negative space: Imagine you have a rectangle drawn by +'s and cut a hole of the size of the pyramid, leaving each side with just one + character (see sample input/outputs for details).

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter5: Repetition Statements
Section5.4: For Loops
Problem 8E
icon
Related questions
Question
[2000 character limit] In this question you are going
write another program that prints a pyramid similar to
that in Q4, but in negative space: Imagine you have a
rectangle drawn by +'s and cut a hole of the size of the
pyramid, leaving each side with just one + character
(see sample input/outputs for details).
Transcribed Image Text:[2000 character limit] In this question you are going write another program that prints a pyramid similar to that in Q4, but in negative space: Imagine you have a rectangle drawn by +'s and cut a hole of the size of the pyramid, leaving each side with just one + character (see sample input/outputs for details).
Expert Solution
Step 1

Program:-

#include <stdio.h>
int main() {
   int i, space, rows, k = 0;
   printf("Enter the number of rows: ");
   scanf("%d", &rows);
   if(rows<=0){
       printf("Invalid base size:Has to be positive int\n");
       return 0;
   }
   for (i = 0; i <= rows+1; ++i, k = 0) {
       //to print first and last lines
      if(i==0 || i==rows+1){
          for(int j=0;j<(2*rows)+1;j++){
              printf("+");
          }
          printf("\n");
          continue;
      }
      //to print first half '+'
      for (space = 0; space <= rows - i; ++space) {
         printf("+");
      }
      //to print spaces
      while (k != 2 * i - 1) {
         printf(" ");
         ++k;
      }
      //to print second half '+'
      for (space = 0; space <= rows - i; ++space) {
         printf("+");
      }
      printf("\n");
   }
   return 0;
}

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Functions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning