c

examples

examples.c🔧
/**
 * @file examples.c
 * @brief Nested Loops Examples
 * 
 * This file demonstrates nested loops in C.
 * 
 * Compile: gcc -Wall examples.c -o examples
 * Run: ./examples
 */

#include <stdio.h>

/* ============================================================
 * EXAMPLE 1: BASIC NESTED FOR LOOP
 * ============================================================ */
void example_basic_nested() {
    printf("\n=== Example 1: Basic Nested for Loop ===\n");
    
    printf("Coordinates in a 3x4 grid:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++) {
            printf("(%d,%d) ", i, j);
        }
        printf("\n");
    }
    printf("Total iterations: %d\n", 3 * 4);
}

/* ============================================================
 * EXAMPLE 2: RECTANGLE OF STARS
 * ============================================================ */
void example_rectangle() {
    printf("\n=== Example 2: Rectangle of Stars ===\n");
    
    int rows = 4, cols = 8;
    
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("* ");
        }
        printf("\n");
    }
}

/* ============================================================
 * EXAMPLE 3: RIGHT TRIANGLE PATTERN
 * ============================================================ */
void example_right_triangle() {
    printf("\n=== Example 3: Right Triangle ===\n");
    
    int n = 5;
    
    printf("Increasing:\n");
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }
    
    printf("\nDecreasing:\n");
    for (int i = n; i >= 1; i--) {
        for (int j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }
}

/* ============================================================
 * EXAMPLE 4: NUMBER TRIANGLE
 * ============================================================ */
void example_number_triangle() {
    printf("\n=== Example 4: Number Triangle ===\n");
    
    int n = 5;
    
    printf("Pattern 1 (1 to row):\n");
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            printf("%d ", j);
        }
        printf("\n");
    }
    
    printf("\nPattern 2 (row number):\n");
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            printf("%d ", i);
        }
        printf("\n");
    }
    
    printf("\nPattern 3 (Floyd's Triangle):\n");
    int num = 1;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            printf("%2d ", num++);
        }
        printf("\n");
    }
}

/* ============================================================
 * EXAMPLE 5: PYRAMID PATTERN
 * ============================================================ */
void example_pyramid() {
    printf("\n=== Example 5: Pyramid ===\n");
    
    int n = 5;
    
    for (int i = 1; i <= n; i++) {
        // Print spaces
        for (int s = 1; s <= n - i; s++) {
            printf(" ");
        }
        // Print stars
        for (int j = 1; j <= 2 * i - 1; j++) {
            printf("*");
        }
        printf("\n");
    }
}

/* ============================================================
 * EXAMPLE 6: HOLLOW SQUARE
 * ============================================================ */
void example_hollow_square() {
    printf("\n=== Example 6: Hollow Square ===\n");
    
    int n = 5;
    
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            // Print * only at edges
            if (i == 0 || i == n-1 || j == 0 || j == n-1) {
                printf("* ");
            } else {
                printf("  ");
            }
        }
        printf("\n");
    }
}

/* ============================================================
 * EXAMPLE 7: 2D ARRAY TRAVERSAL
 * ============================================================ */
void example_2d_array() {
    printf("\n=== Example 7: 2D Array Operations ===\n");
    
    int matrix[3][4] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}
    };
    int rows = 3, cols = 4;
    
    // Print matrix
    printf("Matrix:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%3d ", matrix[i][j]);
        }
        printf("\n");
    }
    
    // Sum of all elements
    int total = 0;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            total += matrix[i][j];
        }
    }
    printf("Total sum: %d\n", total);
    
    // Row sums
    printf("Row sums: ");
    for (int i = 0; i < rows; i++) {
        int rowSum = 0;
        for (int j = 0; j < cols; j++) {
            rowSum += matrix[i][j];
        }
        printf("%d ", rowSum);
    }
    printf("\n");
    
    // Column sums
    printf("Col sums: ");
    for (int j = 0; j < cols; j++) {
        int colSum = 0;
        for (int i = 0; i < rows; i++) {
            colSum += matrix[i][j];
        }
        printf("%d ", colSum);
    }
    printf("\n");
}

/* ============================================================
 * EXAMPLE 8: MATRIX TRANSPOSE
 * ============================================================ */
void example_transpose() {
    printf("\n=== Example 8: Matrix Transpose ===\n");
    
    int matrix[3][3] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };
    int transposed[3][3];
    
    printf("Original:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", matrix[i][j]);
            transposed[j][i] = matrix[i][j];
        }
        printf("\n");
    }
    
    printf("\nTransposed:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", transposed[i][j]);
        }
        printf("\n");
    }
}

/* ============================================================
 * EXAMPLE 9: MULTIPLICATION TABLE
 * ============================================================ */
void example_multiplication_table() {
    printf("\n=== Example 9: Multiplication Table ===\n");
    
    int n = 10;
    
    // Header
    printf("    |");
    for (int j = 1; j <= n; j++) {
        printf("%4d", j);
    }
    printf("\n----+");
    for (int j = 1; j <= n; j++) {
        printf("----");
    }
    printf("\n");
    
    // Table body
    for (int i = 1; i <= n; i++) {
        printf("%3d |", i);
        for (int j = 1; j <= n; j++) {
            printf("%4d", i * j);
        }
        printf("\n");
    }
}

/* ============================================================
 * EXAMPLE 10: DIAMOND PATTERN
 * ============================================================ */
