Array
1) Read the marks of 5 students and display
Algorithm:
Step 1: Start
Step 2: Repeat for each student from 1 to 5
Step 3: Read the mark
Step 4: Store it in a array
Step 5: Display marks of all 5 students
Step 6: Stop
Pseudocode:
START
DECLARE marks[5]
FOR i <- 0 TO 4 DO
READ marks[i]
FOR I <- 0 TO 4 DO
PRINT marks [i]
END
Flowchart:
Start
Student_counter = 1
Yes No
Is Student_counter<= 5?
Read Mark Display all marks
Store mark in array
Stop
Increment counter
(+1)
Program:
#include <stdio.h>
int main() {
int marks[5]; // Array to store marks of 5 students
int i;
printf("Enter the marks of 5 students:\n");
for(i = 0; i < 5; i++) {
printf("Student %d: ", i + 1);
scanf("%d", &marks[i]);
}
printf("\nMarks of the 5 students are:\n");
for(i = 0; i < 5; i++) {
printf("Student %d: %d\n", i + 1, marks[i]);
}
return 0;
}
2) Insert for 5 subjects, 5 students and display
Algorithm:
Step 1: Start
Step 2: Create 2D array marks[5][5]
Step 3: For each students from 1 to 5
>For each subject from 1 to 5
>Read mark
Step 4:Display all marks
Step 5: Stop
Pseudocode:
START
DECLARE marks[5][5]
FOR i <- 0 TO 4 DO
FOR j <- 0 TO 4 DO
READ marks[i][j]
FOR i <- 0 TO 4 DO
FOR j <- 0 TO 4 DO
PRINT marks[i][j]
END FOR
PRINT new line
STOP
Flowchart:
Start
Initialize
marks[5][5]
i =0 to 4
j =0 to 4
Read marks[I] Store in array
[j]
Increment j
Yes No
j < 5? Increment i
Display
all marks
Stop
3) Addition of two matrix
Algorithm:
Step 1: Start
Step 2: Input matrix A and B of sixe mxn
Step 3: Initialize matrix C
Step 4: For each element: C[i][j]= A[i][j]+B[i][j]
Step 5: Display matrix C
Step 6: Stop
Pseudocode:
START
DECLARE A[m][n], B[m][n], C[m][n]
FOR i ← 0 TO m-1 DO
FOR j ← 0 TO n-1 DO
READ A[i][j]
END FOR
END FOR
FOR i ← 0 TO m-1 DO
FOR j ← 0 TO n-1 DO
READ B[i][j]
END FOR
END FOR
FOR i ← 0 TO m-1 DO
FOR j ← 0 TO n-1 DO
C[i][j] ← A[i][j] + B[i][j]
END FOR
END FOR
PRINT C
STOP
Flowchart:
Start
Declare A[i][j]
B[i][j] and c[i][j]
Read i,j
A[] [] and B[][]
jk
jk
i=0, j=0
Yes Yes c[i][j]= A[i][j]
i<r j<c +B[i][j]
No No
i = i +1 j=j+1
Print c
Stop
Program:
#include <stdio.h>
int main() {
int a[3][3], b[3][3], sum[3][3];
int i, j;
printf("Enter elements of first 3x3 matrix:\n");
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
scanf("%d", &a[i][j]);
printf("Enter elements of second 3x3 matrix:\n");
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
scanf("%d", &b[i][j]);
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
sum[i][j] = a[i][j] + b[i][j];
printf("Sum of matrices:\n");
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++)
printf("%d ", sum[i][j]);
printf("\n");
}
return 0;
}
4) Subtraction of two matrices
Algorithm:
Step 1: Start
Step 2: input matrix A and B
Step 3: initialize result matrix c
Step 4: For each element: C[i][j]
Step 5: Display matrix
Step 6: Stop
Pseudocode:
BEGIN
DECLARE A[3][3], B[3][3], DIFF[3][3]
DECLARE i, j as INTEGER
PRINT "Enter elements of first 3x3 matrix:"
FOR i ← 0 TO 2 DO
FOR j ← 0 TO 2 DO
INPUT A[i][j]
END FOR
END FOR
PRINT "Enter elements of second 3x3 matrix:"
FOR i ← 0 TO 2 DO
FOR j ← 0 TO 2 DO
INPUT B[i][j]
END FOR
END FOR
FOR i ← 0 TO 2 DO
FOR j ← 0 TO 2 DO
DIFF[i][j] ← A[i][j] - B[i][j]
END FOR
END FOR
PRINT "Resultant matrix after subtraction (A - B):"
FOR i ← 0 TO 2 DO
FOR j ← 0 TO 2 DO
PRINT DIFF[i][j], " "
END FOR
PRINT newline
END FOR
END
Flowchart:
Start
Declare A[i][j]
B[i][j] and c[i][j]
Read i,j
A[] [] and B[][]
i=0, j=0
Yes
Yes
i<r c[i][j]= A[i][j]
j<c
+B[i][j]
No
No
Print c
i = i +1 j=j+1
stop
Program :
#include <stdio.h>
int main() {
int a[3][3], b[3][3], diff[3][3];
int i, j;
printf("Enter elements of first 3x3 matrix:\n");
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
scanf("%d", &a[i][j]);
printf("Enter elements of second 3x3 matrix:\n");
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
scanf("%d", &b[i][j]);
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
diff[i][j] = a[i][j] - b[i][j];
printf("Difference of matrices:\n");
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++)
printf("%d ", diff[i][j]);
printf("\n");
}
return 0;
}
5) Transpose of matrix
Algorithm:
Step 1: Start
Step 2: read matrix A of size mxn
Step 3: Create matrix B of size nxm
Step 4: For each element: B[I][j] = A[I][j]
Step 5: Display matrix B
Step 6: Stop
Pseudocode:
START
DECLARE A[m][n], B[n][m]
FOR i <- 0 TO m-1 DO
FOR j <- 0 TO m-1 DO
READ A[I][j]
FOR i <- 0 TO m-1 DO
FOR j <- 0 TO m-1 DO
B[j][I] <- A[I][j]
PRINT B
END
Flowchart:
Start
Declare a[m][n] b[m]
[n]
Read matrix
a[m][n]
Declare variables I,j
i =0, j=0
B[I][j]=A[I][j]
i<n j<m
i=j+1
Print matrix
B i=i+1
Stop
Program:
#include <stdio.h>
#define MAX 10
void readMatrix(int matrix[MAX][MAX], int rows, int cols, int num) {
printf("Enter elements for Matrix %d (%dx%d):\n", num, rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Matrix %d [%d][%d]: ", num, i, j);
scanf("%d", &matrix[i][j]);
}
}
}
void printMatrix(int matrix[MAX][MAX], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}
void transposeMatrix(int original[MAX][MAX], int transpose[MAX][MAX], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transpose[j][i] = original[i][j];
}
}
}
int main() {
int rows1, cols1, rows2, cols2;
int matrix1[MAX][MAX], transpose1[MAX][MAX];
int matrix2[MAX][MAX], transpose2[MAX][MAX];
printf("Enter rows and columns for Matrix 1: ");
scanf("%d%d", &rows1, &cols1);
printf("Enter rows and columns for Matrix 2: ");
scanf("%d%d", &rows2, &cols2);
readMatrix(matrix1, rows1, cols1, 1);
readMatrix(matrix2, rows2, cols2, 2);
transposeMatrix(matrix1, transpose1, rows1, cols1);
transposeMatrix(matrix2, transpose2, rows2, cols2);
printf("\nTranspose of Matrix 1:\n");
printMatrix(transpose1, cols1, rows1); // Transposed dimensions are reversed
printf("\nTranspose of Matrix 2:\n");
printMatrix(transpose2, cols2, rows2);
return 0;
}