Computer Science‎ > ‎

C Program: Demonstrating Operations on Matrices - Addition, Subtraction, Multiplication, Inversion, Finding Determinants




 
The C++ Programming Language...
List Price: Rs.775
Our Price: Rs.736
Buy from FlipKart

The C++ Programming Language (Bjarne Stroustrup)

Beginning C++ Through Game Pr...
Our Price: Rs.2008
Buy from FlipKart

       
Beginning C++ through Game Programming 

 
C++ How to Program
List Price: Rs.695
Our Price: Rs.632
Buy from FlipKart

                               
 
The C Programming Language
List Price: Rs.175
Our Price: Rs.137
Buy from FlipKart

             
The C Programming Language (Kernighan)
 
C How To Program 6th  Edition
List Price: Rs.650
Our Price: Rs.624
            Buy from FlipKart


          
C How to Program (Deitel and Deitel)

 




C Program which performs matrix addition ,subtraction, multiplication ,finds the determinant and inverse of Matrices

/*This program performs matrix addition ,subtraction, multiplication ,finds the determinant and inverse of the entered matrices.*/
#include<stdio.h>
float **matrixA;//for the first matrix
float **matrixB;//for the second matrix
float **matrixRes;//for the results of additon,multiplication and subtraction matrix
int rowA,columnA,rowB,columnB,row,column,navigate;//rowA,columnA,rowB,columnB store sizes of A and B row and column are counters
void add()
{//function to add two matrices
if((rowA==rowB)&&(columnA==columnB))
    {
    printf("A+B\n");//Unless the dimensions are same addition not possible
    matrixRes=(float **)malloc(rowA*sizeof(float));
    for(row=0;row<rowA;row++)
    matrixRes[row]=(float *)malloc(columnA*sizeof(float));
    for(row=0;row<rowB;row++)
    {
        for(column=0;column<columnB;column++)
        {
            matrixRes[row][column]=matrixA[row][column]+matrixB[row][column];//Add corresponding elements
            printf("%.2f ",matrixRes[row][column]);
        }
        printf("\n");
    }
    for(row=0;row<rowA;row++)
        free(matrixRes[row]);
    free(matrixRes);
    }
else
printf("Incompatible for addition\n");
}
void sub()
{
if((rowA==rowB)&&(columnA==columnB))
    {//unless dimension are smae subtraction not possible
    printf("A-B\n");
    matrixRes=(float **)malloc(rowA*sizeof(float));
    for(row=0;row<rowA;row++)
    matrixRes[row]=(float *)malloc(columnA*sizeof(float));//dynamic allocation
    for(row=0;row<rowB;row++)
    {
        for(column=0;column<columnB;column++)
        {
            matrixRes[row][column]=matrixA[row][column]-matrixB[row][column];//subtracting the corresponding elements
            printf("%.2f ",matrixRes[row][column]);
        }
        printf("\n");
    }
    for(row=0;row<rowA;row++)
        free(matrixRes[row]);
    free(matrixRes);
    }
else
printf("Incompatible for subtraction\n");
}
void mult()
{
if((columnA==rowB))
    {//for two matrices to allow for multiplication number of column of first matrix should be equal to number of rows in other matrix
    printf("A*B\n");
    matrixRes=(float **)malloc(rowA*sizeof(float));
    for(row=0;row<rowA;row++)
    matrixRes[row]=(float *)malloc(columnB*sizeof(float));
    for(row=0;row<rowB;row++)
    {
        for(column=0;column<columnB;column++)
        {//code to multiply two matrices
            matrixRes[row][column]=0;
            for(navigate=0;navigate<columnA;navigate++)
            matrixRes[row][column]+=matrixA[row][navigate]*matrixB[navigate][column];
            printf("%.2f ",matrixRes[row][column]);
        }
        printf("\n");
    }
    for(row=0;row<rowA;row++)
        free(matrixRes[row]);
    free(matrixRes);
    }
else
printf("Incompatible for multiplication\n");
}
float determinant(float **matrix,int size)
{
    //A recursive function to get the determinant
    float **temp;//the temporary matrix to store subsequent smaller matrix
    float det=0;//initalise value of deteminant zero

    if(size==1)
    det=matrix[0][0];//in case the matrix passed has size 1
else
    if(size==2)
    det=matrix[0][0]*matrix[1][1]-matrix[0][1]*matrix[1][0];//in case the matrix passed has size 2
else
    {

        int cctr,ictr,ctr;
    for(column=0;column<size;column++)
    {//now we create another matrix properly exculding the first row and the element's corresponding colummn and then find its resulting determinant
        temp=(float **)malloc((size-1)*sizeof(float));
        for(ctr=0;ctr<size-1;ctr++)
        temp[ctr]=(float *)malloc((size-1)*sizeof(float));
        for(ctr=1;ctr<size;ctr++)
        {
            cctr=0;
            for(ictr=0;ictr<size;ictr++)
            {
                if(ictr==column)
                    continue;
                temp[ctr-1][cctr]=matrix[ctr][ictr];
                cctr++;
            }
        }
            det=det+pow(-1,column)*matrix[0][column]*determinant(temp,size-1);//pass the new matrix
        for(ctr=0;ctr<size-1;ctr++)
        free(temp[ctr]);
        free(temp);
    }
}
    return det;//return claculated determinant
}
void matrixInverse(float **matrix,int size,float res)
{
float **tem;//matrix to store smaller matrix whose determinant we find out
int ctr,ictr,rowT,columnT;
float **matrixCft;
matrixCft=(float **)malloc(size*sizeof(float));//matrix to store cofactor matrix
tem=(float **)malloc((size-1)*sizeof(float));
for(ctr=0;ctr<size-1;ctr++)
tem[ctr]=(float *)malloc((size-1)*sizeof(float));
for(ctr=0;ctr<size;ctr++)
matrixCft[ctr]=(float *)malloc(size*sizeof(float));
for(row=0;row<size;row++)
{
for(column=0;column<size;column++)
{//we in these loops calculate the smaller matrix which is we pass to the determinant function which allows us to calculate the cofactors
    rowT=0;
    columnT=0;
    for(ctr=0;ctr<size;ctr++)
        for(ictr=0;ictr<size;ictr++)
            if(ctr!=row&&ictr!=column)
                {
     tem[rowT][columnT++]=matrix[ctr][ictr];
                    if(columnT==size-1)
                    {
                        rowT++;
                        columnT=0;
                    }
                }
    matrixCft[row][column]=pow(-1,(row+column))*determinant(tem,size-1);//multiplying the minor obtained with appropriate power of -1 to get the cofactor
}
}
for(column=0;column<size;column++)
for(row=0;row<size;row++)
matrixCft[column][row]=matrixCft[column][row]/res;//In this step i divide the cofactor matrix with the determinant of the matrix
float **Result;
Result=(float **)malloc(size*sizeof(float));
for(ctr=0;ctr<size;ctr++)
Result[ctr]=(float *)malloc(size*sizeof(float));
for(column=0;column<size;column++)
for(row=0;row<size;row++)
Result[column][row]=matrixCft[row][column];//calulating the transpose of the cofactor matrix
for(row=0;row<size;row++)
    {
        for(column=0;column<size;column++)
        printf("%.2f ",Result[row][column]);
        printf("\n");//display the inverse
    }
for(ctr=0;ctr<size-1;ctr++)
    free(tem[ctr]);
free(tem);
for(row=0;row<size;row++)
    {
        free(Result[row]);
        free(matrixCft[row]);
    }
free(Result);
free(matrixCft);
}
int main()
{
    printf("Enter the number of rows and column in A\n");
    scanf("%d%d",&rowA,&columnA);
    printf("Enter the number of rows and column in B\n");
    scanf("%d%d",&rowB,&columnB);//entering sizes
    matrixA=(float **)malloc(rowA*sizeof(float));
    matrixB=(float **)malloc(rowB*sizeof(float));
    for(row=0;row<rowA;row++)
    matrixA[row]=(float *)malloc(columnA*sizeof(float));
    for(row=0;row<rowA;row++)
    matrixB[row]=(float *)malloc(columnB*sizeof(float));
    printf("Enter the matrix A\n");
    for(row=0;row<rowA;row++)//entering the matrices
    for(column=0;column<columnA;column++)
    scanf("%f",&matrixA[row][column]);
    printf("Enter the matrix B\n");
    for(row=0;row<rowB;row++)
    for(column=0;column<columnB;column++)
    scanf("%f",&matrixB[row][column]);
    add();//performing additon,multiplication and subtraction
    sub();
    mult();
    if(rowA==columnA)
    {//It is only possible to find detrminant and inverse for square matrix so checking that condition
    float resA=determinant(matrixA,rowA);
    printf("Determinant of matrix A is %.2f\n",resA);
    if(resA!=0)
    {//If determinant is zero than inverse can not be calculated
    printf("The matrix inverse is\n");
    matrixInverse(matrixA,rowA,resA);
    }
    else
    printf("The inverse can not be calculated\n");
    }
    else
    {
        printf("Determinant of matrix A can not be calculated\n");
        printf("Inverse of matrix A can notbe calculated\n");
    }
    if(rowB==columnB)
    {
    float resB=determinant(matrixB,rowB);
    printf("Determinant of matrix B is %.2f\n",resB);
    if(resB!=0)
    {//Similarly operate on matrix B
    printf("The matrix inverse is\n");
    matrixInverse(matrixB,rowB,resB);
    }
    else
    printf("The inverse can not be calculated");
    }
    else
    {
        printf("Determinant of matrix B can not be calculated\n");
        printf("Inverse of matrix B can not be calculated\n");
    }
    for(row=0;row<rowA;row++)
        free(matrixA[row]);
    for(row=0;row<rowB;row++)
        free(matrixB[row]);
    free(matrixA);
    free(matrixB);
    getch();
}
/*A sample run of the program is found to give rseult as:-
Enter the number of rows and column in A
3
3
Enter the number of rows and column in B
3
3
Enter the matrix A
4 5 6
4 4 7
8 9 0
Enter the matrix B
1 2 3
5 6 7
3 2 1
A+B
5.00 7.00 9.00
9.00 10.00 14.00
11.00 11.00 1.00
A-B
3.00 3.00 3.00
-1.00 -2.00 0.00
5.00 7.00 -1.00
A*B
47.00 50.00 53.00
45.00 46.00 47.00
53.00 70.00 87.00
Determinant of matrix A is 52.00
The matrix inverse is
-1.21 1.04 0.21
1.08 -0.92 -0.08
0.08 0.08 -0.08
Determinant of matrix A is 0.00
Inverse of matrix B can not be calculated
*/





 









