Matrix : Inverse always null - c

The inverse of my matrix shows always 0 and I don't understand why. Thank you for advance.
4 fonctions :
-main : user enters the matrix, and the results (of matrix, determinant, LU factorisation and inverse) are display.
#include<stdio.h>
#include<conio.h>
#include<math.h>
float determinant(float[20][20],float);
void cofactor(float[20][20],float);
void transpose(float[20][20],float[20][20],float);
void main()
{
float A[20][20]= {0},L[20][20]= {0}, U[20][20];
float B[20]= {0}, X[20]= {0},Y[20]= {0};
int i,j,k,n;
printf("Enter the order of square matrix: ");
scanf("%d",&n);
printf("\nEntrer les elements de la matrice A:\n");
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
printf("Entrer l'element A[%d][%d] : ", i,j);
scanf("%f",&A[i][j]);
}
}
printf("\nEntrer les termes de la matrice B\n");
for(i=0; i<n; i++)
{
printf("B[%d]",i);
scanf("%f",&B[i]);
}
for(j=0; j<n; j++)
{
for(i=0; i<n; i++)
{
if(i<=j)
{
U[i][j]=A[i][j];
for(k=0; k<i-1; k++)
U[i][j]-=L[i][k]*U[k][j];
if(i==j)
L[i][j]=1;
else
L[i][j]=0;
}
else
{
L[i][j]=A[i][j];
for(k=0; k<=j-1; k++)
L[i][j]-=L[i][k]*U[k][j];
L[i][j]/=U[j][j];
U[i][j]=0;
}
}
}
printf("[L]: \n");
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
printf("%9.3f",L[i][j]);
printf("\n");
}
printf("\n\n[U]: \n");
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
printf("%9.3f",U[i][j]);
printf("\n");
}
//Y pour calculer X
for(i=0; i<n; i++)
{
Y[i]=B[i];
for(j=0; j<i; j++)
{
Y[i]-=L[i][j]*Y[j];
}
}
for(i=0; i<n; i++)
{
printf("%9.3f",Y[i]);
}
for(i=n-1; i>=0; i--)
{
X[i]= Y[i];
for(j=i+1; j<n; j++)
{
X[i]-=U[i][j]*X[j];
}
X[i]/=U[i][i];
}
printf("\n\n[X]: \n");
for(i=0; i<n; i++)
{
printf("%9.3f",X[i]);
}
printf("\n\nLe determinant de la matrice A est = %f",n);
if (n==0)
printf("\nCette matrice n'a pas d'inverse!\n");
else {
cofactor(A,k);
}
getch();
}
float determinant(float a[20][20],float k)
{
float s=1,det=0,b[20][20];
int i,j,m,n,c;
if (k==1)
{
return (a[0][0]);
}
else
{
det=0;
for (c=0;c<k;c++)
{
m=0;
n=0;
for (i=0;i<k;i++)
{
for (j=0;j<k;j++)
{
b[i][j]=0;
if (i != 0 && j != c)
{
b[m][n]=a[i][j];
if (n<(k-2))
n++;
else
{
n=0;
m++;
}
}
}
}
det=det + s * (a[0][c] * determinant(b,k-1));
s=-1 * s;
}
}
return (det);
}
void cofactor(float num[20][20],float f) //fonction which will calculate the cofactof of matrix
{
float b[20][20],fac[20][20];
int p,q,m,n,i,j;
for (q=0;q<f;q++)
{
for (p=0;p<f;p++)
{
m=0;
n=0;
for (i=0;i<f;i++)
{
for (j=0;j<f;j++)
{
if (i != q && j != p)
{
b[m][n]=num[i][j];
if (n<(f-2))
n++;
else
{
n=0;
m++;
}
}
}
}
fac[q][p]=pow(-1,q + p) * determinant(b,f-1);
}
}
transpose(num,fac,f);
}
/*Finding transpose of matrix*/
void transpose(float num[20][20],float fac[20][20],float r)
{
int i,j;
float b[20][20],inverse[20][20],n;
for (i=0;i<r;i++)
{
for (j=0;j<r;j++)
{
b[i][j]=fac[j][i];
}
}
n=determinant(num,r);
for (i=0;i<r;i++)
{
for (j=0;j<r;j++)
{
inverse[i][j]=b[i][j] / n;
}
}
printf("\n\n\nThe inverse of matrix is : \n");
for (i=0;i<r;i++)
{
for (j=0;j<r;j++)
{
printf("\t%f",inverse[i][j]); //show inverse of the matrix
}
printf("\n");
}
}

