Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions 08_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,139 @@ int multiply(int x, int y) {
```

---
/*
* gcd_lcm_functions.c
* Demonstrates writing and reusing functions in C.
*
* What it shows:
* - Pure functions with clear inputs/outputs
* - Reuse: LCM computed using GCD
*
* Build: gcc gcd_lcm_functions.c -o gcd_lcm
* Run: ./gcd_lcm
*/

#include <stdio.h>

int gcd(int a, int b) {
while (b != 0) {
int r = a % b;
a = b;
b = r;
}
return (a < 0) ? -a : a; // handle negatives
}

long long lcm(int a, int b) {
int g = gcd(a, b);
// avoid overflow for small inputs by casting
return (long long)a / g * b;
}

int main(void) {
int x, y;
printf("Enter two integers: ");
if (scanf("%d %d", &x, &y) != 2) {
printf("Invalid input.\n");
return 1;
}

printf("GCD(%d, %d) = %d\n", x, y, gcd(x, y));
printf("LCM(%d, %d) = %lld\n", x, y, lcm(x, y));
return 0;
}
Comment on lines +205 to +245
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

Align comment formatting with documentation standards.

The C code examples in this section use inconsistent formatting with raw markdown comments instead of embedding comments within fenced C code blocks. This violates MD007 and MD018 linting rules.

Restructure the three new examples (gcd_lcm_functions, perfect_number_function, strong_number_function) to place C-style comments inside the fenced code blocks, consistent with the repository's existing documentation:

-/*
- * gcd_lcm_functions.c
- * Demonstrates writing and reusing functions in C.
- *
- * What it shows:
- *  - Pure functions with clear inputs/outputs
- *  - Reuse: LCM computed using GCD
- *
- * Build:  gcc gcd_lcm_functions.c -o gcd_lcm
- * Run:    ./gcd_lcm
- */
-
 #include <stdio.h>
+
+/*
+ * gcd_lcm_functions.c
+ * Demonstrates writing and reusing functions in C.
+ * 
+ * Shows: Pure functions with clear I/O; LCM computed using GCD
+ * Build:  gcc gcd_lcm_functions.c -o gcd_lcm
+ * Run:    ./gcd_lcm
+ */

Apply the same restructuring to the perfect_number_function.c and strong_number_function.c comments.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/*
* gcd_lcm_functions.c
* Demonstrates writing and reusing functions in C.
*
* What it shows:
* - Pure functions with clear inputs/outputs
* - Reuse: LCM computed using GCD
*
* Build: gcc gcd_lcm_functions.c -o gcd_lcm
* Run: ./gcd_lcm
*/
#include <stdio.h>
int gcd(int a, int b) {
while (b != 0) {
int r = a % b;
a = b;
b = r;
}
return (a < 0) ? -a : a; // handle negatives
}
long long lcm(int a, int b) {
int g = gcd(a, b);
// avoid overflow for small inputs by casting
return (long long)a / g * b;
}
int main(void) {
int x, y;
printf("Enter two integers: ");
if (scanf("%d %d", &x, &y) != 2) {
printf("Invalid input.\n");
return 1;
}
printf("GCD(%d, %d) = %d\n", x, y, gcd(x, y));
printf("LCM(%d, %d) = %lld\n", x, y, lcm(x, y));
return 0;
}
#include <stdio.h>
/*
* gcd_lcm_functions.c
* Demonstrates writing and reusing functions in C.
*
* Shows: Pure functions with clear I/O; LCM computed using GCD
* Build: gcc gcd_lcm_functions.c -o gcd_lcm
* Run: ./gcd_lcm
*/
int gcd(int a, int b) {
while (b != 0) {
int r = a % b;
a = b;
b = r;
}
return (a < 0) ? -a : a; // handle negatives
}
long long lcm(int a, int b) {
int g = gcd(a, b);
// avoid overflow for small inputs by casting
return (long long)a / g * b;
}
int main(void) {
int x, y;
printf("Enter two integers: ");
if (scanf("%d %d", &x, &y) != 2) {
printf("Invalid input.\n");
return 1;
}
printf("GCD(%d, %d) = %d\n", x, y, gcd(x, y));
printf("LCM(%d, %d) = %lld\n", x, y, lcm(x, y));
return 0;
}
🧰 Tools
🪛 markdownlint-cli2 (0.18.1)

206-206: Unordered list indentation
Expected: 0; Actual: 1

(MD007, ul-indent)


207-207: Unordered list indentation
Expected: 0; Actual: 1

(MD007, ul-indent)


208-208: Unordered list indentation
Expected: 0; Actual: 1

(MD007, ul-indent)


209-209: Unordered list indentation
Expected: 0; Actual: 1

(MD007, ul-indent)


210-210: Unordered list indentation
Expected: 0; Actual: 1

(MD007, ul-indent)


210-210: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)


211-211: Unordered list indentation
Expected: 0; Actual: 1

(MD007, ul-indent)


211-211: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)


212-212: Unordered list indentation
Expected: 0; Actual: 1

(MD007, ul-indent)


213-213: Unordered list indentation
Expected: 0; Actual: 1

(MD007, ul-indent)


214-214: Unordered list indentation
Expected: 0; Actual: 1

(MD007, ul-indent)


217-217: No space after hash on atx style heading

(MD018, no-missing-space-atx)


242-242: Code block style
Expected: fenced; Actual: indented

(MD046, code-block-style)

🤖 Prompt for AI Agents
In 08_functions.md around lines 205 to 245, the example uses raw markdown
comment syntax outside of the fenced C code block which violates MD007/MD018;
update the three examples (gcd_lcm_functions, perfect_number_function,
strong_number_function) so that each C example is entirely inside a ```c fenced
code block and contains C-style comments (/* ... */) at the top of the code
instead of markdown text outside the block; ensure the fence markers surround
only the C source, move any descriptive lines into C comments within the block,
and make the formatting consistent with existing repository examples.


/*
* perfect_number_function.c
* Checks whether a number is a perfect number using a helper function.
*
* What it shows:
* - Decomposing logic into a function (sum of proper divisors)
* - Simple loop + conditionals inside a function
*
* Build: gcc perfect_number_function.c -o perfect
* Run: ./perfect
*/

#include <stdio.h>

int sumOfProperDivisors(int n) {
if (n <= 1) return 0;
int sum = 1; // 1 is a proper divisor for n > 1
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
sum += i;
if (i != n / i) sum += n / i; // add the paired divisor
}
}
return sum;
}

