Home > Backend Development > C++ > body text

Mastering Hollow Patterns: A Comprehensive Guide with Code Examples

WBOY
Release: 2024-07-16 19:00:58
Original
569 people have browsed it

Mastering Hollow Patterns: A Comprehensive Guide with Code Examples

Welcome to our comprehensive guide on creating various hollow patterns using loops in C programming! In this tutorial, we'll walk through step-by-step instructions on how to draw 18 different hollow patterns. These patterns range from basic shapes like squares and triangles to more complex forms like diamonds, hexagons, and pentagons. Each pattern is created using nested loops, making it an excellent exercise for beginners to practice control structures in C. Let's dive in!

You can find all the code in our GitHub repository.

Table of Contents

  1. Introduction to Nested Loops
  2. Hollow Square
  3. Hollow Right Triangle
  4. Hollow Inverted Right Triangle
  5. Hollow Right Aligned Triangle
  6. Hollow Right Aligned Inverted Triangle
  7. Hollow Right Pascal Triangle
  8. Hollow Left Pascal Triangle
  9. Hollow Equilateral Triangle
  10. Hollow Inverted Equilateral Triangle
  11. Hollow Pyramid
  12. Hollow Inverted Pyramid
  13. Hollow Diamond
  14. Hollow Hourglass
  15. Hollow Rhombus
  16. Hollow Parallelogram
  17. Hollow Hexagon
  18. Hollow Pentagon
  19. Hollow Inverted Pentagon
  20. Conclusion

Introduction to Nested Loops

Before we start with the patterns, it’s essential to understand the concept of nested loops. A nested loop is a loop inside another loop. This structure is particularly useful for handling multi-dimensional arrays and for generating patterns. In C, a typical nested loop structure looks like this:

for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        // Code to execute
    }
}
Copy after login

Hollow Square

Explanation:

  • The hollow square pattern consists of n rows and n columns.
  • Characters are printed only at the borders (first row, last row, first column, and last column).
int n = 5; // size of the square
char ch = '*';

printf("1. Hollow Square:\n");
for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        if (i == 0 || i == n - 1 || j == 0 || j == n - 1) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
Copy after login

Output:

*  *  *  *  *  
*           *  
*           *  
*           *  
*  *  *  *  *  
Copy after login

Hollow Right Triangle

Explanation:

  • The hollow right triangle pattern starts with one character in the first row and increases by one character in each subsequent row.
  • Characters are printed only at the borders (first row, last row, and the diagonal).
