c

exercises

exercises.cšŸ”§
/*
 * =============================================================================
 * MULTI-DIMENSIONAL ARRAYS - PRACTICE EXERCISES
 * =============================================================================
 * 
 * Complete each exercise to practice multi-dimensional arrays in C.
 * 
 * Compilation: gcc exercises.c -o exercises
 * =============================================================================
 */

#include <stdio.h>
#include <stdlib.h>

/*
 * -----------------------------------------------------------------------------
 * EXERCISE 1: Initialize and Print a 3x3 Matrix
 * -----------------------------------------------------------------------------
 * Create a 3x3 matrix with values 1-9 and print it in a formatted way.
 * 
 * Expected Output:
 *   1   2   3
 *   4   5   6
 *   7   8   9
 */
void exercise1(void) {
    printf("\n=== Exercise 1: Initialize and Print 3x3 Matrix ===\n");
    
    // TODO: Declare and initialize a 3x3 matrix with values 1-9
    // int matrix[3][3] = ...
    
    // TODO: Print the matrix with proper formatting
    
    printf("(Complete the code in exercise1)\n");
}

/*
 * -----------------------------------------------------------------------------
 * EXERCISE 2: Sum of Diagonal Elements
 * -----------------------------------------------------------------------------
 * Calculate and print the sum of main diagonal elements of a square matrix.
 * 
 * Matrix:
 *   5   2   8
 *   3   9   4
 *   7   1   6
 * 
 * Main diagonal: 5, 9, 6
 * Expected Output: Sum = 20
 */
void exercise2(void) {
    printf("\n=== Exercise 2: Sum of Diagonal Elements ===\n");
    
    int matrix[3][3] = {
        {5, 2, 8},
        {3, 9, 4},
        {7, 1, 6}
    };
    
    // TODO: Calculate sum of main diagonal (matrix[i][i])
    
    // TODO: Print the result
    
    printf("(Complete the code in exercise2)\n");
}

/*
 * -----------------------------------------------------------------------------
 * EXERCISE 3: Find the Largest Element
 * -----------------------------------------------------------------------------
 * Find the largest element in a 2D array and its position.
 * 
 * Matrix:
 *   23  45  12
 *   67  34  89
 *   15  78  56
 * 
 * Expected Output: Largest = 89 at position [1][2]
 */
void exercise3(void) {
    printf("\n=== Exercise 3: Find Largest Element ===\n");
    
    int matrix[3][3] = {
        {23, 45, 12},
        {67, 34, 89},
        {15, 78, 56}
    };
    
    // TODO: Find the largest element and its position
    
    printf("(Complete the code in exercise3)\n");
}

/*
 * -----------------------------------------------------------------------------
 * EXERCISE 4: Row-wise Sum
 * -----------------------------------------------------------------------------
 * Calculate and print the sum of each row.
 * 
 * Matrix:
 *   1   2   3   4
 *   5   6   7   8
 *   9  10  11  12
 * 
 * Expected Output:
 *   Row 0 sum: 10
 *   Row 1 sum: 26
 *   Row 2 sum: 42
 */
void exercise4(void) {
    printf("\n=== Exercise 4: Row-wise Sum ===\n");
    
    int matrix[3][4] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}
    };
    
    // TODO: Calculate and print sum of each row
    
    printf("(Complete the code in exercise4)\n");
}

/*
 * -----------------------------------------------------------------------------
 * EXERCISE 5: Transpose a Matrix
 * -----------------------------------------------------------------------------
 * Create the transpose of a 2x3 matrix.
 * 
 * Original [2x3]:     Transposed [3x2]:
 *   1   2   3           1   4
 *   4   5   6           2   5
 *                       3   6
 */
void exercise5(void) {
    printf("\n=== Exercise 5: Matrix Transpose ===\n");
    
    int original[2][3] = {
        {1, 2, 3},
        {4, 5, 6}
    };
    
    int transposed[3][2];
    
    // TODO: Create transpose of original matrix
    
    // TODO: Print both matrices
    
    printf("(Complete the code in exercise5)\n");
}

/*
 * -----------------------------------------------------------------------------
 * EXERCISE 6: Add Two Matrices
 * -----------------------------------------------------------------------------
 * Add two 2x2 matrices and store the result in a third matrix.
 * 
 * A:          B:          C (A+B):
 *   1   2      5   6        6   8
 *   3   4      7   8       10  12
 */
