」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 掌握填滿模式:帶有程式碼範例的綜合指南

掌握填滿模式:帶有程式碼範例的綜合指南

發佈於2024-08-22
瀏覽:404

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]刪除
最新教學 更多>
  • 在 Laravel 測試排隊作業的技巧
    在 Laravel 測試排隊作業的技巧
    使用 Laravel 應用程式時,經常會遇到命令需要執行昂貴任務的情況。為了避免阻塞主進程,您可能決定將任務卸載到可以由佇列處理的作業。 讓我們來看一個例子。想像一下指令 app:import-users 需要讀取一個大的 CSV 檔案並為每個條目建立一個使用者。該命令可能如下所示: /* Imp...
    程式設計 發佈於2024-11-05
  • 如何創建人類層級的自然語言理解 (NLU) 系統
    如何創建人類層級的自然語言理解 (NLU) 系統
    Scope: Creating an NLU system that fully understands and processes human languages in a wide range of contexts, from conversations to literature. ...
    程式設計 發佈於2024-11-05
  • 如何使用 JSTL 迭代 HashMap 中的 ArrayList?
    如何使用 JSTL 迭代 HashMap 中的 ArrayList?
    使用JSTL 迭代HashMap 中的ArrayList在Web 開發中,JSTL(JavaServer Pages 標準標記庫)提供了一組標記來簡化JSP 中的常見任務( Java 伺服器頁面)。其中一項任務是迭代資料結構。 要迭代 HashMap 及其中包含的 ArrayList,可以使用 JS...
    程式設計 發佈於2024-11-05
  • Encore.ts — 比 ElysiaJS 和 Hono 更快
    Encore.ts — 比 ElysiaJS 和 Hono 更快
    几个月前,我们发布了 Encore.ts — TypeScript 的开源后端框架。 由于已经有很多框架,我们想分享我们做出的一些不常见的设计决策以及它们如何带来卓越的性能数据。 性能基准 我们之前发布的基准测试显示 Encore.ts 比 Express 快 9 倍,比 Fasti...
    程式設計 發佈於2024-11-05
  • 為什麼使用 + 對字串文字進行字串連接失敗?
    為什麼使用 + 對字串文字進行字串連接失敗?
    連接字串文字與字串在 C 中,運算子可用於連接字串和字串文字。但是,此功能存在限制,可能會導致混亂。 在問題中,作者嘗試連接字串文字「Hello」、「,world」和「!」以兩種不同的方式。第一個例子:const string hello = "Hello"; const str...
    程式設計 發佈於2024-11-05
  • React 重新渲染:最佳效能的最佳實踐
    React 重新渲染:最佳效能的最佳實踐
    React高效率的渲染機制是其受歡迎的關鍵原因之一。然而,隨著應用程式複雜性的增加,管理元件重新渲染對於最佳化效能變得至關重要。讓我們探索優化 React 渲染行為並避免不必要的重新渲染的最佳實踐。 1. 使用 React.memo() 作為函數式元件 React.memo() 是...
    程式設計 發佈於2024-11-05
  • 如何實作條件列建立:探索 Pandas DataFrame 中的 If-Elif-Else?
    如何實作條件列建立:探索 Pandas DataFrame 中的 If-Elif-Else?
    Creating a Conditional Column: If-Elif-Else in Pandas給定的問題要求將新列新增至DataFrame 中基於一系列條件標準。挑戰在於在實現這些條件的同時保持程式碼效率和可讀性。 使用函數應用程式的解決方案一種方法涉及創建一個將每一行映射到所需結果的函...
    程式設計 發佈於2024-11-05
  • 介紹邱!
    介紹邱!
    我很高興地宣布發布 Qiu – 一個嚴肅的 SQL 查詢運行器,旨在讓原始 SQL 再次變得有趣。老實說,ORM 有其用武之地,但當您只想編寫簡單的 SQL 時,它們可能會有點不知所措。我一直很喜歡寫原始 SQL 查詢,但我意識到我需要練習——大量的練習。這就是Qiu發揮作用的地方。 有了 Qiu...
    程式設計 發佈於2024-11-05
  • 為什麼 CSS 中的 Margin-Top 百分比是根據容器寬度計算的?
    為什麼 CSS 中的 Margin-Top 百分比是根據容器寬度計算的?
    CSS 中的 margin-top 百分比計算CSS 中的 margin-top 百分比計算當對元素應用 margin-top 百分比時,必須了解計算方式執行。與普遍的看法相反,邊距頂部百分比是根據包含塊的寬度而不是其高度來確定的。 W3C 規範解釋:W3C 規範解釋:根據W3C 規範,“百分比是根...
    程式設計 發佈於2024-11-05
  • 如何解決 CSS 轉換期間 Webkit 文字渲染不一致的問題?
    如何解決 CSS 轉換期間 Webkit 文字渲染不一致的問題?
    解決CSS 轉換期間的Webkit 文本渲染不一致在CSS 轉換期間,特別是縮放元素時,Webkit 中可能會出現文本渲染不一致的情況瀏覽器。這個問題源自於瀏覽器嘗試優化渲染效能。 一種解決方案是透過添加以下屬性來強制對過渡元素的父元素進行硬體加速:-webkit-transform: transl...
    程式設計 發佈於2024-11-05
  • 使用 Reactables 簡化 RxJS
    使用 Reactables 簡化 RxJS
    介紹 RxJS 是一個功能強大的庫,但眾所周知,它的學習曲線很陡峭。 這個函式庫龐大的 API 介面,再加上向反應式程式設計的典範轉移,可能會讓新手不知所措。 我創建了 Reactables API 來簡化 RxJS 的使用並簡化開發人員對反應式程式設計的介紹。 ...
    程式設計 發佈於2024-11-05
  • 如何在 Pandas 中找到多列的最大值?
    如何在 Pandas 中找到多列的最大值?
    找出 Pandas 中多列的最大值要確定 pandas DataFrame 中多列的最大值,可以採用多種方法。以下是實現此目的的方法:對指定列使用max() 函數此方法涉及明確選擇所需的列並應用max() 函數: df[["A", "B"]] df[[&quo...
    程式設計 發佈於2024-11-05
  • CI/CD 入門:自動化第一個管道的初學者指南(使用 Jenkins)
    CI/CD 入門:自動化第一個管道的初學者指南(使用 Jenkins)
    目錄 介紹 什麼是 CI/CD? 持續整合(CI) 持續交付(CD) 持續部署 CI/CD 的好處 更快的上市時間 提高程式碼品質 高效率協作 提高自動化程度和一致性 如何建立您的第一個 CI/CD 管道 第 1 步:設定版本控制 (GitHub) 步驟 2: 選擇 CI/CD ...
    程式設計 發佈於2024-11-05
  • TypeScript 如何讓 JavaScript 在大型專案中更加可靠。
    TypeScript 如何讓 JavaScript 在大型專案中更加可靠。
    介绍 JavaScript 广泛应用于 Web 开发,现在也被应用于不同行业的大型项目中。然而,随着这些项目的增长,管理 JavaScript 代码变得更加困难。数据类型不匹配、运行时意外错误以及代码不清晰等问题可能会导致查找和修复错误变得困难。 这就是TypeScript介入的地...
    程式設計 發佈於2024-11-05
  • 如何使用PHP的password_verify函數安全地驗證使用者密碼?
    如何使用PHP的password_verify函數安全地驗證使用者密碼?
    使用 PHP 解密加密密碼許多應用程式使用密碼雜湊等加密演算法安全地儲存使用者密碼。然而,在驗證登入嘗試時,將輸入密碼與加密的儲存版本進行比較非常重要。 加密問題password_hash 使用 Bcrypt,一元加密演算法方式雜湊演算法,表示加密的密碼無法逆轉或解密。這是一項安全功能,可確保即使資...
    程式設計 發佈於2024-11-05

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3