0% found this document useful (0 votes)
36 views20 pages

Matrix Calculation Project in C Programming

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 20

Advanced College of Engineering and Management

Department of Bachelor in Computer Applications

REPORTS FOR C - PROGRAMMING


ON
MATRIX CALCULATOR

Submitted By:
Saurav Subedi

Submitted To:
Er. Kiran Sharma
Department of Bachelor in Computer Applications
ACKNOWLEDGEMENT
I take this moment to extend my sincere appreciation to my teacher, Mr. Kiran Sharma, for his insightful
advice, motivating suggestions, and invaluable guidance that were instrumental in the successful
completion of this project.

My gratitude extends to the Head of the Computer Department, Ms. Kritika Gulati, for her essential
guidance and support in cultivating my skills. I also recognize the department's timely facilities,
contributing significantly to my academic journey.

A special thanks goes to the teaching and non-teaching staff of the Advanced College of Engineering and
Management for their dedicated help and support throughout my ongoing bachelor’s degree. I am also
appreciative of my classmates for their collaborative efforts, encouragement, and valuable suggestions.

Lastly, but most importantly, I express deep appreciation to my parents for their consistent support and
encouragement throughout my bachelor’s degree. Their constant guidance has been a pillar of strength
and motivation.

Saurav Subedi

i
Abstract
This report introduces the Matrix Calculator in C project, a user-friendly application facilitating efficient
matrix operations. Serving as a comprehensive tool for users across scientific and engineering disciplines,
the calculator addresses common challenges associated with manual matrix calculations. The Matrix
Calculator not only simplifies complex computations but also serves as an educational resource. The
report outlines the project's significance, identifies current challenges, and details the program's structure,
including user-defined functions and the main function. The calculator's dynamic features, such as matrix
size selection, transparent solution display, and user-friendly interface, make it a valuable asset for users
seeking an efficient and flexible solution to matrix-based problems. Future enhancements, including error
handling and additional operations, are envisioned to further optimize the Matrix Calculator's
functionality and usability.

ii
Table of Contents
ACKNOWLEDGEMENT ……………………………………………………………………………… i
ABSTRACT ……………….…………………………………………………………………………… ii

CHAPTER 1 : INTRODUCTION ………..…………………………………………………………….1


1.1 Introduction ………..……………………………………………………………………………..1
1.2 Key Features ………..…………………………………………………………………………….1
1.3 Significance of the Project ………..…………………………………………………………….1

CHAPTER 2: CURRENT SCENARIO ………..………………………………………………………2


2.1 Challenges in the Current System ………..……………………………………………………2

CHAPTER 3: Problem this Project Will Solve ………..…………………………………………3

CHAPTER 4: PROGRAM BREAKDOWN ………..…………………………………………………4


4.1 USER DEFINED FUNCTION ………..…………………………………………………………….4
4.2 MAIN FUNCTION ………..………………………………………………………………………9

CHAPTER 5: FUTURE ENHANCEMENTS ………..………………………………………………14

CHAPTER 6 : CONCLUSION ………..………………………………………………………………15


CHAPTER 7 : REFERENCES ………..………………………………………………………………16
CHAPTER 1: INTRODUCTION

1.1 Introduction
Matrix operations are fundamental in various scientific and engineering disciplines, including
mathematics, computer science, physics, and more. Performing matrix calculations manually can be
error-prone and time-consuming. To address these challenges, the Matrix Calculator in C is designed as a
user-friendly tool that empowers users to perform various matrix operations efficiently.

The Matrix Calculator in C project aims to streamline matrix-based problem-solving by providing a


versatile and transparent platform for performing operations on matrices of varying sizes. This abstract
outlines the key features, problem statement, and significance of the project.

1.2 Key Features:


Dynamic Matrix Size Selection:
Users can choose the size of matrices before entering elements, allowing flexibility for diverse matrix
dimensions.

Comprehensive Solution:
The project displays both the final results and specific details from each matrix, enhancing transparency
and usability.

User-Friendly Interface:
The intuitive interface enables users to perform matrix operations effortlessly.

Problem Statement:
The current matrix multiplication systems have limitations in handling matrices with different index sizes
and lack simultaneous result verification. The Matrix Calculator in C aims to overcome these limitations,
providing a user-centric and comprehensive solution.

1.3 Significance of the Project:

