Skip to content

Commit 9b139c2

Browse files
committed
docs: sync pattern examples with .c files and fix code blocks
1 parent 809a2ae commit 9b139c2

File tree

1 file changed

+62
-70
lines changed

1 file changed

+62
-70
lines changed

21_pattern_examples.md

Lines changed: 62 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<div align="right">
44

55
**🧭 Navigation**
6-
[⬅️ Previous](20_typedef.md) | [🏠 Home](README.md) | [➡️ Next](21_pattern_examples.md)
6+
[⬅️ Previous](20_typedef.md) | [🏠 Home](README.md) | [➡️ Next](README.md)
77

88
</div>
99

@@ -256,7 +256,10 @@ int main() {
256256

257257
---
258258

259+
**Code:**
260+
Runnable source: [contributions/basic_programs/pattern_hollow_square.c](../contributions/basic_programs/pattern_hollow_square.c)
259261

262+
```c
260263
/*
261264
* Hollow Square Pattern
262265
*
@@ -265,9 +268,7 @@ int main() {
265268
* - Only the border (first row, last row, first column, last column)
266269
* is printed with '*'.
267270
* - The inner cells are printed with spaces to make the square hollow.
268-
*
269-
270-
271+
271272
*/
272273

273274
#include <stdio.h>
@@ -277,97 +278,91 @@ int main() {
277278

278279
for (int i = 1; i <= n; i++) {
279280
for (int j = 1; j <= n; j++) {
280-
281-
// Print star on the boundary, space inside
282-
if (i == 1 || i == n || j == 1 || j == n) {
281+
if (i == 1 || i == n || j == 1 || j == n)
283282
printf("* ");
284-
} else {
283+
else
285284
printf(" ");
286-
}
285+
287286
}
288287
printf("\n");
289288
}
290289
return 0;
291290
}
291+
```
292+
292293
**Expected output:**
293294
```
294-
* * * *
295-
* *
296-
* *
297-
* *
298-
* * * *
295+
* * * * *
296+
* *
297+
* *
298+
* *
299+
* * * * *
299300
```
300301
---
301302

303+
**Code:**
304+
Runnable source: [contributions/basic_programs/pattern_diamond.c](../contributions/basic_programs/pattern_diamond.c)
305+
306+
```c
302307
/*
303308
* Diamond Star Pattern
304309
*
305310
* Explanation:
306-
* - The diamond is created using two pyramids:
307-
* 1. Upper pyramid (increasing stars)
308-
* 2. Lower inverted pyramid (decreasing stars)
309-
* - For each line:
310-
* - First print spaces to center the stars.
311-
* - Then print (2*i - 1) stars to maintain symmetric shape.
312-
311+
* - The diamond is created using:
312+
* 1. Upper pyramid
313+
* 2. Lower inverted pyramid
314+
* - Each row is centered using spaces.
313315
*/
314316

315317
#include <stdio.h>
316318

317319
int main() {
318320
int n = 5;
319321

320-
// Upper half of diamond
322+
// Upper half
321323
for (int i = 1; i <= n; i++) {
322-
// Print leading spaces
323-
for (int s = 1; s <= n - i; s++) {
324-
printf(" ");
325-
}
326-
// Print stars
327-
for (int j = 1; j <= 2*i - 1; j++) {
328-
printf("*");
329-
}
324+
for (int s = 1; s <= n - i; s++) printf(" ");
325+
for (int j = 1; j <= 2*i - 1; j++) printf("*");
330326
printf("\n");
331327
}
332328

333-
// Lower half of diamond
329+
// Lower half
334330
for (int i = n - 1; i >= 1; i--) {
335-
for (int s = 1; s <= n - i; s++) {
336-
printf(" ");
337-
}
338-
for (int j = 1; j <= 2*i - 1; j++) {
339-
printf("*");
340-
}
331+
for (int s = 1; s <= n - i; s++) printf(" ");
332+
for (int j = 1; j <= 2*i - 1; j++) printf("*");
341333
printf("\n");
342334
}
343335

344336
return 0;
345337
}
338+
```
339+
346340
**Expected output:**
347341
```
348-
349-
*
350-
***
351-
*****
352-
*******
353-
*********
354-
*******
355-
*****
356-
***
357-
*
342+
*
343+
***
344+
*****
345+
*******
346+
*********
347+
*******
348+
*****
349+
***
350+
*
358351
```
352+
359353
---
360354

355+
**Code:**
356+
Runnable source: [contributions/basic_programs/pattern_hollow_pyramid.c](../contributions/basic_programs/pattern_hollow_pyramid.c)
357+
358+
```c
361359
/*
362360
* Hollow Pyramid Pattern
363361
*
364362
* Explanation:
365-
* - A pyramid with its inside hollow.
366-
* - For each row:
367-
* * Print leading spaces to center the pyramid.
368-
* * Print 1 star at the start and 1 star at the end.
369-
* * For the last row, print all stars to close the shape.
370-
363+
* - Spaces center the pyramid.
364+
* - First and last positions print stars.
365+
* - Last row prints all stars.
371366
*/
372367

373368
#include <stdio.h>
@@ -377,35 +372,31 @@ int main() {
377372

378373
for (int i = 1; i <= n; i++) {
379374

380-
// Print leading spaces
381-
for (int s = 1; s <= n - i; s++) {
382-
printf(" ");
383-
}
375+
// Leading spaces
376+
for (int s = 1; s <= n - i; s++) printf(" ");
384377

385-
// Print hollow pattern
386378
for (int j = 1; j <= 2*i - 1; j++) {
387-
388-
// First star, last star, or entire last row
389-
if (j == 1 || j == 2*i - 1 || i == n) {
379+
if (j == 1 || j == 2*i - 1 || i == n)
390380
printf("*");
391-
} else {
381+
else
392382
printf(" ");
393-
}
383+
394384
}
395385

396386
printf("\n");
397387
}
398388

399389
return 0;
400390
}
401-
**Expected output:**
402391
```
403392

404-
*
405-
* *
406-
* *
407-
* *
408-
*********
393+
**Expected output:**
394+
```
395+
*
396+
* *
397+
* *
398+
* *
399+
*********
409400
```
410401
---
411402

@@ -422,6 +413,7 @@ int main() {
422413
<div align="right">
423414

424415
**🧭 Navigation**
425-
[⬅️ Previous](20_typedef.md) | [🏠 Home](README.md) | [➡️ Next](21_pattern_examples.md)
416+
[⬅️ Previous](20_typedef.md) | [🏠 Home](README.md)
417+
426418

427419
</div>

0 commit comments

Comments
 (0)