Day 8 of Programming in C

Write a program to multiply 2 matrices of dimension 3x3.


#include <stdio.h>
void main()
{
    int mat1[3][3], mat2[3][3], mult[3][3] = {0};
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            printf("Enter elements [%d][%d] of matrix 1: ", i + 1, j + 1);
            scanf("%d", &mat1[i][j]);
        }
    }
    printf("Matrix 1 :\n");
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            printf("%d ", mat1[i][j]);
        }
        printf("\n");
    }
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            printf("Enter elements [%d][%d] of matrix 2: ", i + 1, j + 1);
            scanf("%d", &mat2[i][j]);
        }
    }
    printf("Matrix 2 :\n");
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            printf("%d ", mat2[i][j]);
        }
        printf("\n");
    }
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            for (int k = 0; k < 3; k++)
            {
                mult[i][j] += (mat1[i][k] * mat2[k][j]);
            }
        }
    }
    printf("Product :\n");
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            printf("%d ", mult[i][j]);
        }
        printf("\n");
    }
}
    
    
Step 1:

Include the `stdio.h` header file.

  • This header provides standard input/output functions, allowing the program to interact with the user via `printf` (for output) and `scanf` (for input).

Step 2:

Define the `main` function.

  • This is the entry point of the program.

Step 3:

Declare three 3x3 integer matrices: `mat1`, `mat2`, and `mult`.

  • `mat1` and `mat2` will store the input matrices.

  • `mult` will store the product of the matrices, initialized to all zeros.

Step 4:

Prompt the user to enter the elements of `mat1`.

  • Use nested loops to iterate through each element of `mat1`.

  • Use `printf` to display a prompt for each element.

  • Use `scanf` to read the input and store it in the corresponding element of `mat1`.

Step 5:

Print the elements of `mat1` to the console.

  • Use nested loops to iterate through each element of `mat1`.

  • Use `printf` to display each element, followed by a space.

  • Print a newline character after each row.

Step 6:

Prompt the user to enter the elements of `mat2`.

  • Use nested loops to iterate through each element of `mat2`.

  • Use `printf` to display a prompt for each element.

  • Use `scanf` to read the input and store it in the corresponding element of `mat2`.

Step 7:

Print the elements of `mat2` to the console.

  • Use nested loops to iterate through each element of `mat2`.

  • Use `printf` to display each element, followed by a space.

  • Print a newline character after each row.

Step 8:

Calculate the product of `mat1` and `mat2` and store it in `mult`.

  • Use three nested loops: `i`, `j`, and `k`.

  • The outer loops (`i` and `j`) iterate through the rows and columns of `mult`.

  • The inner loop (`k`) iterates through the columns of `mat1` and the rows of `mat2`.

  • For each element `mult[i][j]`, calculate the sum of `mat1[i][k] * mat2[k][j]` for all values of `k`.

Step 9:

Print the elements of `mult` to the console.

  • Use nested loops to iterate through each element of `mult`.

  • Use `printf` to display each element, followed by a space.

  • Print a newline character after each row.

Step 10:

The program execution ends.

Write a program to multiply 3 matrices each of dimension of 3x3.


#include <stdio.h>
void main()
{
    int mat[3][3][3];
    int temp[3][3] = {0}, result[3][3] = {0};

    for (int k = 0; k < 3; k++)
    {
        printf("Enter elements of Matrix %d:\n", k + 1);
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                scanf("%d", &mat[k][i][j]);
            }
        }
    }

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            temp[i][j] = 0;
            for (int k = 0; k < 3; k++)
            {
                temp[i][j] += mat[0][i][k] * mat[1][k][j];
            }
        }
    }

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            result[i][j] = 0;
            for (int k = 0; k < 3; k++)
            {
                result[i][j] += temp[i][k] * mat[2][k][j];
            }
        }
    }

    printf("Final Product:\n");
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            printf("%d ", result[i][j]);
        }
        printf("\n");
    }
}

        
    
    
Step 1:

Include the `stdio.h` header file.

  • This header provides standard input/output functions, allowing the program to interact with the user via `printf` (for output) and `scanf` (for input).

Step 2:

Define the `main` function.

  • This is the entry point of the program.

Step 3:

Declare a 3x3x3 integer array `mat` to store three 3x3 matrices.

Step 4:

Declare two 3x3 integer arrays `temp` and `result`, both initialized to zero.

  • `temp` will store the product of the first two matrices.

  • `result` will store the final product of all three matrices.

Step 5:

Prompt the user to enter the elements of the three matrices.

  • Use a `for` loop (with `k`) to iterate through each of the three matrices.

  • Use nested `for` loops (with `i` and `j`) to iterate through the rows and columns of each matrix.

  • Use `printf` to display a prompt indicating which matrix is being entered.

  • Use `scanf` to read the input and store it in the corresponding element of the `mat` array.

Step 6:

Calculate the product of the first two matrices and store it in `temp`.

  • Use nested `for` loops (with `i` and `j`) to iterate through the rows and columns of `temp`.

  • Initialize `temp[i][j]` to 0 before calculating the product.

  • Use another `for` loop (with `k`) to iterate through the columns of the first matrix and the rows of the second matrix.

  • Calculate `mat[0][i][k] * mat[1][k][j]` and add it to `temp[i][j]`.