void example_diamond() {
    printf("\n=== Example 10: Diamond Pattern ===\n");
    
    int n = 5;
    
    // Upper half (including middle)
    for (int i = 1; i <= n; i++) {
        // Spaces
        for (int s = 1; s <= n - i; s++) {
            printf(" ");
        }
        // Stars
        for (int j = 1; j <= 2 * i - 1; j++) {
            printf("*");
        }
        printf("\n");
    }
    
    // Lower half
    for (int i = n - 1; i >= 1; i--) {
        // Spaces
        for (int s = 1; s <= n - i; s++) {
            printf(" ");
        }
        // Stars
        for (int j = 1; j <= 2 * i - 1; j++) {
            printf("*");
        }
        printf("\n");
    }
}

/* ============================================================
 * EXAMPLE 11: MATRIX SEARCH
 * ============================================================ */
void example_matrix_search() {
    printf("\n=== Example 11: Matrix Search ===\n");
    
    int matrix[4][4] = {
        {12, 25, 38, 47},
        {51, 66, 73, 84},
        {95, 10, 11, 28},
        {33, 44, 55, 66}
    };
    int target = 73;
    int found = 0;
    
    printf("Searching for %d...\n", target);
    
    for (int i = 0; i < 4 && !found; i++) {
        for (int j = 0; j < 4 && !found; j++) {
            if (matrix[i][j] == target) {
                printf("Found at position (%d, %d)\n", i, j);
                found = 1;
            }
        }
    }
    
    if (!found) {
        printf("Not found in matrix\n");
    }
}

/* ============================================================
 * EXAMPLE 12: PASCAL'S TRIANGLE
 * ============================================================ */
void example_pascals_triangle() {
    printf("\n=== Example 12: Pascal's Triangle ===\n");
    
    int n = 6;
    int pascal[10][10] = {0};
    
    // Generate Pascal's triangle
    for (int i = 0; i < n; i++) {
        pascal[i][0] = 1;  // First element is always 1
        for (int j = 1; j <= i; j++) {
            pascal[i][j] = pascal[i-1][j-1] + pascal[i-1][j];
        }
    }
    
    // Print with spacing
    for (int i = 0; i < n; i++) {
        // Leading spaces
        for (int s = 0; s < n - i - 1; s++) {
            printf("  ");
        }
        // Numbers
        for (int j = 0; j <= i; j++) {
            printf("%3d ", pascal[i][j]);
        }
        printf("\n");
    }
}

/* ============================================================
 * EXAMPLE 13: CHECKERBOARD PATTERN
 * ============================================================ */
void example_checkerboard() {
    printf("\n=== Example 13: Checkerboard Pattern ===\n");
    
    int n = 8;
    
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if ((i + j) % 2 == 0) {
                printf("# ");
            } else {
                printf(". ");
            }
        }
        printf("\n");
    }
}

/* ============================================================
 * EXAMPLE 14: SPIRAL PRINT (SIMPLIFIED)
 * ============================================================ */
void example_spiral_fill() {
    printf("\n=== Example 14: Spiral Fill Matrix ===\n");
    
    int n = 4;
    int matrix[4][4];
    int num = 1;
    
    int top = 0, bottom = n-1, left = 0, right = n-1;
    
    while (top <= bottom && left <= right) {
        // Right
        for (int i = left; i <= right; i++)
            matrix[top][i] = num++;
        top++;
        
        // Down
        for (int i = top; i <= bottom; i++)
            matrix[i][right] = num++;
        right--;
        
        // Left
        if (top <= bottom) {
            for (int i = right; i >= left; i--)
                matrix[bottom][i] = num++;
            bottom--;
        }
        
        // Up
        if (left <= right) {
            for (int i = bottom; i >= top; i--)
                matrix[i][left] = num++;
            left++;
        }
    }
    
    // Print result
    printf("Spiral filled matrix:\n");
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            printf("%3d ", matrix[i][j]);
        }
        printf("\n");
    }
}

/* ============================================================
 * EXAMPLE 15: TRIPLE NESTED LOOP
 * ============================================================ */
void example_triple_nested() {
    printf("\n=== Example 15: Triple Nested Loop ===\n");
    
    printf("Generating all 3-digit combinations (max 2):\n");
    int count = 0;
    
    for (int i = 0; i <= 2; i++) {
        for (int j = 0; j <= 2; j++) {
            for (int k = 0; k <= 2; k++) {
                printf("(%d,%d,%d) ", i, j, k);
                count++;
            }
            printf("\n");
        }
        printf("\n");
    }
    
    printf("Total combinations: %d (= 3×3×3 = 27)\n", count);
}

/* ============================================================
 * MAIN FUNCTION
 * ============================================================ */
int main() {
    printf("╔════════════════════════════════════════════════╗\n");
    printf("║    NESTED LOOPS - EXAMPLES                     ║\n");
    printf("║    Loops inside loops                          ║\n");
    printf("╚════════════════════════════════════════════════╝\n");
    
    example_basic_nested();
    example_rectangle();
    example_right_triangle();
    example_number_triangle();
    example_pyramid();
    example_hollow_square();
    example_2d_array();
    example_transpose();
    example_multiplication_table();
    example_diamond();
    example_matrix_search();
    example_pascals_triangle();
    example_checkerboard();
    example_spiral_fill();
    example_triple_nested();
    
    printf("\n=== All Examples Completed! ===\n");
    
    return 0;
}
Examples - C Programming Tutorial | DeepML