There are several issues with your code, but the principal error is that you pass a wrong parameter to
cofactor(A,k);
Here, k is a loop variable that has the value it had after it was used in the code that does the triangular decomposition. (You can see that this value is 1, because only one entry of the matrix is printed.)
You should use the order of the matrices, n:
cofactor(A, n);
In general, you should use a more consistent nomenclature. In main, the order of the matrices is n, in determinant it's k, in cofactor you use the floating-point variable f and in transpose, you use the floating-point variable r. To add to the confusion, n is a float here that represents the determinant. I suggest you use the integer n for the order, local integers i, j and k as loop variables – C99 allows you to define them only for the scope of the loop, e.g. for (int i = 0; ...) – and more or less meaningful names for the floating-point numbers; det for the determinant is good.
Further suggestions:
Don't write explicit loops to print matrices every time; write a function to print a matrix and a function to print a vector instead and call them.
Separate calculating from printing. For example, the code to print the inverse should not be part of (the badly named?) transpose. Instead, transpose should fill a matrix passed in from main, and main should then print that matrix.
Ideally, triangular decomposition and solving the equation should be functions, too. Experience the joys of nicely organised code. :)

Related

Varying Results When Displaying Result Matrix

I am currently working on a code that calculates the following equation:
W = (XT * X)^-1 * XT * Y
with XT being the transpose of X and ^-1 the inverse of the product XT*X.
To do this, I am passing a text file with a matrix to the program, and then performing operations on it to obtain the proper matrix formats to use in the equation. Although the code works some times, there are instances when I will run the code and receive the proper answer, but others when a random number will be produced.
For example, when using the matrix [first integer = columns-1, second integer = rows]:
4
4
4.000000,3.000000,2.000000,3.000000,200.000000
5.000000,2.000000,1.000000,7.000000,300.000000
1.000000,4.000000,2.000000,1.000000,500.000000
8.000000,8.000000,9.000000,3.000000,200.000000
it will produce an answer for XT * (XT * X)^-1 as:
0.497617 -0.166646 0.061712 -0.137570
-61.086998 -0.258283 -0.340935 -0.064228
0.186411 -0.083895 0.285920 -0.082587
-0.722773 0.207515 -0.009408 0.238550
-0.579552 0.345476 0.154362 0.055048
and the numbers will not remain the same through each test run. It does this with this result matrix multiplied by Y [the last column in the original matrix] as well. Below is a sample of the code I have written thus far:
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char* argv[]){
if(argc < 2){
printf("error.");
return 0;
}
FILE *fptrain = fopen(argv[1], "r");
int row, col, i, j;
fscanf(fptrain, "%d", &col);
col = col+1;
fscanf(fptrain, "%d", &row);
char ch;
//creates the original X and Y matrix
float trainX[row][col];
float trainY[row][1];
for(i=0; i<row; i++)
{
trainX[i][0] = 1.000000;
for(j=1; j<col; j++)
{
fscanf(fptrain, "%f%c", &trainX[i][j], &ch);
}
fscanf(fptrain, "%f%c", &trainY[i][0], &ch);
}
//creates the X transposed matrix
float trainXtrans[col][row];
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
trainXtrans[j][i] = trainX[i][j];
}
}
//multiplies X and X transposed
float trainXtemp[row][row];
int s;
int num=0;
for(i=0; i<row; i++)
{
for(j=0; j<row; j++)
{
for(s=0; s<col; s++)
{
num = num + trainX[i][s]*trainXtrans[s][j];
}
trainXtemp[i][j] = num;
num =0;
}
}
//finds the identity matrix of X times X transposed
float trainXinden[row][row*2];
for(i=0; i<row; i++)
{
for(j=0; j<row; j++)
{
trainXinden[i][j] = trainXtemp[i][j];
}
for(j=row; j<row*2; j++)
{
if(j==i+row)
{
trainXinden[i][j] = 1;
}
else{
trainXinden[i][j] = 0;
}
}
}
//finds the inverse of X times X transposed through Gauss Jordan Elimination
int k;
float divscalar;
for(i=0; i<row; i++)
{
divscalar = trainXinden[i][i];
for(j=0; j<row*2; j++)
{
trainXinden[i][j] = trainXinden[i][j]/divscalar;
}
for(k=0; k<row; k++)
{
if(i!=k)
{
float subscalar = trainXinden[k][i];
for(j=0; j<row*2; j++)
{
trainXinden[k][j] = trainXinden[k][j] - subscalar*trainXinden[i][j];
}
}
}
}
//copies over the result of gauss jordan elimination
float trainXinverse[row][row];
for(i=0; i<row; i++)
{
for(j=0; j<row; j++)
{
trainXinverse[i][j] = trainXinden[i][j+row];
}
}
//multiplies (X times X transpose) inverse by (X transposed)
float trainXinvXt[col][row];
for(i=0; i<col; i++)
{
for(j=0; j<row; j++)
{
for(s=0; s<row; s++)
{
trainXinvXt[i][j] += trainXtrans[i][s]*trainXinverse[s][j];
}
}
}
//multiples (trainXinvXt) by Y
float weight[row][1];
for(i=0; i<col; i++)
{
for(s=0; s<col-1; s++)
{
weight[i][0] += trainXinvXt[i][s]*trainY[s][0];
}
}
return 0;
}
is this perhaps a memory issue or is my Gauss-Jordan Elimination method throwing something off?