Step 7:

Calculate the product of `temp` (the result of the first two matrices) and the third matrix, storing the result in `result`.

  • Use nested `for` loops (with `i` and `j`) to iterate through the rows and columns of `result`.

  • Initialize `result[i][j]` to 0 before calculating the product.

  • Use another `for` loop (with `k`) to iterate through the columns of `temp` and the rows of the third matrix.

  • Calculate `temp[i][k] * mat[2][k][j]` and add it to `result[i][j]`.

Step 8:

Print the final product stored in `result`.

  • Use nested `for` loops (with `i` and `j`) to iterate through the rows and columns of `result`.

  • Use `printf` to display each element of `result`, followed by a space.

  • Print a newline character after each row.

Step 9:

The program execution ends.

Write a program to find transpose of a matrix.


#include <stdio.h>
void main()
{
    int mat[3][3];
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            printf("Enter elements [%d][%d] of matrix : ", i + 1, j + 1);
            scanf("%d", &mat[i][j]);
        }
    }
    printf("Matrix :\n");
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            printf("%d ", mat[i][j]);
        }
        printf("\n");
    }
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            if (i < j)
            {
                mat[i][j] = mat[i][j] + mat[j][i] - (mat[j][i] = mat[i][j]);
            }
        }
    }
    printf("Transpose :\n");
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            printf("%d ", mat[i][j]);
        }
        printf("\n");
    }
}

        
    
Step 1:

Include the `stdio.h` header file.

  • This header provides standard input/output functions, allowing the program to interact with the user via `printf` (for output) and `scanf` (for input).

Step 2:

Define the `main` function.

  • This is the entry point of the program.

Step 3:

Declare a 3x3 integer array `mat` to store the matrix.

Step 4:

Prompt the user to enter the elements of the matrix.

  • Use nested `for` loops (with `i` and `j`) to iterate through the rows and columns of the matrix.

  • Use `printf` to display a prompt for each element.

  • Use `scanf` to read the input and store it in the corresponding element of the `mat` array.

Step 5:

Print the entered matrix to the console.

  • Use nested `for` loops to iterate through each element of the matrix.

  • Use `printf` to display each element, followed by a space.

  • Print a newline character after each row.

Step 6:

Calculate the transpose of the matrix.

  • Use nested `for` loops (with `i` and `j`) to iterate through the upper triangular part of the matrix (excluding the diagonal).

  • Use an `if` statement to check if `i < j`, ensuring that only elements above the diagonal are swapped.

  • Use the line `mat[i][j] = mat[i][j] + mat[j][i] - (mat[j][i] = mat[i][j]);` to swap `mat[i][j]` and `mat[j][i]` without using a temporary variable.

  • This line cleverly swaps the values in one statement using addition, subtraction, and assignment.

Step 7:

Print the transposed matrix to the console.

  • Use nested `for` loops to iterate through each element of the transposed matrix.

  • Use `printf` to display each element, followed by a space.

  • Print a newline character after each row.

Step 8:

The program execution ends.

Write a program to find determinant of a matrix of 3x3.


#include <stdio.h>
void main()
{
    int mat[3][3];
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            printf("Enter elements [%d][%d] of matrix : ", i + 1, j + 1);
            scanf("%d", &mat[i][j]);
        }
    }
    printf("Matrix :\n");
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            printf("%d ", mat[i][j]);
        }
        printf("\n");
    }
    int determinant = mat[0][0] * (mat[1][1] * mat[2][2] - mat[1][2] * mat[2][1]) - mat[0][1] * (mat[1][0] * mat[2][2] - mat[1][2] * mat[2][0]) + mat[0][2] * (mat[1][0] * mat[2][1] - mat[1][1] * mat[2][0]);

    printf("Determinant of the matrix = %d\n", determinant);
}
  
 
    
Step 1:

Include the `stdio.h` header file.

  • This header provides standard input/output functions, allowing the program to interact with the user via `printf` (for output) and `scanf` (for input).

Step 2:

Define the `main` function.

  • This is the entry point of the program.

Step 3:

Declare a 3x3 integer array `mat` to store the matrix.

Step 4:

Prompt the user to enter the elements of the matrix.

  • Use nested `for` loops (with `i` and `j`) to iterate through the rows and columns of the matrix.

  • Use `printf` to display a prompt for each element.

  • Use `scanf` to read the input and store it in the corresponding element of the `mat` array.

Step 5:

Print the entered matrix to the console.

  • Use nested `for` loops to iterate through each element of the matrix.

  • Use `printf` to display each element, followed by a space.

  • Print a newline character after each row.

Step 6:

Calculate the determinant of the matrix.

  • Use the formula for the determinant of a 3x3 matrix:

  • `determinant = mat[0][0] * (mat[1][1] * mat[2][2] - mat[1][2] * mat[2][1]) - mat[0][1] * (mat[1][0] * mat[2][2] - mat[1][2] * mat[2][0]) + mat[0][2] * (mat[1][0] * mat[2][1] - mat[1][1] * mat[2][0]);`

  • This formula expands the determinant along the first row.

Step 7:

Print the calculated determinant to the console.

  • Use `printf` to display the determinant.

Step 8:

The program execution ends.

Comments

Popular Posts