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;
}
Include the stdio.h
header file.
This enables use of input/output functions like
printf
andscanf
.
Declare three 3×3 integer matrices: a
, b
, and result
.
a
andb
store user-input matrices.result
is initialized to zero to store the product.
Input elements for the first matrix a
.
Use nested
for
loops to iterate over rows and columns.Read each element using
scanf
.
Input elements for the second matrix b
.
Same logic as above; elements are stored in matrix
b
.
Perform matrix multiplication and store in result
.
Use three nested loops:
i
iterates over rows ofa
.j
iterates over columns ofb
.k
does the dot product:a[i][k] * b[k][j]
.
Accumulate the result in
result[i][j]
.
Display the resulting product matrix.
Use nested loops to print each element of
result
row by row.
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;
}
Include the stdio.h
header file.
Enables standard input and output functions like
scanf
andprintf
.
Declare variables for matrix dimensions and the matrices.
rows
andcols
hold the number of rows and columns.matrix
stores the original matrix.transpose
will hold the transposed matrix.
Input the matrix dimensions from the user.
Prompt the user and read values into
rows
andcols
.
Input elements of the original matrix.
Use nested
for
loops to traverse the matrix.Store each element in
matrix[i][j]
.
Calculate the transpose of the matrix.
Use nested loops to copy
matrix[i][j]
intotranspose[j][i]
.
Display the transposed matrix.
Use nested
for
loops to print each element oftranspose
.The transposed matrix will have dimensions
cols × rows
.
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;
}
Include the stdio.h
header file.
Provides access to standard input/output functions like
scanf
andprintf
.
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.
Take input for the size of the square matrix.
User enters the value of
n
for ann×n
matrix.
Input elements into the matrix.
Nested loops iterate over rows and columns to read matrix elements.
Calculate the trace of the matrix.
Only diagonal elements
matrix[i][i]
are added totrace
.Loop runs from
0
tosize - 1
.
Display the trace.
printf
is used to output the result.
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;
}
Include the stdio.h
header file.
Enables standard input/output operations using
scanf
andprintf
.
Declare variables.
rows
andcols
: to store matrix dimensions.matrix[50][50]
: a 2D array to hold matrix elements.max
: to store the maximum value found in the matrix.
Input matrix dimensions from the user.
Prompt the user and read
rows
andcols
.
Read the matrix elements.
Use nested
for
loops to read each element usingscanf
.
Initialize max
with the first matrix element.
max = matrix[0][0];
ensures comparison starts correctly.
Find the maximum element.
Use nested loops to compare each element with
max
.Update
max
if a greater value is found.
Print the maximum element.
Display the result using
printf
.
End the program using return 0;
.
Comments