Matrix operations are foundational in numerous fields. The Matrix Calculator in C not only simplifies
complex matrix computations but also serves as an educational tool for understanding matrix-based
problem-solving principles.

1
CHAPTER 2: CURRENT SCENARIO
Matrix-based problem-solving is a common requirement in various disciplines. However, existing
systems for matrix multiplication are often limited in their ability to accommodate matrices of different
sizes. The Matrix Calculator in C seeks to address these limitations by offering a more flexible and
user-friendly system.

2.1 Challenges in the Current System:


I. Limited to matrices with fixed index sizes.
II. Inability to handle matrices of varying sizes.
III. Lack of simultaneous result verification.
IV. Absence of displaying entered elements for matrices larger than 2x2.

The Matrix Calculator in C aims to provide users with a comprehensive solution for efficient matrix
operations. In the following sections, we will explore the specific problems this project aims to solve and
the methodologies employed.

2
CHAPTER 3: PROBLEM THIS PROJECT WILL
SOLVE
Matrix multiplication is a fundamental operation, but the current system has limitations that hinder its
effectiveness and usability. The Matrix Calculator in C project aims to address the following problems
prevalent in the current scenario:

Limited Matrix Size Compatibility:


Existing systems are often restricted to matrices with fixed index sizes, making it challenging to perform
operations on matrices with varying dimensions. The Matrix Calculator in C will allow users to input
matrices of any size, providing a more flexible and versatile solution.

Lack of Simultaneous Result Verification:


The current system lacks the ability to verify results simultaneously, particularly for matrices larger than
2x2. The Matrix Calculator in C aims to offer users the capability to simultaneously view and verify their
results, enhancing the reliability of matrix operations.

Inability to Display Entered Elements for Larger Matrices:


As matrices grow in size, the current system fails to display the entered elements, limiting users' ability to
review and verify their input. The Matrix Calculator in C will address this limitation by providing users
with a comprehensive view of both the entered elements and the final results.

By solving these problems, the Matrix Calculator in C seeks to provide users with a more efficient,
transparent, and user-friendly tool for matrix-based problem-solving. The subsequent sections will delve
into the breakdown of the program, its functionalities, and how it tackles these identified issues.

3
CHAPTER 4. PROGRAM BREAKDOWN
The Matrix Calculator in C project is structured to provide users with a comprehensive set of
functionalities for efficient matrix operations. The breakdown of the program includes a series of
user-defined functions and a main function that orchestrates the overall functionality.

4.1 User-Defined Functions

Function: readMatrix

Purpose:
Reads matrix elements entered by the user.

Parameters:
Integer array for the matrix, number of rows, and number of columns.

void readMatrix(int array[10][10], int rows, int cols) {


int i, j;
for (i = 0; i < rows; i++) {
printf("\t%d entries for row %d: ", cols, i + 1);
for (j = 0; j < cols; j++) {
scanf("%d", &array[i][j]);
}
}
return;
}

4
Function: printMatrix

Purpose:
Prints the matrix entered by the user.

Parameters:
Integer array for the matrix, number of rows, and number of columns.

void printMatrix(int array[10][10], int rows, int cols) {


int i, j;

for (i = 0; i < rows; i++) {


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

5
Function: matrixAddSub

Purpose:
Adds or subtracts matrices based on user input.

Parameters:
Integer arrays for two matrices, the number of rows, the number of columns, and a multiplier for
subtraction.

void matrixAddSub(int arrayA[10][10], int arrayB[10][10], int result[10][10], int rows, int
cols, int operation) {
int i, j;

for (i = 0; i < rows; i++) {


for (j = 0; j < cols; j++) {
if (operation == ADD) {
result[i][j] = arrayA[i][j] + arrayB[i][j];
} else if (operation == SUBTRACT) {
result[i][j] = arrayA[i][j] - arrayB[i][j];
}
}
}
}

6
Function: matrixScalarMultiply

Purpose:
Performs scalar multiplication on a matrix.

Parameters:
Integer array for the matrix, scalar value, the number of rows, and the number of columns.

void matrixScalarMultiply(int array[10][10], int scalar, int result[10][10], int rows, int cols)
{
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
result[i][j] = scalar * array[i][j];
}
}
}

7
Function: matrixMultiply

Purpose:
Multiplies two matrices and prints the result.

Parameters:
Integer arrays for two matrices, the number of rows for the first matrix, the number of columns for the
first matrix, and the number of columns for the second matrix.