void exercise6(void) {
    printf("\n=== Exercise 6: Add Two Matrices ===\n");
    
    int A[2][2] = {{1, 2}, {3, 4}};
    int B[2][2] = {{5, 6}, {7, 8}};
    int C[2][2];
    
    // TODO: Add A and B, store in C
    
    // TODO: Print all three matrices
    
    printf("(Complete the code in exercise6)\n");
}

/*
 * -----------------------------------------------------------------------------
 * EXERCISE 7: Count Even and Odd Numbers
 * -----------------------------------------------------------------------------
 * Count how many even and odd numbers are in a 2D array.
 * 
 * Matrix:
 *   1   2   3   4
 *   5   6   7   8
 *   9  10  11  12
 * 
 * Expected: Even = 6, Odd = 6
 */
void exercise7(void) {
    printf("\n=== Exercise 7: Count Even and Odd Numbers ===\n");
    
    int matrix[3][4] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}
    };
    
    // TODO: Count even and odd numbers
    
    printf("(Complete the code in exercise7)\n");
}

/*
 * -----------------------------------------------------------------------------
 * EXERCISE 8: Check if Matrix is Symmetric
 * -----------------------------------------------------------------------------
 * Check if a matrix is symmetric (matrix[i][j] == matrix[j][i] for all i,j).
 * 
 * Symmetric Matrix:     Non-Symmetric:
 *   1   2   3             1   2   3
 *   2   4   5             4   5   6
 *   3   5   6             7   8   9
 */
void exercise8(void) {
    printf("\n=== Exercise 8: Check Symmetric Matrix ===\n");
    
    int matrix1[3][3] = {
        {1, 2, 3},
        {2, 4, 5},
        {3, 5, 6}
    };
    
    int matrix2[3][3] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };
    
    // TODO: Check if each matrix is symmetric
    // A matrix is symmetric if matrix[i][j] == matrix[j][i]
    
    printf("(Complete the code in exercise8)\n");
}

/*
 * -----------------------------------------------------------------------------
 * EXERCISE 9: Multiply Two Matrices
 * -----------------------------------------------------------------------------
 * Multiply a 2x3 matrix by a 3x2 matrix to get a 2x2 result.
 * 
 * A[2x3]:        B[3x2]:        C[2x2]:
 *   1  2  3        7   8          58   64
 *   4  5  6        9  10         139  154
 *                 11  12
 * 
 * C[i][j] = Sum of A[i][k] * B[k][j] for all k
 */
void exercise9(void) {
    printf("\n=== Exercise 9: Matrix Multiplication ===\n");
    
    int A[2][3] = {{1, 2, 3}, {4, 5, 6}};
    int B[3][2] = {{7, 8}, {9, 10}, {11, 12}};
    int C[2][2] = {0};
    
    // TODO: Multiply A and B, store result in C
    
    // TODO: Print all three matrices
    
    printf("(Complete the code in exercise9)\n");
}

/*
 * -----------------------------------------------------------------------------
 * EXERCISE 10: Create a Magic Square
 * -----------------------------------------------------------------------------
 * Verify if a 3x3 matrix is a magic square.
 * A magic square has the same sum for all rows, columns, and diagonals.
 * 
 * Magic Square:
 *   2   7   6      Row sums: 15
 *   9   5   1      Col sums: 15
 *   4   3   8      Diag sums: 15
 */
void exercise10(void) {
    printf("\n=== Exercise 10: Magic Square Verification ===\n");
    
    int matrix[3][3] = {
        {2, 7, 6},
        {9, 5, 1},
        {4, 3, 8}
    };
    
    // TODO: Calculate all row sums, column sums, and diagonal sums
    // TODO: Check if they are all equal
    // TODO: Print whether it's a magic square
    
    printf("(Complete the code in exercise10)\n");
}

/*
 * -----------------------------------------------------------------------------
 * EXERCISE 11: Spiral Print
 * -----------------------------------------------------------------------------
 * Print a 4x4 matrix in spiral order (clockwise from outside to inside).
 * 
 * Matrix:
 *   1   2   3   4
 *   5   6   7   8
 *   9  10  11  12
 *  13  14  15  16
 * 
 * Spiral Order: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
 */