Dynamic Memory Allocation for Matrix Multiplication

Currently I am working on a code that computes the following equation with two matrices, X and Y, to return the value of matrix W.
W = (XT * X)^-1 * XT * Y
Input Matrix train:
4
10
3.000000,1.000000,1180.000000,1955.000000,221900.000000
3.000000,2.250000,2570.000000,1951.000000,538000.000000
2.000000,1.000000,770.000000,1933.000000,180000.000000
4.000000,3.000000,1960.000000,1965.000000,604000.000000
3.000000,2.000000,1680.000000,1987.000000,510000.000000
4.000000,4.500000,5420.000000,2001.000000,1230000.000000
3.000000,2.250000,1715.000000,1995.000000,257500.000000
3.000000,1.500000,1060.000000,1963.000000,291850.000000
3.000000,1.000000,1780.000000,1960.000000,229500.000000
3.000000,2.500000,1890.000000,2003.000000,323000.000000
Input Matrix test:
3
3.000000,2.500000,3560.000000,1965.000000
2.000000,1.000000,1160.000000,1942.000000
3.000000,1.000000,1430.000000,1927.000000
Result Matrix:
716559
194430
323391
My code returns the proper values for the testcases with the exception of matrices over the size of 1000. I know this is because the size is not dynamically allocated, but I am not sure what the best approach to doing this in my code would be:
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char* argv[]){
if(argc < 3){
printf("error.");
return 0;
}
FILE *fptrain = fopen(argv[1], "r");
if(fptrain == NULL)
{
printf("error.");
return 0;
}
int row, col, i, j;
fscanf(fptrain, "%d", &col);
col = col+1;
fscanf(fptrain, "%d", &row);
char ch;
//creates the original X and Y matrix
double trainX[row][col];
double trainY[row][1];
for(i=0; i<row; i++)
{
trainX[i][0] = 1.000000;
for(j=1; j<col; j++)
{
fscanf(fptrain, "%lf%c", &trainX[i][j], &ch);
}
fscanf(fptrain, "%lf%c", &trainY[i][0], &ch);
}
//creates the X transposed matrix
double trainXtrans[col][row];
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
trainXtrans[j][i] = trainX[i][j];
}
}
//multiplies X and X transposed
double trainXtemp[row][row];
int s;
double num=0;
for(i=0; i<col; i++)
{
for(j=0; j<col; j++)
{
for(s=0; s<row; s++)
{
num = num + trainX[s][j]*trainXtrans[i][s];
}
trainXtemp[i][j] = num;
num = 0;
}
}
//finds the identity matrix of X times X transposed
double trainXinden[col][col*2];
for(i=0; i<col; i++)
{
for(j=0; j<col; j++)
{
trainXinden[i][j] = trainXtemp[i][j];
}
for(j=col; j<col*2; j++)
{
if(j==i+col)
{
trainXinden[i][j] = 1.000000;
}
else{
trainXinden[i][j] = 0.000000;
}
}
}
//finds the inverse of X times X transposed through Gauss Jordan Elimination
int k;
double divscalar;
for(i=0; i<col; i++)
{
divscalar = trainXinden[i][i];
for(j=0; j<col*2; j++)
{
if(trainXinden[i][j] != 0)
{
trainXinden[i][j] = trainXinden[i][j]/divscalar;
}
}
for(k=0; k<col; k++)
{
if(i!=k)
{
double subscalar = trainXinden[k][i];
for(j=0; j<col*2; j++)
{
trainXinden[k][j] = trainXinden[k][j] - subscalar*trainXinden[i][j];
}
}
}
}
double trainXinverse[row][row];
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
trainXinverse[i][j] = trainXinden[i][j+col];
}
}
double trainXinvXt[col][row];
for(i=0; i<col; i++)
{
for(j=0; j<row; j++)
{
for(s=0; s<col; s++)
{
num = num + trainXinverse[i][s]*trainXtrans[s][j];
}
trainXinvXt[i][j] = num;
num = 0;
}
}
//multiples (trainXinvXt) by Y
double weight[row][1];
for(i=0; i<col; i++)
{
for(s=0; s<row; s++)
{
weight[i][0] += trainXinvXt[i][s]*trainY[s][0];
}
}
FILE *fptest = fopen(argv[2], "r");
if(fptest == NULL)
{
printf("error.");
return 0;
}
int testrows;
fscanf(fptest, "%d", &testrows);
//creates the test file matrix
double testM[testrows][col];
for(i=0; i<testrows; i++)
{
testM[i][0] = 1.000000;
for(j=1; j<col; j++)
{
fscanf(fptest, "%lf%c", &testM[i][j], &ch);
}
}
double prices[testrows][1];
for(i=0; i<testrows; i++)
{
for(s=0; s<col; s++)
{
num = num + testM[i][s]*weight[s][0];
}
prices[i][0] = num;
num = 0;
}
for(i=0; i<testrows; i++)
{
printf("%0.0lf", prices[i][0]);
printf("\n");
}
return 0;
}
When I use malloc on each matrix, for some reason it appears that it is not allowing me to create the augmented matrix or perform my gauss-jordan reduction, which is ruining my final answer.
The best approach to allocate memory to a multi-dimensional array (taking e.g. of 2D array, since you are using matrix in your program):
int(*matrix)[col] = malloc (sizeof(int[row][col]));
If you want to write a function for it then:
void* allocMatrix (int row, int col)
{
return malloc (sizeof(int[row][col]));
}
If you are not familiar with this approach of allocating memory dynamically to a multi-dimensional array then read this (very nicely explained by Lundin).
In your program, you are having the matrix of type double, so the allocMatrix() will be -
void* allocMatrix (int row, int col)
{
return malloc (sizeof(double[row][col]));
}
and in main(), you need to make following changes to create matrix dynamically -
double (*trainX)[col] = allocMatrix (row, col);
Do same changes for other matrices also and make sure to free() the dynamically allocated memory to matrices at appropriate places in your program.
Just for the knowledge purpose, this practice of allocating memory to the multi-dimensional array has been followed commonly, though it's not the best.

