0% found this document useful (0 votes)
3 views9 pages

Cs Micro Project

The document details a micro-project from B. P. Poddar Institute of Management and Technology focused on creating a C program for performing basic operations on two-dimensional matrices, including addition, subtraction, and multiplication. It outlines the program's structure, user input requirements, operation selection, and processing methods, along with the complete code implementation. The project aims to enhance understanding of array manipulation and fundamental programming concepts in C.

Uploaded by

papercloudds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Cs Micro Project

The document details a micro-project from B. P. Poddar Institute of Management and Technology focused on creating a C program for performing basic operations on two-dimensional matrices, including addition, subtraction, and multiplication. It outlines the program's structure, user input requirements, operation selection, and processing methods, along with the complete code implementation. The project aims to enhance understanding of array manipulation and fundamental programming concepts in C.

Uploaded by

papercloudds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

B. P.

Poddar Institute of Management and Technology

Micro-project Details

Paper Name: PROGRAMMING FOR PROBLEM SOLVING

Paper Code: ES-CS291

Group: F

A simple C programme to perform operations on


Micro-project Title:
mathematical matrices.

Student Details:

Stream & Section:

IT-B
Semester: 2nd

Academic Year: 2024-25

Sl.no Name University Roll No.

01 SHREYASI LAHA 11500224106

02 SIKRITI DAS 11500224107

03 SIMRAN BERA 11500224108


TITLE: A simple C programme to perform operations on
mathematical matrices.
About the programme:
The C program developed aims to perform basic operations on two-
dimensional matrices, including addition, subtraction, and multiplication.
1. User Input:
o The program asks the user to enter the size (rows and columns) of
the matrices.
o The user then inputs the values for one or two matrices, depending
on the operation.
2. Operation Selection:
o The user selects an operation from a menu: addition, subtraction,
multiplication, or transpose.
3. Processing:
o The program performs the selected operation using loops and
stores the result in a new matrix.
4. Output:
o The resulting matrix is displayed to the user in a structured format.
5. Modular Code:
o Each operation is usually handled by a separate function to keep
the code clean and organized.
CODE:
#include <stdio.h>

#define SIZE 10

void readMatrix(int mat[SIZE][SIZE], int rows, int cols) {


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

void printMatrix(int mat[SIZE][SIZE], int rows, int cols) {


for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++)
printf("%d\t", mat[i][j]);
printf("\n");
}
}

void addMatrices(int a[SIZE][SIZE], int b[SIZE][SIZE], int res[SIZE][SIZE],


int rows, int cols) {
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
res[i][j] = a[i][j] + b[i][j];
}
void subtractMatrices(int a[SIZE][SIZE], int b[SIZE][SIZE], int
res[SIZE][SIZE], int rows, int cols) {
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
res[i][j] = a[i][j] - b[i][j];
}

void multiplyMatrices(int a[SIZE][SIZE], int b[SIZE][SIZE], int


res[SIZE][SIZE], int r1, int c1, int c2) {
for(int i = 0; i < r1; i++)
for(int j = 0; j < c2; j++) {
res[i][j] = 0;
for(int k = 0; k < c1; k++)
res[i][j] += a[i][k] * b[k][j];
}
}

int main() {
int mat1[SIZE][SIZE], mat2[SIZE][SIZE], result[SIZE][SIZE];
int r1, c1, r2, c2;
int choice;

printf("Enter rows and columns for Matrix 1: ");


scanf("%d%d", &r1, &c1);
readMatrix(mat1, r1, c1);

printf("Enter rows and columns for Matrix 2: ");


scanf("%d%d", &r2, &c2);
readMatrix(mat2, r2, c2);

printf("\nChoose Operation:\n1. Addition\n2. Subtraction\n3.


Multiplication\n");
scanf("%d", &choice);

switch(choice) {
case 1:
if (r1 == r2 && c1 == c2) {
addMatrices(mat1, mat2, result, r1, c1);
printf("Result (Addition):\n");
printMatrix(result, r1, c1);
} else {
printf("Addition not possible: matrices must be of the same size.\n");
}
break;
case 2:
if (r1 == r2 && c1 == c2) {
subtractMatrices(mat1, mat2, result, r1, c1);
printf("Result (Subtraction):\n");
printMatrix(result, r1, c1);
} else {
printf("Subtraction not possible: matrices must be of the same
size.\n");
}
break;
case 3:
if (c1 == r2) {
multiplyMatrices(mat1, mat2, result, r1, c1, c2);
printf("Result (Multiplication):\n");
printMatrix(result, r1, c2);
} else {
printf("Multiplication not possible: columns of Matrix 1 must match
rows of Matrix 2.\n");
}
break;
default:
printf("Invalid choice.\n");
}
return 0;

Algorithm: Matrix Operations


Step 1: Start
Step 2: Input number of rows and columns of matrices (say r and c)
Step 3: Input elements of first matrix A[r][c]
Step 4: Input elements of second matrix B[r][c]
Step 5: Display a menu for user to choose an operation:
1. Addition
2. Subtraction
3. Multiplication
Step 6:
If option is 1:
Perform A + B and store in C
If option is 2:
Perform A - B and store in C
If option is 3:
Check if columns of A = rows of B
If yes, perform A * B and store in C
Else, display "Multiplication not possible"
Step 7: Display result matrix C
Step 8: End

OUTPUT:

FOR ADDITION:

FOR SUBTRACTION:
FOR MULTIPLICATION:
DISCUSSION:

The C program developed aims to perform basic operations on two-


dimensional matrices, including addition, subtraction, and multiplication.
Matrix operations are fundamental in various fields such as engineering,
physics, and computer science, and implementing them in C provides a good
understanding of array manipulation, loops, and function usage.
The program starts by accepting input from the user for the dimensions and
elements of two matrices. For addition and subtraction, the program checks if
the two matrices are of the same order (same number of rows and columns). If
the matrices are compatible, it proceeds to perform element-wise addition or
subtraction using nested loops.
For matrix multiplication, the program ensures that the number of columns in
the first matrix is equal to the number of rows in the second matrix — a
necessary condition for multiplication. The multiplication is carried out using
three nested loops, which handle the row-by-column multiplication and the
summation of the products.
In conclusion, the project demonstrates a practical implementation of matrix
operations using fundamental C programming concepts. It not only reinforces
the understanding of arrays and loops but also introduces users to
mathematical logic in programming.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy