Day 2 of DSA in C

Write a program to multiply two matrices.

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

    printf("Enter elements of first 3×3 matrix:\n");
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            scanf("%d", &a[i][j]);

    printf("Enter elements of second 3×3 matrix:\n");
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            scanf("%d", &b[i][j]);

    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            for (int k = 0; k < 3; k++)
                result[i][j] += a[i][k] * b[k][j];

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

    return 0;
}
        
        
    
Step 1:

Include the stdio.h header file.

  • This enables use of input/output functions like printf and scanf.

Step 2:

Declare three 3×3 integer matrices: a, b, and result.

  • a and b store user-input matrices.

  • result is initialized to zero to store the product.

Step 3:

Input elements for the first matrix a.

  • Use nested for loops to iterate over rows and columns.

  • Read each element using scanf.

Step 4:

Input elements for the second matrix b.

  • Same logic as above; elements are stored in matrix b.

Step 5:

Perform matrix multiplication and store in result.

  • Use three nested loops:

    • i iterates over rows of a.
    • j iterates over columns of b.
    • k does the dot product: a[i][k] * b[k][j].
  • Accumulate the result in result[i][j].

Step 6:

Display the resulting product matrix.

  • Use nested loops to print each element of result row by row.

Step 7:

The program ends with return 0;.

Write a program to find transpose of a matrix.

        
#include <stdio.h>
int main() {
    int rows, cols;
    int matrix[50][50], transpose[50][50];

    printf("Enter number of rows and columns: ");
    scanf("%d %d", &rows, &cols);

    printf("Enter elements of the matrix (%d × %d):\n", rows, cols);
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            scanf("%d", &matrix[i][j]);

    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            transpose[j][i] = matrix[i][j];


    printf("Transpose of the matrix (%d × %d):\n", cols, rows);
    for (int i = 0; i < cols; i++) {
        for (int j = 0; j < rows; j++)
            printf("%d ", transpose[i][j]);
        printf("\n");
    }

    return 0;
}       
        
    
Step 1:

Include the stdio.h header file.

  • Enables standard input and output functions like scanf and printf.

Step 2:

Declare variables for matrix dimensions and the matrices.

  • rows and cols hold the number of rows and columns.

  • matrix stores the original matrix.

  • transpose will hold the transposed matrix.

Step 3:

Input the matrix dimensions from the user.

  • Prompt the user and read values into rows and cols.

Step 4:

Input elements of the original matrix.

  • Use nested for loops to traverse the matrix.

  • Store each element in matrix[i][j].

Step 5:

Calculate the transpose of the matrix.

  • Use nested loops to copy matrix[i][j] into transpose[j][i].

Step 6:

Display the transposed matrix.

  • Use nested for loops to print each element of transpose.

  • The transposed matrix will have dimensions cols × rows.

Step 7:

End the program with return 0;.

Write a program to find trace of a matrix.

        
#include <stdio.h>
int main() {
    int matrix[50][50], size, trace = 0;

    printf("Enter size of square matrix (n for n×n): ");
    scanf("%d", &size);

    printf("Enter elements of the %d×%d matrix:\n", size, size);
    for (int i = 0; i < size; i++)
        for (int j = 0; j < size; j++)
            scanf("%d", &matrix[i][j]);

    for (int i = 0; i < size; i++)
        trace += matrix[i][i];

    printf("Trace of the matrix: %d\n", trace);

    return 0;
}
        
        
    
Step 1:

Include the stdio.h header file.

  • Provides access to standard input/output functions like scanf and printf.

Step 2:

Declare variables.

  • matrix: A 2D array to hold up to a 50×50 matrix.

  • size: Holds the dimension of the square matrix (n).

  • trace: Initialized to 0; will store the sum of diagonal elements.

Step 3:

Take input for the size of the square matrix.

  • User enters the value of n for an n×n matrix.

Step 4:

Input elements into the matrix.

  • Nested loops iterate over rows and columns to read matrix elements.

Step 5:

Calculate the trace of the matrix.

  • Only diagonal elements matrix[i][i] are added to trace.

  • Loop runs from 0 to size - 1.

Step 6:

Display the trace.

  • printf is used to output the result.

Step 7:

End the program with return 0;.

Write a program to find the maximum element of a matrix.

        
#include <stdio.h>
int main() {
    int rows, cols, matrix[50][50], max;

    printf("Enter number of rows and columns: ");
    scanf("%d %d", &rows, &cols);

    printf("Enter elements of the matrix (%d×%d):\n", rows, cols);
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            scanf("%d", &matrix[i][j]);

    max = matrix[0][0];

    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            if (matrix[i][j] > max)
                max = matrix[i][j];

    printf("Maximum element in the matrix: %d\n", max);

    return 0;
}     
        
    
Step 1:

Include the stdio.h header file.

  • Enables standard input/output operations using scanf and printf.

Step 2:

Declare variables.

  • rows and cols: to store matrix dimensions.

  • matrix[50][50]: a 2D array to hold matrix elements.

  • max: to store the maximum value found in the matrix.

Step 3:

Input matrix dimensions from the user.

  • Prompt the user and read rows and cols.

Step 4:

Read the matrix elements.

  • Use nested for loops to read each element using scanf.

Step 5:

Initialize max with the first matrix element.

  • max = matrix[0][0]; ensures comparison starts correctly.

Step 6:

Find the maximum element.

  • Use nested loops to compare each element with max.

  • Update max if a greater value is found.

Step 7:

Print the maximum element.

  • Display the result using printf.

Step 8:

End the program using return 0;.

Comments

Popular Posts