Adding Matrices with while loops

I need to add two 3x3 matrices together using while loops. I am able to read and print both matrices using while loops but cannot work out how to add the matrices with while loops.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=0,j=0,k=0,l=0,m=0,n=0;
int a[3][3],b[3][3],c[3][3];
printf("Enter the first matrix \n \n");
while(i<3)
{
j=0;
while(j<3)
{
scanf("%d",&a[i][j]);
j++;
}
i++;
}
printf("\n");
printf("The first matrix is \n\n");
i=0;
while(i<3)
{
j=0;
while(j<3)
{
printf("%d ",a[i][j]);
j++;
}
printf("\n");
i++;
}
printf("\n");
//////////////////////////////////////////////////////////////////////////
printf("Enter the second matrix \n \n");
while(k<3)
{
l=0;
while(l<3)
{
scanf("%d",&b[k][l]);
l++;
}
k++;
}
printf("\n");
printf("The second matrix is \n\n");
k=0;
while(k<3)
{
l=0;
while(l<3)
{
printf("%d ",b[k][l]);
l++;
}
printf("\n");
k++;
}
printf("\n");
///////////////////////////////////////////////////////////////////////////////
printf("The sum of the matrix's is \n \n");
return 0;
}
int matrix_a [3][3] = {{1,2,3},{3,4,5},{4,5,6}};
int matrix_b [3][3] = {{5,6,7},{6,7,8},{7,8,9}};
int i = 0;
while (i < 3) {
j = 0;
while (j < 3) {
matrix_a[i][j] += matrix[i][j];
++ j;
}
++ i;
}
for loops are a much better choice:
for (int i = 0; i < 3; ++ i) {
for (int j = 0; j < 3; ++ j) {
matrix_a[i][j] += matrix_b[i][j];
}
}
Superoptimal pointer-arithmetic alternative:
int* ptr_a = matrix_a;
int* ptr_b = matrix_b;
int size = 3 * 3;
while (size --) {
* ptr_a++ += * ptr_b++;
}
Break your problem down into smaller sub problems to help solve it.
EXAMPLE:
How to loop through a 2-D array?
How to get values and assign them to a variable from looping through the array?
How to manipulate the values to are obtaining (In your case adding them to something else)
How to insert these values into the correct position in a new array.
You can add the below code to do the addition:
k=0;
while(k<3)
{
l=0;
while(l<3)
{
c[k][l] = a[k][l]+b[k][l];
l++;
}
k++;
}
And, to display your matrix, you can use for loop instead of while.
Example:
for(int p =0;p<3;p++){
for(int q=0;q<3;q++){
c[p][q] = a[p][q] + b[p][q] ;
}
}
for(int p =0;p<3;p++){
for(int q=0;q<3;q++){
printf("%d \t",c[p][q]) ;
}
printf("\n");
}