int main(void) {
int n;
printf("Enter a positive integer: ");
if (scanf("%d", &n) != 1 || n <= 0) {
printf("Invalid input.\n");
return 1;
}

if (sumOfProperDivisors(n) == n)
printf("%d is a Perfect Number\n", n);
else
printf("%d is NOT a Perfect Number\n", n);

return 0;
}

/*
* strong_number_function.c
* Strong number: sum of factorials of digits equals the number (e.g., 145).
*
* What it shows:
* - Multiple small functions (digit factorial, checker)
* - Reusability of a precomputed factorial(0..9) table
*
* Build: gcc strong_number_function.c -o strong
* Run: ./strong
*/

#include <stdio.h>

int fact[10];

void precomputeFactorials(void) {
fact[0] = 1;
for (int d = 1; d <= 9; d++) fact[d] = fact[d - 1] * d;
}

int isStrong(int n) {
int original = n, sum = 0;
while (n > 0) {
int digit = n % 10;
sum += fact[digit];
n /= 10;
}
return sum == original;
}

int main(void) {
precomputeFactorials();

int n;
printf("Enter a positive integer: ");
if (scanf("%d", &n) != 1 || n <= 0) {
printf("Invalid input.\n");
return 1;
}

if (isStrong(n))
printf("%d is a Strong Number\n", n);
else
printf("%d is NOT a Strong Number\n", n);

return 0;
}


## Summary

Expand Down
151 changes: 149 additions & 2 deletions 21_pattern_examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div align="right">

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

</div>