void exercise11(void) {
    printf("\n=== Exercise 11: Spiral Print ===\n");
    
    int matrix[4][4] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12},
        {13, 14, 15, 16}
    };
    
    // TODO: Print matrix in spiral order
    // Hint: Use variables for top, bottom, left, right boundaries
    
    printf("(Complete the code in exercise11)\n");
}

/*
 * -----------------------------------------------------------------------------
 * EXERCISE 12: 3D Array Practice
 * -----------------------------------------------------------------------------
 * Create a 2x3x4 array and:
 * 1. Fill it with consecutive numbers 1 to 24
 * 2. Print it layer by layer
 * 3. Find the sum of all elements
 */
void exercise12(void) {
    printf("\n=== Exercise 12: 3D Array Practice ===\n");
    
    int cube[2][3][4];
    
    // TODO: Fill with consecutive numbers 1 to 24
    
    // TODO: Print layer by layer
    
    // TODO: Calculate and print sum
    
    printf("(Complete the code in exercise12)\n");
}

/*
 * =============================================================================
 * SOLUTIONS (Uncomment to check your answers)
 * =============================================================================
 */

/*
void solution1(void) {
    printf("\n=== Solution 1 ===\n");
    
    int matrix[3][3] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };
    
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%3d ", matrix[i][j]);
        }
        printf("\n");
    }
}

void solution2(void) {
    printf("\n=== Solution 2 ===\n");
    
    int matrix[3][3] = {
        {5, 2, 8},
        {3, 9, 4},
        {7, 1, 6}
    };
    
    int sum = 0;
    for (int i = 0; i < 3; i++) {
        sum += matrix[i][i];
    }
    
    printf("Sum of diagonal elements: %d\n", sum);
}

void solution3(void) {
    printf("\n=== Solution 3 ===\n");
    
    int matrix[3][3] = {
        {23, 45, 12},
        {67, 34, 89},
        {15, 78, 56}
    };
    
    int max = matrix[0][0];
    int maxRow = 0, maxCol = 0;
    
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if (matrix[i][j] > max) {
                max = matrix[i][j];
                maxRow = i;
                maxCol = j;
            }
        }
    }
    
    printf("Largest element: %d at position [%d][%d]\n", max, maxRow, maxCol);
}

void solution10(void) {
    printf("\n=== Solution 10: Magic Square ===\n");
    
    int matrix[3][3] = {
        {2, 7, 6},
        {9, 5, 1},
        {4, 3, 8}
    };
    
    int rowSums[3] = {0}, colSums[3] = {0};
    int mainDiag = 0, antiDiag = 0;
    
    // Calculate sums
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            rowSums[i] += matrix[i][j];
            colSums[j] += matrix[i][j];
        }
        mainDiag += matrix[i][i];
        antiDiag += matrix[i][2-i];
    }
    
    // Check if all sums are equal
    int magicSum = rowSums[0];
    int isMagic = 1;
    
    for (int i = 0; i < 3; i++) {
        if (rowSums[i] != magicSum || colSums[i] != magicSum) {
            isMagic = 0;
            break;
        }
    }
    if (mainDiag != magicSum || antiDiag != magicSum) {
        isMagic = 0;
    }
    
    printf("Matrix is %s magic square (magic sum = %d)\n", 
           isMagic ? "a" : "NOT a", magicSum);
}
*/

/*
 * =============================================================================
 * MAIN FUNCTION
 * =============================================================================
 */
int main(void) {
    printf("╔════════════════════════════════════════════════════════════╗\n");
    printf("ā•‘     MULTI-DIMENSIONAL ARRAYS - PRACTICE EXERCISES          ā•‘\n");
    printf("ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•\n");
    
    exercise1();
    exercise2();
    exercise3();
    exercise4();
    exercise5();
    exercise6();
    exercise7();
    exercise8();
    exercise9();
    exercise10();
    exercise11();
    exercise12();
    
    printf("\n════════════════════════════════════════════════════════════\n");
    printf("Complete each exercise by filling in the TODO sections.\n");
    printf("Uncomment the solution functions to verify your answers.\n");
    printf("════════════════════════════════════════════════════════════\n");
    
    return 0;
}
Exercises - C Programming Tutorial | DeepML