What changes should I make to this code so it solves the system of linear equations?

#include<stdio.h>
int main()
{
int i,j,k,n;
float A[20][20],c,x[10];
scanf("%d",&n);//size of matrix
for(i=1; i<=n; i++)
{
for(j=1; j<=(n+1); j++)
{
printf(" A[%d][%d]:", i,j);
scanf("%f",&A[i][j]);
}
}
//looking for elements in the diagonal matrix
for(j=1; j<=n; j++)
{
for(i=1; i<=n; i++)
{
if(i!=j)
{
c=A[i][j]/A[j][j];
for(k=1; k<=n+1; k++)
{
A[i][k]=A[i][k]-c*A[j][k];
}
}
}
}
for(i=1; i<=n; i++)
{
x[i]=A[i][n+1]/A[i][i];
printf("\n x%d=%f\n",i,x[i]);//solution
}
return(0);
}
it prints things like "#IO". The algorithm is supposed to have three equations with n variables as inputs. I think the problem is when I divide by zero, but I don't know what can I add in order to avoid that.

How to perform different operations on matrix using dynamic memory allocation?

There is segmentation fault in case 5 of this program(transpose) this segmentation fault occurs only when the input row is greater than that of the input column. Hopefully this is due to the reason I have not allocated the memory accordingly.
b= (int**)malloc(r*sizeof(int*));
for(i=0; i<c; i++)
{
b[i] = (int*)malloc(sizeof(int));
}
And if I am doing it accordingly like this:
b= (int**)malloc(c*sizeof(int*));
for(i=0; i<r; i++)
{
b[i] = (int*)malloc(sizeof(int));
}
there still is seg. fault and this time it is not producing correct output for matrix of any order.
Here is my full code :
#include<stdio.h>
#include<stdlib.h>
int** inputmatrix(int **a,int r, int c)
{
int i, j;
a = (int**)malloc(r*sizeof(int));
for(i=0; i<c; i++)
{
a[i] = (int*)malloc(sizeof(int));
}
printf("\n Input the Elements of the Matrix :");
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
scanf("%d",&a[i][j]);
}
}
return a;
}
void showmatrix(int** a, int r, int c)
{
int i, j;
for(i=0; i<r; i++)
{
printf("\n");
for(j=0; j<c; j++)
{
printf(" %d",a[i][j]);
}
}
}
int** add(int **a, int **b, int r, int c)
{
int i,j;
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
a[i][j] = a[i][j]+b[i][j];
}
}
return a;
}
int** multiplication(int** a, int **b, int r1, int c1, int c2)
{
int **c,i,j,k;
c = (int**)malloc(r1*sizeof(int*));
for(i=0; i<c2; i++)
{
c[i] = (int*)malloc(sizeof(int));
}
for(i=0; i<r1; i++)
{
for(j=0; j<c2; j++)
{
c[i][j] = 0;
for(k=0; k<c1; k++)
{
c[i][j] = c[i][j] + a[i][k]*b[k][j];
}
}
}
return c;
}
int minval(int **a, int r, int c)
{
int i, min;
min = a[r][0];
for(i=0; i<c; ++i)
{
if(a[r][i]<min)
{
min = a[r][i];
}
}
return min;
}
int maxval(int **a, int r, int c)
{
int i, max;
max = a[0][c];
for(i=0; i<r; ++i)
{
if(a[i][c] > max )
{
max = a[i][c];
}
}
return max;
}
void saddlepoint(int **a, int r, int c)
{
int i, j, rpos, cpos, flag = 0,sp;
for(i=0; i<r; ++i)
{
for(j=0; j<c; ++j)
{
if(a[i][j] == minval(a, i, c) && a[i][j] == maxval(a, r, j))
{
sp = a[i][j];
flag = 1;
rpos = i;
cpos = j;
}
}
}
if(flag == 1)
{
printf("\n The Saddle point of the Matrix is found at position (%d,%d) value is %d ", rpos, cpos,sp);
}
else
{
printf("\n There is no saddle point in the Matrix ");
}
}
int magicsquare(int **a, int r, int c)
{
int i, j, row_sum, col_sum, d1, d2, flag = 0;
if(r==c)
{
for(i =0 ;i<r; i++) // for digonals
{
d1 = d1 + a[i][i];
d2 = d2 + a[i][r-i-1];
}
for(i=0; i<r; i++)
{
row_sum = 0;
for(j=0; j<c; j++)
{
row_sum = row_sum + a[i][j];
}
if(row_sum == d1)
flag = 1;
else
break;
}
for(i=0; i<r; i++)
{
col_sum = 0;
for(j=0; j<c; j++)
{
col_sum = col_sum + a[j][i];
}
if(col_sum == d1)
flag =1;
else
break;
}
}
else
{
printf("\n This Matrix is not a Magic Square ");
}
return flag;
}
int** transpose(int **a, int r, int c)
{
int i, j, **b;
b= (int**)malloc(c*sizeof(int*));
for(i=0; i<r; i++)
{
b[i] = (int*)malloc(sizeof(int));
}
for(i =0; i<r; i++)
{
for(j=0; j<c; j++)
{
b[j][i] = a[i][j];
}
}
return b;
}
int main()
{
int **a, **b,r1,c1,r2,c2, i,j,ch,f;
int **c;
printf("\n enter your choice : \n1.Addition \n2.Multiplication \n3.Saddle Point \n4. Magic Square \n5.Transpose\n");
scanf("%d",&ch);
printf("\n enter the oder of matrix A :");
scanf("%d%d",&r1,&c1);
a = inputmatrix(a,r1,c1);
switch(ch)
{
case 1:
printf("\n enter the oder of matrix B :");
scanf("%d%d",&r2,&c2);
if(r1==r2 && c1==c2)
{
b = inputmatrix(b,r2,c2);
a = add(a,b,r1,c1);
printf("\n the result of the addition of matrices is :");
for(i=0; i<r1; i++)
{
printf("\n");
for(j=0;j<c1; j++)
{
printf("%d\t",a[i][j]);
}
}
}
else
{
printf("\n these matrices can't be added ");
}
break;
case 2 :
printf("\n Enter the Order of Matrix B :");
scanf("%d%d",&r2,&c2);
b = inputmatrix(b,r2,c2);
if(c1 == r2)
{
c = multiplication(a, b, r1, c1, r2);
for(i=0; i<r1; i++)
{
printf("\n");
for(j=0; j<c2; j++)
{
printf("%d\t",c[i][j]);
}
}
}
else
{
printf("\n Sorry, These Matrices Can't be Multiplied ");
}
break;
case 3 :
printf("\n The Matrix you Entered is :");
for(i=0; i<r1; i++)
{
printf("\n");
for(j=0; j<c1; j++)
{
printf(" %d",a[i][j]);
}
}
saddlepoint(a,r1,c1);
break;
case 4 :
printf("\n The Matrix you Entered is :");
for(i=0; i<r1; i++)
{
printf("\n");
for(j=0; j<c1; j++)
{
printf(" %d",a[i][j]);
}
}
int f = magicsquare(a, r1, c1);
if(f==1)
printf("\n This Matrix is a Magic Square ");
else
printf("\n This Matrix is not a Magic Square ");
break;
case 5 :
printf("\n The Matrix you enter is :");
showmatrix(a,r1,c1);
b = transpose(a,r1,c1);
printf("\n the transpose of the entered matrix is :");
for(i=0; i<c1; i++)
{
printf("\n");
for(j=0; j<r1; j++)
{
printf(" %d",b[i][j]);
}
}
break;
default : printf("\n Sorry, This is a Wrong Choice ");
}
return 0;
}
A few Output cases are also below:
case 1:
enter your choice :
1.Addition
2.Multiplication
3.Saddle Point
4. Magic Square
5.Transpose
5
enter the oder of matrix A :3
2
Input the Elements of the Matrix :1
2
3
4
5
Segmentation fault (core dumped)
case 2:
enter your choice:
1.Addition
2.Multiplication
3.Saddle Point
4. Magic Square
5.Transpose
5
enter the oder of matrix A :2
3
Input the Elements of the Matrix :1
2
3
4
5
6
The Matrix you enter is :
1 2 3
4 5 6
the transpose of the entered matrix is :
1 4
2 5
3 6
And there is some problem in the multiplication function also, there also right matrix is not being displayed.
b= (int**)malloc(r*sizeof(int*));
This allocates an array of r int* elements.
for(i=0; i<c; i++)
This goes over the first c elements of this array. If c>r, then
b[i] = (int*)malloc(sizeof(int));
the behaviour of the line above is undefined. If c<=r, it is well-defined but not very useful, as it allocates one element for each matrix row. It probably becomes broken later, when you try to access elements past the first column.
To allocate a matrix with r rows and c columns you probably want to do this:
b = malloc(r * sizeof(int*)); /* allocate `r` rows */
for(i = 0; i < r; i++) /* for each of the `r` rows */
b[i] = malloc (c * sizeof(int)); /* allocate `c` columns */
There is mistake in the declaration of the arrays using malloc.
When you wish to declare a 2-D array of 'r' rows and 'c' columns, the statements should be something like
int **arr=malloc(r*sizeof(int *));
for(i=0;i<r;i++)
arr[i]=malloc(c*sizeof(int));
This will eliminate the segmentation fault.
The first statement declares an array of 'r' integer pointers.
The for loop executes 'r' times since you have to initialize each of the 'r' pointers declared by the previous statement.
The statement inside the for loop declares an integer array of length 'c' to store 'c' elements of each row.
Moreover, it initializes each of the pointers of the array 'arr' with the addresses of these rows(in correct order of-course).
Thus we get 'r' rows of 'c' elements each and hence an (r x c) matrix.

Resources