C Program to Reverse A String

C Program: Building an Expression Evaluator
C Program: Check for Armstrong Numbers
C Program: Check whether a string is a Palindrome or not
C Program: Common Operations on Sets - Union, Intersection, Difference, Cardinal Product
C Program: Computing exp(x), sin(x), cos(x), tan(x) using series expansions
C Program: Computing the Area of a Circle
C Program: Computing the Upper Triangular Matrix and Lower Triangular Matrix
C Program: Demonstrating File Handling Functions
C Program: Demonstrating Operations on Matrices - Addition, Subtraction, Multiplication, Inversion, Finding Determinants
C Program: Demonstrating the use of Bitwise Operators
C Program: Displaying a Histogram of word frequencies (unigram)
C Program: Distance Vector Routing Algorithm using Bellman Ford's Algorithm
C Program: Numerical Computing - The Gaussian Elimination Method
C Program: Numerical Computing - Implementing the Newton Raphson Method
C Program: Numerical Computing - the Bisection Method
C Program: Numerical Computing - The Gaussian Elimination Technique from Linear Algebra
C Program: Numerical Computing - the Jacobi Method
C Program: Printing the Pascal Triangle
C Program: Reversing the order of words in a sentence
C Program: Solving Simultaneous Equations in Two Variables
C Program: Source Code for computing the GCD(HFC) of two numbers
C Program: Source Code for Solving Quadratic Equations
C Program: Source code to solve the Josephus Problem
C Program: Sudoku Solver
C Program: The Usage of Command Line Arguments
C Program: Using the Sieve of Eratosthenes to print Prime Numbers