printf("2. Hollow Right Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = 0; j < i + 1; j++) {
        if (i == n - 1 || j == 0 || j == i) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
Copy after login

Output:

*  
*  *  
*     *  
*        *  
*  *  *  *  *  
Copy after login

Hollow Inverted Right Triangle

Explanation:

  • The hollow inverted right triangle pattern starts with n characters in the first row and decreases by one character in each subsequent row.
  • Characters are printed only at the borders (first row, last row, and the diagonal).
printf("3. Hollow Inverted Right Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = n; j > i; j--) {
        if (i == 0 || j == n || j == i + 1) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
Copy after login

Output:

*  *  *  *  *  
*        *  
*     *  
*  *  
*  
Copy after login

Hollow Right Aligned Triangle

Explanation:

  • The hollow right aligned triangle pattern is similar to the hollow right triangle, but the triangle is right-aligned.
  • Characters are printed only at the borders (first row, last row, and the diagonal).
printf("4. Hollow Right Aligned Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j--) {
        printf("   ");
    }
    for (int j = 0; j < i + 1; j++) {
        if (i == n - 1 || j == 0 || j == i) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
Copy after login

Output:

            *  
         *  *  
      *     *  
   *        *  
*  *  *  *  *  
Copy after login

Hollow Right Aligned Inverted Triangle

Explanation:

  • The hollow right aligned inverted triangle pattern is the opposite of the hollow right aligned triangle.
  • It starts with n characters in the first row and decreases by one character in each subsequent row, but the triangle is right-aligned.
printf("5. Hollow Right Aligned Inverted Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = 1; j < i + 1; j++) {
        printf("   ");
    }
    for (int j = n; j > i; j--) {
        if (i == 0 || j == n || j == i + 1) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
Copy after login

Output:

*  *  *  *  *  
   *        *  
      *     *  
         *  *  
            *  
Copy after login

Hollow Right Pascal Triangle

Explanation:

  • The hollow right Pascal triangle pattern combines the right triangle and the inverted right triangle to form a Pascal-like triangle.
  • Characters are printed only at the borders (first row, last row, and the diagonals).
printf("6. Hollow Right Pascal Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = 0; j < i + 1; j++) {
        if (j == 0 || j == i) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
for (int i = 0; i < n; i++) {
    for (int j = n; j > i + 1; j--) {
        if (j == n || j == i + 2) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
Copy after login

Output:

*  
*  *  
*     *  
*        *  
*           *  
*        *  
*     *  
*  *  
*  
Copy after login

Hollow Left Pascal Triangle

Explanation:

  • The hollow left Pascal triangle pattern is similar to the hollow right Pascal triangle, but it is left-aligned.
  • Characters are printed only at the borders (first row, last row, and the diagonals).
printf("7. Hollow Left Pascal Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j--) {
        printf("   ");
    }
    for (int j = 0; j < i + 1; j++) {
        if (j == 0 || j == i) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}

for (int i = 0; i < n; i++) {
    for (int j = 0; j < i + 1; j++) {
        printf("   ");
    }
    for (int j = n - 1; j > i; j--) {
        if (j == n

 - 1 || j == i + 1) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
Copy after login

Output:

            *  
         *  *  
      *     *  
   *        *  
*           *  
   *        *  
      *     *  
         *  *  
            *  
Copy after login

Hollow Equilateral Triangle

Explanation:

  • The hollow equilateral triangle pattern is symmetrical and centered.
  • Characters are printed only at the borders (first row, last row, and the diagonals).
printf("8. Hollow Equilateral Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j--) {
        printf("   ");
    }
    for (int j = 0; j < 2 * i + 1; j++) {
        if (j == 0 || j == 2 * i || i == n - 1) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
Copy after login

Output:

            *  
         *     *  
      *           *  
   *                 *  
*  *  *  *  *  *  *  *  * 
Copy after login

Hollow Inverted Equilateral Triangle

Explanation:

  • The hollow inverted equilateral triangle pattern is the opposite of the hollow equilateral triangle.
  • Characters are printed only at the borders (first row, last row, and the diagonals).
printf("9. Hollow Inverted Equilateral Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = 0; j < i; j++) {
        printf("   ");
    }
    for (int j = 2 * n - 1; j > 2 * i; j--) {
        if (j == 2 * n - 1 || j == 2 * i + 1 || i == 0) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
Copy after login

Output:

*  *  *  *  *  *  *  *  *  
   *                 *  
      *           *  
         *     *  
            *  
Copy after login

Hollow Pyramid

Explanation:

  • The hollow pyramid pattern is centered and symmetrical.
  • Characters are printed only at the borders (first row, last row, and the diagonals).
printf("10. Hollow Pyramid:\n");
for (i = 0; i < n; i++) {
    for (j = n - 1; j > i; j--) {
       printf(" ");
    }
    for (j = 0; j < (2 * i + 1); j++) {
        if (i == n - 1 || j == 0 || j == i * 2 ) {
            printf("%c", ch);
        } else {
            printf(" ");
        }
    }
    printf("\n"); 
}
Copy after login

Output:

    *
   * *
  *   *
 *     *
********* 
Copy after login

Hollow Inverted Pyramid

Explanation:

  • The hollow inverted pyramid pattern is the opposite of the hollow pyramid.
  • Characters are printed only at the borders (first row, last row, and the diagonals).
printf("11. Hollow Inverted Pyramid:\n");
for (i = n; i > 0; i--) {
    for (j = n - i; j > 0; j--) {
        printf(" ");
    }
    for (j = 0; j < (2 * i - 1); j++) {
        if (j == 0 || i == n  || j == (i-1) * 2 ) {
            printf("%c", ch);
        } else {
            printf(" ");
        }
    }
  printf("\n");
}
Copy after login

Output:

*********
 *     *
  *   *
   * *
    * 
Copy after login

Hollow Diamond

Explanation:

  • The hollow diamond pattern is symmetrical and centered.
  • It consists of a hollow upper and lower triangle.
printf("12. Hollow Diamond:\n");
for (i = 0; i < n; i++) {
    for (j = n - 1; j > i; j--) {
        printf(" ");
    }
    for (j = 0; j < i + 1; j++) {
        if (j == 0 || j == i) {
            printf("%c ", ch);
        } else {
            printf("  ");
        }
    }
    printf("\n");
}
for (i = 0; i < n; i++) {
    for (j = 0; j < i + 1; j++) {
        printf(" ");
    }
    for (j = n - 1; j > i; j--) {
        if (j == n - 1 || j == i + 1) {
            printf("%c ", ch);
        } else {
            printf("  ");
        }
    }
    printf("\n");
}
Copy after login

Output:

    * 
   * * 
  *   * 
 *     * 
*       * 
 *     * 
  *   * 
   * * 
    * 
Copy after login

Hollow Hourglass

Explanation:

  • The hollow hourglass pattern is symmetrical and centered.
  • It consists of a hollow upper and lower inverted triangle.
printf("13. Hollow Hourglass:\n");
for (i = 0; i < n; i++) {
    for (j = 0; j < i; j++) {
        printf(" ");
    }
    for (j = 0; j < (n - i) ; j++) {
        if (j == 0 || i == 0  || j ==  n - i - 1) {
            printf("%c ", ch);
        } else {
            printf("  ");
        }
    }
    printf("\n");
}
for (i = 1; i < n; i++) {
    for (j = n - 1; j > i; j--) {
       printf(" ");
    }
    for (j = 0; j < (i + 1); j++) {
       if (i == n - 1 || j == 0 || j == i) {
           printf("%c ", ch);
       } else {
           printf("  ");
       }
     }
     printf("\n");
}
Copy after login

Output:

* * * * * 
 *     * 
  *   * 
   * * 
    * 
   * * 
  *   * 
 *     * 
* * * * * 
Copy after login

Hollow Rhombus

Explanation:

  • The hollow rhombus pattern is symmetrical and centered.
  • Characters are printed only at the borders.
printf("14. Hollow Rhombus:\n");
for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j--) {
        printf("   ");
    }
    for (int j = 0; j < n; j++) {
        if (i == 0 || i == n - 1 || j == 0 || j == n - 1) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
Copy after login

Output:

            *  *  *  *  *  
         *           *  
      *           *  
   *           *  
*  *  *  *  *  
Copy after login

Hollow Parallelogram

Explanation:

  • The hollow parallelogram pattern is symmetrical and slanted to one side.
  • Characters are printed only at the borders.
printf("15. Hollow Parallelogram:\n");
for (i = 0; i < n; i++) {
    for (j = 0; j < i; j++) {
        printf(" ");
    }
    for (j = 0; j < n * 2; j++) {
        if (i == n - 1 || i == 0 || j == 0 || j == n * 2 - 1) {
            printf("%c ", ch);
        } else {
            printf("  ");
        }
    }
    printf("\n");
}
Copy after login

Output:

* * * * * * * * * * 
 *                 * 
  *                 * 
   *                 * 
    * * * * * * * * * *  
Copy after login

Hollow Hexagon

Explanation:

  • The hollow hexagon pattern consists of a combination of upper and lower triangles and a middle section.
  • Characters are printed only at the borders.
printf("16. Hollow Hexagon:\n");
for (i = 0; i < n / 2; i++) {
    for (j = n / 2 - i; j > 0; j--) {
        printf(" ");
    }
    for (j = 0; j < n + 1 * i; j++) {
        if ( i == 0 || j == 0 || j == n * i) {
           printf("%c ", ch);
        } else {
           printf("  ");
        }
    }
    printf("\n");
}
for (i = n / 2; i >= 0; i--) {
    for (j = 0; j < n / 2 - i; j++) {
        printf(" ");
    }
    for (j = 0; j < n + i; j++) {
        if (i == n - 1 || i == 0 || j == 0 || j == n + i - 1) {
            printf("%c ", ch);
        } else {
            printf("  ");
        }
    }
    printf("\n");
}
Copy after login

Output:

  * * * * * 
 *         * 
*           * 
 *         * 
  * * * * * 
Copy after login

Hollow Pentagon

Explanation:

  • The hollow pentagon pattern consists of an upper triangle and a lower rectangle.
  • Characters are printed only at the borders.
printf("17. Hollow Pentagon:\n");
for (i = 0; i < n+1; i++) {
    for (j = n ; j > i; j--) {
        printf(" ");
    }
    for (j = 0; j < (i + 1); j++) {
        if ( j == 0 || i == 0 || j == i ) {
            printf(" %c", ch);
        } else {
            printf("  ");
        }
    }
    printf("\n");
}
for (i = n / 2; i >= 0; i--) {
    for (j = 0; j < n / 2 - i; j++) {
        printf(" ");
    }
    for (j = 0; j < n +  i; j++) {
        if (i == n - 1 || i == 0 || j == 0 || j == n + i - 1) {
            printf("%c ", ch);
        } else {
            printf("  ");
        }
    }
    printf("\n");
}
Copy after login

Output:

      *
     * *
    *   *
   *     *
  *       *
 *         *
*           * 
 *         * 
  * * * * * 
Copy after login

Hollow Inverted Pentagon

Explanation:

  • The hollow inverted pentagon pattern consists of an upper inverted triangle and a lower inverted rectangle.
  • Characters are printed only at the borders.
printf("18. Hollow Inverted Pentagon:\n");
for (int i = 0; i <= n / 2; i++) {
    for (int j = 0; j < n / 2 - i; j++) {
        printf(" ");
    }
    for (int j = 0; j < n + i; j++) {
        if (i == n - 1 || i == 0 || j == 0 || j == n + i - 1) {
            printf("%c ", ch);
        } else {
            printf("  ");
        }
    }
    printf("\n");
}
for (int i = n + 1; i > 0; i--) {
    for (int j = n + 2; j > i; j--) {
        printf(" ");
    }
    for (int j = 0; j < i; j++) {
        if ( j == 0 || j == i - 1) {
            printf("%c ", ch);
        } else {
            printf("  ");
        }
    }
    printf("\n");
}
Copy after login

Output:

  * * * * * 
 *         * 
*           * 
 *         * 
  *       * 
   *     * 
    *   * 
     * * 
      *  
Copy after login

Conclusion

In conclusion, we have explored a variety of patterns using loops and conditional statements in C, each producing different geometric shapes and designs. These patterns include solid and hollow variants of squares, triangles, pyramids, diamonds, hourglasses, rhombuses, parallelograms, hexagons, and pentagons. Understanding and implementing these patterns helps to strengthen programming logic, loop constructs, and conditionals, which are fundamental concepts in computer science.

By practicing these patterns, you can enhance your problem-solving skills and improve your ability to visualize and implement complex patterns in code. These exercises also provide a solid foundation for more advanced programming tasks and algorithms.

The above is the detailed content of Mastering Hollow Patterns: A Comprehensive Guide with Code Examples. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!