void matrixMultiply(int arrayA[10][10], int arrayB[10][10], int result[10][10], int rowsA,


int colsA, int colsB) {
int i, j, k;
// Initializing all elements of the result matrix to 0
for (i = 0; i < rowsA; ++i)
for (j = 0; j < colsB; ++j)
{
result[i][j] = 0;
}

// Multiplying matrices A and B and storing the result in the result matrix
for (i = 0; i < rowsA; ++i)
for (j = 0; j < colsB; ++j)
for (k = 0; k < colsA; ++k)
{
result[i][j] += arrayA[i][k] * arrayB[k][j];
}
printf("\nOutput Matrix:\n");
for (i = 0; i < rowsA; ++i)
for (j = 0; j < colsB; ++j)
{
printf("\t%d ", result[i][j]);
if (j == colsB - 1)
printf("\n\n");
}

8
4.2 Main Function
The main function serves as the control center for the program, offering users a menu of options for
matrix operations. Users can choose to add matrices, subtract matrices, perform scalar multiplication, or
multiply two matrices.

#include <stdio.h>
// Constants for matrix operations
#define ADD 1
#define SUBTRACT 2
// Function prototypes
void readMatrix(int array[10][10], int rows, int cols);
void printMatrix(int array[10][10], int rows, int cols);
void matrixAddSub(int arrayA[10][10], int arrayB[10][10], int result[10][10], int rows, int
cols, int operation);
void matrixScalarMultiply(int array[10][10], int scalar, int result[10][10], int rows, int
cols);
void matrixMultiply(int arrayA[10][10], int arrayB[10][10], int result[10][10], int rowsA,
int colsA, int colsB);
int main() {
// Variable declarations
int matrixA[10][10], matrixB[10][10], result[10][10];
int rowsA, colsA, rowsB, colsB, operation, scalar;
// User input for matrix A
printf("Enter the number of rows and columns for matrix A: ");
scanf("%d %d", &rowsA, &colsA);
readMatrix(matrixA, rowsA, colsA);
// User input for matrix B
printf("Enter the number of rows and columns for matrix B: ");
scanf("%d %d", &rowsB, &colsB);
readMatrix(matrixB, rowsB, colsB);
// Display matrices A and B
printf("\nMatrix A:\n");
printMatrix(matrixA, rowsA, colsA);
printf("\nMatrix B:\n");
printMatrix(matrixB, rowsB, colsB);
// Matrix operations menu
printf("\nOperation Menu:\n");

9
printf("1. Add\n");
printf("2. Subtract\n");
printf("3. Scalar Multiply\n");
printf("4. Multiply two matrices\n");
printf("Enter your choice: ");
scanf("%d", &operation);
// Perform the selected operation
switch (operation) {
case ADD:
case SUBTRACT:
matrixAddSub(matrixA, matrixB, result, rowsA, colsA, operation);
printf("\nResult of matrix operation:\n");
printMatrix(result, rowsA, colsA);
break;

case 3:
printf("Enter scalar value: ");
scanf("%d", &scalar);
matrixScalarMultiply(matrixA, scalar, result, rowsA, colsA);
printf("\nResult of scalar multiplication:\n");
printMatrix(result, rowsA, colsA);
break;

case 4:
if (colsA != rowsB) {
printf("Invalid matrix dimensions for multiplication.\n");
} else {
matrixMultiply(matrixA, matrixB, result, rowsA, colsA, colsB);
printf("\nResult of matrix multiplication:\n");
printMatrix(result, rowsA, colsB);
}
Break;
default:
printf("Invalid choice.\n");
break;
}
return 0;
}

10
Explanation of the Code:
Matrix Input:
Users are prompted to enter the number of rows and columns for matrices A and B. The readMatrix
function is called to input elements for both matrices.

printf("Enter the number of rows and columns for matrix A: ");


scanf("%d %d", &rowsA, &colsA);
readMatrix(matrixA, rowsA, colsA);

printf("Enter the number of rows and columns for matrix B: ");


scanf("%d %d", &rowsB, &colsB);
readMatrix(matrixB, rowsB, colsB);

Matrix Display:
The printMatrix function is called to display matrices A and B.

printf("\nMatrix A:\n");
printMatrix(matrixA, rowsA, colsA);

printf("\nMatrix B:\n");
printMatrix(matrixB, rowsB, colsB);

Operation Menu:
Users are presented with a menu to choose the desired matrix operation.

printf("\nOperation Menu:\n");
printf("1. Add\n");
printf("2. Subtract\n");
printf("3. Scalar Multiply\n");
printf("4. Multiply two matrices\n");
printf("Enter your choice: ");
scanf("%d", &operation);

11
Performing the Operation:
Depending on the user's choice, the corresponding matrix operation is performed using the appropriate
function (matrixAddSub, matrixScalarMultiply, or matrixMultiply). The result is then displayed using the
printMatrix function.

switch (operation) {
case ADD:
case SUBTRACT:
matrixAddSub(matrixA, matrixB, result, rowsA, colsA, operation);
printf("\nResult of matrix operation:\n");
printMatrix(result, rowsA, colsA);
break;

case 3:
printf("Enter scalar value: ");
scanf("%d", &scalar);
matrixScalarMultiply(matrixA, scalar, result, rowsA, colsA);
printf("\nResult of scalar multiplication:\n");
printMatrix(result, rowsA, colsA);
break;

case 4:
if (colsA != rowsB) {
printf("Invalid matrix dimensions for multiplication.\n");
} else {
matrixMultiply(matrixA, matrixB, result, rowsA, colsA, colsB);
printf("\nResult of matrix multiplication:\n");
printMatrix(result, rowsA, colsB);
}
break;

default:
printf("Invalid choice.\n");
break;
}

The main function orchestrates the entire program's functionality, interacting with the user, calling
relevant functions, and displaying results.

12
Sample Execution:
To illustrate the functionality of the Matrix Calculator in C, let's consider a sample execution. In this
example, we will perform matrix multiplication with the following matrices:

Matrix A: Matrix B:
12 56
34 78

Input:

Enter the number of rows and columns for matrix A: 2 2


1 entries for row 1: 1 2
1 entries for row 2: 3 4

Enter the number of rows and columns for matrix B: 2 2


2 entries for row 1: 5 6
2 entries for row 2: 7 8

Operation Menu:
Operation Menu:
1. Add
2. Subtract
3. Scalar Multiply
4. Multiply two matrices
Enter your choice: 4

Output:
Matrix A: Matrix B:
1 2 5 6
3 4 7 8

Result of matrix multiplication:


19 22
43 50

This sample execution demonstrates the Matrix Calculator's capability to perform matrix multiplication
with matrices of user-defined sizes.
13
CHAPTER 5: Future Enhancements
While the current version of the Matrix Calculator in C addresses significant limitations in existing matrix
multiplication systems, there is room for future enhancements and improvements. Some potential areas
for future development include:

Error Handling:
Implement more robust error handling mechanisms to guide users in case of invalid inputs or operations.

Matrix Inversion:
Add functionality for matrix inversion, providing users with a broader set of matrix operations.

Graphical User Interface (GUI):


Consider developing a graphical user interface to enhance the overall user experience, especially for users
less familiar with command-line interfaces.

Optimizations:
Explore opportunities to optimize matrix multiplication algorithms for larger matrices, improving overall
performance.

Additional Matrix Operations:


Extend the range of supported matrix operations, such as determinant calculation, eigenvalue
computation, and singular value decomposition.

By focusing on these future enhancements, the Matrix Calculator in C can evolve into a more powerful
and versatile tool, catering to a broader audience with diverse matrix computation needs.

14
CHAPTER 6: Conclusion
The Matrix Calculator in C project successfully addresses the limitations of existing matrix multiplication
systems. By allowing users to input matrices of varying sizes and providing a user-friendly interface, the
calculator enhances the efficiency and usability of matrix operations. The inclusion of simultaneous result
verification and the display of entered elements for larger matrices contribute to the project's transparency
and educational value.

In conclusion, the Matrix Calculator in C not only simplifies complex matrix computations but also serves
as a valuable educational tool for understanding the principles of matrix-based problem-solving. The
project's user-centric design, flexibility, and comprehensive features make it a valuable asset for students,
researchers, and professionals in various scientific and engineering fields.

15
CHAPTER 7: REFERENCES
https://www.programiz.com/c-programming/examples/matrix-multiplication
https://www.programmingsimplified.com/c-program-multiply-matrices
https://www.javatpoint.com/matrix-calculator-in-c

16

You might also like