0% found this document useful (0 votes)
28 views

Project Cse

This C program performs matrix operations - addition, subtraction, and multiplication - on 2D arrays. It defines functions to add, subtract, and multiply two matrices. It takes input from the user to select an operation and the matrix dimensions. It calls the corresponding function, passing the input matrices, performs the operation, and displays the output matrix. It repeats this process in a loop until the user selects the exit option.

Uploaded by

Joy Rahman
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Project Cse

This C program performs matrix operations - addition, subtraction, and multiplication - on 2D arrays. It defines functions to add, subtract, and multiply two matrices. It takes input from the user to select an operation and the matrix dimensions. It calls the corresponding function, passing the input matrices, performs the operation, and displays the output matrix. It repeats this process in a loop until the user selects the exit option.

Uploaded by

Joy Rahman
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <stdio.

h>
#include <stdlib.h>

void add(int r, int k, int m[][k], int n[][k], int sum[][k]) {


for (int i = 0; i < r; i++)
for (int j = 0; j < k; j++)
sum[i][j] = m[i][j] + n[i][j];
}
void subtract(int r, int k, int m[][k], int n[][k], int result[][k]) {
for (int i = 0; i < r; i++)
for (int j = 0; j < k; j++)
result[i][j] = m[i][j] - n[i][j];
}
void multiply(int r, int k, int m[][k], int n[][k], int result[][k]) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < k; j++) {
result[i][j] = 0;
for (int L = 0; L < 3; L++)
result[i][j] += m[i][L] * n[L][j];
}
}
}
void display(int r, int k, int matrix[][k]) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < k; j++)
printf("%d\t", matrix[i][j]);
printf("\n");
}
}
int main() {

int r, k;
printf("Enter row: ");
scanf("%d", &r);
printf("Enter column: ");
scanf("%d", &k);
printf("Enter elements of first matrix: ");
int a[r][k], b[r][k], c[r][k];
for (int i = 0; i < r; i++) {
for (int j = 0; j < k; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Enter elements of second matrix: ");
for (int i = 0; i < r; i++) {
for (int j = 0; j < k; j++) {
scanf("%d", &b[i][j]);
}
}

// print both matrix


printf("First Matrix:\n");
display(r, k, a);
printf("Second Matrix:\n");
display(r, k, b);

int choice;
do {
printf("\nChoose the matrix operation,\n");
printf("----------------------------\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");

printf("4. Exit\n");
printf("----------------------------\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
add(r, k, a, b, c);
printf("Sum of matrix: \n");
display(r, k, c);
break;
case 2:
subtract(r, k, a, b, c);
printf("Subtraction of matrix: \n");
display(r, k, c);
break;
case 3:
multiply(r, k, a, b, c);
printf("Multiplication of matrix: \n");
display(r, k, c);
break;
case 4:
printf("Thank You.\n");
exit(0);
default:
printf("Invalid input.\n");
printf("Please enter the correct input.\n");
}
} while (1);

return 0;

#Algorithm:

1.Start

2.Make a function for addition,substract,multiply which provide the


addition,subtraction,multiplication
of two 2d arrays by using loop

3.Use void add function for addition

4.Use void substract function for subtraction


5.Use void multiply function for multiplication

6. Take two arrays a and b

7. Display both matris using display function

8. Take a integer choice 9. Take the choice from the user

10. define choice as a switch

11.When the value of choice is 1 use add function then use display function to show the value

12.When the value of choice is 2 use substract function then use display function to show the
value

13.When the value of choice is 3 use multiply function then use display function to show the
value

14.When the value of choice is 4 then u display thank you

15.When choice is anything else show “Please enter the correct input “And run from the
beginning
16.stop

You might also like