”工欲善其事,必先利其器。“—孔子《论语.录灵公》
首页 > 编程 > 掌握填充模式:带有代码示例的综合指南

掌握填充模式:带有代码示例的综合指南

发布于2024-08-22
浏览:940

Mastering Filled Patterns: A Comprehensive Guide with Code Examples

Welcome to our comprehensive guide on creating various filled patterns using loops in C programming! In this tutorial, we'll walk through step-by-step instructions on how to draw 18 different filled 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. Filled Square
  3. Filled Right Triangle
  4. Filled Inverted Right Triangle
  5. Filled Right Aligned Triangle
  6. Filled Right Aligned Inverted Triangle
  7. Filled Right Pascal Triangle
  8. Filled Left Pascal Triangle
  9. Filled Equilateral Triangle
  10. Filled Inverted Equilateral Triangle
  11. Filled Pyramid
  12. Filled Inverted Pyramid
  13. Filled Diamond
  14. Filled Hourglass
  15. Filled Rhombus
  16. Filled Parallelogram
  17. Filled Hexagon
  18. Filled Pentagon
  19. Filled 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 





Filled Square

Explanation:

  • The filled square pattern is one of the simplest patterns to create.
  • It consists of n rows and n columns, where each cell contains the same character.
  • We use two nested loops to iterate over each row and column, printing the character in each cell.
int n = 5; // size of the square
char ch = '*';

printf("1. Filled Square:\n");
for (int i = 0; i 



Output:

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

Filled Right Triangle

Explanation:

  • The filled right triangle pattern starts with one character in the first row and increases by one character in each subsequent row.
  • This pattern is achieved by using two nested loops. The outer loop controls the number of rows, and the inner loop controls the number of characters printed in each row.
printf("2. Filled Right Triangle:\n");
for (int i = 0; i 



Output:

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

Filled Inverted Right Triangle

Explanation:

  • The filled inverted right triangle pattern is the opposite of the filled right triangle.
  • It starts with n characters in the first row and decreases by one character in each subsequent row.
  • Similar to the filled right triangle, this pattern is created using two nested loops.
printf("3. Filled Inverted Right Triangle:\n");
for (int i = 0; i  i; j--) {
        printf("%c  ", ch);
    }
    printf("\n");
}

Output:

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

Filled Right Aligned Triangle

Explanation:

  • The filled right aligned triangle pattern is similar to the filled right triangle, but the triangle is right-aligned.
  • This pattern is achieved by adding spaces before each row, creating a right-aligned appearance.
printf("4. Filled Right Aligned Triangle:\n");
for (int i = 0; i  i; j--) {
        printf("   ");
    }
    for (int j = 0; j 



Output:

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

Filled Right Aligned Inverted Triangle

Explanation:

  • The filled right aligned inverted triangle pattern is the opposite of the filled right aligned triangle.
  • It starts with one character in the first row and increases by one character in each subsequent row, but the triangle is right-aligned.
printf("5. Filled Right Aligned Inverted Triangle:\n");
for (int i = 0; i  i; j--) {
        printf("%c  ", ch);
    }
    printf("\n");
}

Output:

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

Filled Right Pascal Triangle

Explanation:

  • The filled right Pascal triangle pattern combines the right triangle and the inverted right triangle to form a Pascal-like triangle.
  • The first half of the pattern is similar to the filled right triangle, and the second half is similar to the filled inverted right triangle.
printf("6. Filled Right Pascal Triangle:\n");
for (int i = 0; i  i   1; j--) {
        printf("%c  ", ch);
    }
    printf("\n");
}

Output:

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

Filled Left Pascal Triangle

Explanation:

  • The filled left Pascal triangle pattern is similar to the filled right Pascal triangle, but it is left-aligned.
  • The first half of the pattern is similar to the filled right aligned triangle, and the second half is similar to the filled right aligned inverted triangle.
printf("7. Filled Left Pascal Triangle:\n");
for (int i = 0; i  i; j--) {
        printf("   ");
    }
    for (int j = 0; j  i; j--) {
        printf("%c  ", ch);
    }
    printf("\n");
}