Expand Down Expand Up @@ -256,6 +256,152 @@ int main() {

---

**Code:**
Runnable source: [contributions/basic_programs/pattern_hollow_square.c](../contributions/basic_programs/pattern_hollow_square.c)

```c
/*
* Hollow Square Pattern
*
* Explanation:
* - The pattern prints a square of size n.
* - Only the border (first row, last row, first column, last column)
* is printed with '*'.
* - The inner cells are printed with spaces to make the square hollow.

*/

#include <stdio.h>

int main() {
int n = 5;

for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == 1 || i == n || j == 1 || j == n)
printf("* ");
else
printf(" ");

}
printf("\n");
}
return 0;
}
```

**Expected output:**
```
* * * * *
* *
* *
* *
* * * * *
```
Comment on lines +293 to +300
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Add language specifier to expected output block to resolve MD040.

The expected output fence (line 294) is missing a language specifier. While the linter accepts an empty specifier, explicitly using output is clearer and complies with markdown-lint rules.

 **Expected output:**
-```
+```output
 * * * * *
 *       *
 *       *
 *       *
 * * * * *
-```
+```
🧰 Tools
🪛 markdownlint-cli2 (0.18.1)

294-294: Fenced code blocks should have a language specified

(MD040, fenced-code-language)

🤖 Prompt for AI Agents
In 21_pattern_examples.md around lines 293 to 300, the fenced block showing the
expected output is missing a language specifier which triggers MD040; update the
opening fence from ``` to ```output so the block becomes a fenced code block
with the language specifier "output" (leave the content lines unchanged and keep
the closing ``` intact).

---

**Code:**
Runnable source: [contributions/basic_programs/pattern_diamond.c](../contributions/basic_programs/pattern_diamond.c)
Comment on lines +303 to +304
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Remove leading space before **Code:** for consistency.

Line 303 has an unintended leading space. Remove it to match the formatting of earlier patterns and line 259.

-**Code:**  
+**Code:**
 Runnable source: [contributions/basic_programs/pattern_diamond.c](../contributions/basic_programs/pattern_diamond.c)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
**Code:**
Runnable source: [contributions/basic_programs/pattern_diamond.c](../contributions/basic_programs/pattern_diamond.c)
**Code:**
Runnable source: [contributions/basic_programs/pattern_diamond.c](../contributions/basic_programs/pattern_diamond.c)
🤖 Prompt for AI Agents
In 21_pattern_examples.md around lines 303-304, there is an unintended leading
space before the "**Code:**" heading; remove that single leading space so the
line starts with "**Code:**" (no preceding whitespace) to match the formatting
used elsewhere (e.g., line 259).


```c
/*
* Diamond Star Pattern
*
* Explanation:
* - The diamond is created using:
* 1. Upper pyramid
* 2. Lower inverted pyramid
* - Each row is centered using spaces.
*/

#include <stdio.h>

int main() {
int n = 5;

// Upper half
for (int i = 1; i <= n; i++) {
for (int s = 1; s <= n - i; s++) printf(" ");
for (int j = 1; j <= 2*i - 1; j++) printf("*");
printf("\n");
}

// Lower half
for (int i = n - 1; i >= 1; i--) {
for (int s = 1; s <= n - i; s++) printf(" ");
for (int j = 1; j <= 2*i - 1; j++) printf("*");
printf("\n");
}

return 0;
}
```

**Expected output:**
```
*
***
*****
*******
*********
*******
*****
***
*
```
Comment on lines +340 to +351
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Add language specifier to expected output block to resolve MD040.

Same as the Hollow Square pattern: specify output as the language for the fence on line 341.

 **Expected output:**
-```
+```output
     *
    ***
   *****
  *******
 *********
  *******
   *****
    ***
     *
-```
+```
🧰 Tools
🪛 markdownlint-cli2 (0.18.1)

341-341: Fenced code blocks should have a language specified

(MD040, fenced-code-language)

🤖 Prompt for AI Agents
In 21_pattern_examples.md around lines 340 to 351, the fenced code block showing
the Expected output lacks a language specifier and triggers MD040; update the
opening fence on line 341 to use ```output (i.e., change ``` to ```output) so
the block is explicitly marked as output and keep the closing fence as ``` to
match.


---

**Code:**
Runnable source: [contributions/basic_programs/pattern_hollow_pyramid.c](../contributions/basic_programs/pattern_hollow_pyramid.c)
Comment on lines +355 to +356
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Remove leading space before **Code:** for consistency.

Line 355 has an unintended leading space. Remove it to match earlier patterns.

-**Code:**  
+**Code:**
 Runnable source: [contributions/basic_programs/pattern_hollow_pyramid.c](../contributions/basic_programs/pattern_hollow_pyramid.c)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
**Code:**
Runnable source: [contributions/basic_programs/pattern_hollow_pyramid.c](../contributions/basic_programs/pattern_hollow_pyramid.c)
**Code:**
Runnable source: [contributions/basic_programs/pattern_hollow_pyramid.c](../contributions/basic_programs/pattern_hollow_pyramid.c)
🤖 Prompt for AI Agents
In 21_pattern_examples.md around lines 355-356, there is an unintended leading
space before "**Code:**"; remove that single leading space so the line begins
with "**Code:**" to match the formatting of previous entries and keep
consistency across the document.


```c
/*
* Hollow Pyramid Pattern
*
* Explanation:
* - Spaces center the pyramid.
* - First and last positions print stars.
* - Last row prints all stars.
*/

#include <stdio.h>

int main() {
int n = 5;

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

// Leading spaces
for (int s = 1; s <= n - i; s++) printf(" ");

for (int j = 1; j <= 2*i - 1; j++) {
if (j == 1 || j == 2*i - 1 || i == n)
printf("*");
else
printf(" ");

}

printf("\n");
}

return 0;
}
```

**Expected output:**
```
*
* *
* *
* *
*********
```
---



## Summary

- Pattern programs use nested loops for rows and columns.
Expand All @@ -267,6 +413,7 @@ int main() {
<div align="right">

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


</div>