Output:

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

Filled Equilateral Triangle

Explanation:

  • The filled equilateral triangle pattern has a symmetrical shape with each row centered.
  • To achieve this, we print spaces before each row to center the characters.
printf("8. Filled Equilateral Triangle:\n");
for (int i = 0; i  i; j--) {
        printf(" ");
    }
    for (int j = 0; j 



Output:

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

Filled Inverted Equilateral Triangle

Explanation:

  • The filled inverted equilateral triangle pattern is the inverted version of the filled equilateral triangle.
  • It starts with n characters at the base and decreases by one character per row, centered.
printf("9. Filled Inverted Equilateral Triangle:\n");
for (int i = n - 1; i >= 0; i--) {
    for (int j = n - 1; j > i; j--) {
        printf(" ");
    }
    for (int j = 0; j 



Output:

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

Filled Pyramid

Explanation:

  • The filled pyramid pattern starts with one character at the top and increases by two characters per row, forming a symmetrical pyramid.
  • We use spaces to center each row.
printf("10. Filled Pyramid:\n");
for (int i = 0; i  i; j--) {
        printf(" ");
    }
    for (int j = 0; j 



Output:

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

Filled Inverted Pyramid

Explanation:

  • The filled inverted pyramid pattern is the opposite of the filled pyramid.
  • It starts with 2 * n - 1 characters at the top and decreases by two characters per row, centered.
printf("11. Filled Inverted Pyramid:\n");
for (int i = n; i > 0; i--) {
    for (int j = n - i; j > 0; j--) {
        printf(" ");
    }
    for (int j = 0; j 



Output:

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

Filled Diamond

Explanation:

  • The filled diamond pattern is formed by combining the filled equilateral triangle and the filled inverted equilateral triangle.
  • It creates a symmetrical diamond shape.
printf("12. Filled Diamond:\n");
for (int i = 0; i  i; j--) {
        printf(" ");
    }
    for (int j = 0; j  i; j--) {
        printf("%c ", ch);
    }
    printf("\n");
}

Output:

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

Filled Hourglass

Explanation:

  • The filled hourglass pattern combines an inverted equilateral triangle and an equilateral triangle, forming an hourglass shape.
  • Each row is centered by adding spaces.
printf("13. Filled Hourglass:\n");
for (int i = 0; i  i; j--) {
        printf(" ");
    }
    for (int j = 0; j 



Output:

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

Filled Rhombus

Explanation:

  • The filled rhombus pattern consists of rows where each row is shifted to the right by spaces.
  • This creates a diamond-like shape with equal length sides.
printf("14. Filled Rhombus:\n");
for (int i = 0; i 



Output:

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

Filled Parallelogram

Explanation:

  • The filled parallelogram pattern is created by shifting each row to the right.
  • It looks like a rectangle leaning to one side.
printf("15. Filled Parallelogram:\n");
for (int i = 0; i 



Output:

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

Filled Hexagon

Explanation:

  • The filled hexagon pattern has a wider middle section, with each row increasing and then decreasing in width.
  • This creates a hexagonal shape.
printf("16. Filled Hexagon:\n");
for (int i = 0; i  0; j--) {
        printf(" ");
    }
    for (int j = 0; j = 0; i--) {
    for (int j = 0; j 



Output:

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

Filled Pentagon

Explanation:

  • The filled pentagon pattern starts with one character at the top and increases, forming a wider base.
  • This creates a pentagon-like shape.
printf("17. Filled Pentagon:\n");
for (int i = 0; i  i; j--) {
        printf(" ");
    }
    for (int j = 0; j = 0; i--) {
    for (int j = 0; j 



Output:

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

Filled Inverted Pentagon

Explanation:

  • The filled inverted pentagon pattern is the inverted version of the filled pentagon.
  • It starts with the wider base and decreases, forming an inverted pentagon shape.
printf("18. Filled Inverted Pentagon:\n");
for (int i = 0; i  0; i--) {
    for (int j = n   2; j > i; j--) {
        printf(" ");
    }
    for (int j = 0; j 



Output:

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

Conclusion

Learning to create these filled patterns in C is an excellent way to practice using nested loops and enhance your understanding of how loops work. By experimenting with different values and shapes, you can deepen your understanding of control structures in C and develop a keen eye for detail and logic. Whether you're a beginner or looking to brush up on your skills, these patterns provide a solid foundation for mastering loops in C programming.

We hope this guide has been helpful and encourages you to explore more complex patterns and designs. Happy coding!

For more tutorials and coding tips, be sure to subscribe to our blog and follow us on social media!

版本声明 本文转载于:https://dev.to/jitheshpoojari/mastering-c-programming-drawing-filled-patterns-with-loops-4def如有侵犯,请联系[email protected]删除
最新教程 更多>
  • 在JavaScript中如何并发运行异步操作并正确处理错误?
    在JavaScript中如何并发运行异步操作并正确处理错误?
    同意操作execution 在执行asynchronous操作时,相关的代码段落会遇到一个问题,当执行asynchronous操作:此实现在启动下一个操作之前依次等待每个操作的完成。要启用并发执行,需要进行修改的方法。 第一个解决方案试图通过获得每个操作的承诺来解决此问题,然后单独等待它们: co...
    编程 发布于2025-05-21
  • Java是否允许多种返回类型:仔细研究通用方法?
    Java是否允许多种返回类型:仔细研究通用方法?
    在Java中的多个返回类型:一种误解类型:在Java编程中揭示,在Java编程中,Peculiar方法签名可能会出现,可能会出现,使开发人员陷入困境,使开发人员陷入困境。 getResult(string s); ,其中foo是自定义类。该方法声明似乎拥有两种返回类型:列表和E。但这确实是如此吗...
    编程 发布于2025-05-21
  • Python不会对超范围子串切片报错的原因
    Python不会对超范围子串切片报错的原因
    在python中用索引切片范围:二重性和空序列索引单个元素不同,该元素会引起错误,切片在序列的边界之外没有。这种行为源于索引和切片之间的基本差异。索引一个序列,例如“示例” [3],返回一个项目。但是,切片序列(例如“示例” [3:4])返回项目的子序列。索引不存在的元素时,例如“示例” [9] ...
    编程 发布于2025-05-21
  • 如何从PHP中的Unicode字符串中有效地产生对URL友好的sl。
    如何从PHP中的Unicode字符串中有效地产生对URL友好的sl。
    为有效的slug生成首先,该函数用指定的分隔符替换所有非字母或数字字符。此步骤可确保slug遵守URL惯例。随后,它采用ICONV函数将文本简化为us-ascii兼容格式,从而允许更广泛的字符集合兼容性。接下来,该函数使用正则表达式删除了不需要的字符,例如特殊字符和空格。此步骤可确保slug仅包含...
    编程 发布于2025-05-21
  • 用户本地时间格式及时区偏移显示指南
    用户本地时间格式及时区偏移显示指南
    在用户的语言环境格式中显示日期/时间,并使用时间偏移在向最终用户展示日期和时间时,以其localzone and格式显示它们至关重要。这确保了不同地理位置的清晰度和无缝用户体验。以下是使用JavaScript实现此目的的方法。方法:推荐方法是处理客户端的Javascript中的日期/时间格式化和时...
    编程 发布于2025-05-21
  • Java开发者如何保护数据库凭证免受反编译?
    Java开发者如何保护数据库凭证免受反编译?
    在java 在单独的配置文件保护数据库凭证的最有效方法中存储凭据是将它们存储在单独的配置文件中。该文件可以在运行时加载,从而使登录数据从编译的二进制文件中远离。使用prevereness class import java.util.prefs.preferences; 公共类示例{ 首选项...
    编程 发布于2025-05-21
  • 如何在Java中正确显示“ DD/MM/YYYY HH:MM:SS.SS”格式的当前日期和时间?
    如何在Java中正确显示“ DD/MM/YYYY HH:MM:SS.SS”格式的当前日期和时间?
    如何在“ dd/mm/yyyy hh:mm:mm:ss.ss”格式“ gormat 解决方案:的,请访问量很大,并应为procectiquiestate的,并在整个代码上正确格式不多: java.text.simpledateformat; 导入java.util.calendar; 导入java...
    编程 发布于2025-05-21
  • CSS强类型语言解析
    CSS强类型语言解析
    您可以通过其强度或弱输入的方式对编程语言进行分类的方式之一。在这里,“键入”意味着是否在编译时已知变量。一个例子是一个场景,将整数(1)添加到包含整数(“ 1”)的字符串: result = 1 "1";包含整数的字符串可能是由带有许多运动部件的复杂逻辑套件无意间生成的。它也可以是故意从单个真理...
    编程 发布于2025-05-21
  • 在程序退出之前,我需要在C ++中明确删除堆的堆分配吗?
    在程序退出之前,我需要在C ++中明确删除堆的堆分配吗?
    在C中的显式删除 在C中的动态内存分配时,开发人员通常会想知道是否有必要在heap-procal extrable exit exit上进行手动调用“ delete”操作员,但开发人员通常会想知道是否需要手动调用“ delete”操作员。本文深入研究了这个主题。 在C主函数中,使用了动态分配变量(H...
    编程 发布于2025-05-21
  • 在Java中使用for-to-loop和迭代器进行收集遍历之间是否存在性能差异?
    在Java中使用for-to-loop和迭代器进行收集遍历之间是否存在性能差异?
    For Each Loop vs. Iterator: Efficiency in Collection TraversalIntroductionWhen traversing a collection in Java, the choice arises between using a for-...
    编程 发布于2025-05-21
  • 如何检查对象是否具有Python中的特定属性?
    如何检查对象是否具有Python中的特定属性?
    方法来确定对象属性存在寻求一种方法来验证对象中特定属性的存在。考虑以下示例,其中尝试访问不确定属性会引起错误: >>> a = someClass() >>> A.property Trackback(最近的最新电话): 文件“ ”,第1行, AttributeError: SomeClass...
    编程 发布于2025-05-21
  • 如何使用组在MySQL中旋转数据?
    如何使用组在MySQL中旋转数据?
    在关系数据库中使用mySQL组使用mySQL组进行查询结果,在关系数据库中使用MySQL组,转移数据的数据是指重新排列的行和列的重排以增强数据可视化。在这里,我们面对一个共同的挑战:使用组的组将数据从基于行的基于列的转换为基于列。 Let's consider the following ...
    编程 发布于2025-05-21
  • PHP SimpleXML解析带命名空间冒号的XML方法
    PHP SimpleXML解析带命名空间冒号的XML方法
    在php 很少,请使用该限制很大,很少有很高。例如:这种技术可确保可以通过遍历XML树和使用儿童()方法()方法的XML树和切换名称空间来访问名称空间内的元素。
    编程 发布于2025-05-21
  • C++中如何将独占指针作为函数或构造函数参数传递?
    C++中如何将独占指针作为函数或构造函数参数传递?
    在构造函数和函数中将唯一的指数管理为参数 unique pointers( unique_ptr [2启示。通过值: base(std :: simelor_ptr n) :next(std :: move(n)){} 此方法将唯一指针的所有权转移到函数/对象。指针的内容被移至功能中,在操作...
    编程 发布于2025-05-21
  • 如何将多种用户类型(学生,老师和管理员)重定向到Firebase应用中的各自活动?
    如何将多种用户类型(学生,老师和管理员)重定向到Firebase应用中的各自活动?
    Red: How to Redirect Multiple User Types to Respective ActivitiesUnderstanding the ProblemIn a Firebase-based voting app with three distinct user type...
    编程 发布于2025-05-21

免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。

Copyright© 2022 湘ICP备2022001581号-3