# In this program, the saddle () function works on the matrix differently if it does not produce correct output. What for?

I wrote this program to do various matrix operations using dynamic allocation, saddle point search function does not generate correct output. Why?

``````#include<stdio.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;
}

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, j) && a[i][j] == maxval(a, i, 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 main()
{
int **a, **b,r1,c1,r2,c2, i,j,ch;
int **c;
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);
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;
break;
default : printf("\n Sorry, This is a Wrong Choice ");
}
return 0;
}
```

```

please let me know any boolean error in my function `saddlepoint()`

.

``````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, j) && a[i][j] == maxval(a, i, 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 ");
}
}
```

```

Here in the saddlepoint () problem, I check that if any element of a two-dimensional matrix is ββminimum in a row and simultaneously maximum in a column, then it will be a saddle point. To do this, I call the minval () and maxval () functions in the if condition of the saddle point () fuction.

Output example: - enter your choice: 1.Addition 2.Multiplication 3.Connected point 4. Magic square 3

enter another value of matrix A: 3 3

Entering matrix elements: 1 2 3 4 5 6 7 8 9

Matrix saddle point found at position (0,0), value is 1 amit @ amit-HP-15-Notebook-PC: ~ \$

+3

source to share

``````#include <stdio.h>
#include <stdlib.h> // error #1: was missing this; required for malloc()

int** inputmatrix(int** a, int r, int c ) {

int i,j;

a = (int**)malloc(r*sizeof(int*)); // error #2: didn't have *
for (i = 0; i < r; ++i) a[i] = (int*)malloc(c*sizeof(int)); // error #3: had i < c, error #4: didn't have c*
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;

} // end inputmatrix()

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 < r1; ++i) c[i] = (int*)malloc(c2*sizeof(int)); // error #5: had i < c2, error #6: didn't have c2*
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];
} // end for
} // end for
return c;

} // end multiplication()

int min_r(int** a, int r, int c ) { // suggestion: clarify the purpose of this function by renaming and adding comment: // min for row r across all columns 0:c-1

int i,min;

min = a[r][0];
for (i = 0; i < c; ++i)
if (a[r][i] < min)
min = a[r][i];
return min;

} // end min_r()

int max_c(int** a, int r, int c ) { // suggestion: clarify the purpose of this function by renaming and adding comment: // max for column c across all rows 0:r-1

int i,max;

max = a[0][c];
for (i = 0; i < r; ++i)
if (a[i][c] > max)
max = a[i][c];
return max;

} // end max_c()

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] == min_r(a,i,c) && a[i][j] == max_c(a,r,j)) { // error #7: was passing j instead of c into min_r(), error #8: was passing i instead of r into max_c()
sp = a[i][j];
flag = 1;
rpos = i;
cpos = j;
} // end if
} // end for
} // end for
if (flag == 1) // error #9: had 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 main(void) {

int **a,**b,r1,c1,r2,c2,i,j,ch;
int **c;

scanf("%d",&ch);
printf("\n enter the order of matrix A : ");
scanf("%d%d",&r1,&c1);
a = inputmatrix(a,r1,c1);

switch (ch) {
case 1:
printf("\n enter the order of matrix B : ");
scanf("%d%d",&r2,&c2);
if (r1 == r2 && c1 == c2) {
b = inputmatrix(b,r2,c2);
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]);
} // end for
} else {
printf("\n these matrices can't be added ");
} // end if
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]);
} // end for
} else {
printf("\n these matrices can't be multiplied ");
} // end if
break;
case 3:
break;
default:
printf("\n this is a wrong choice ");
} // end switch

return 0;

} // end main()
```

```

Demo:

``````gcc a.c -o a;
./a;
##
## 2. multiplication
## 4. magic square
## 1
##
##  enter the order of matrix A : 3 2
##
##  input the elements of the matrix : 1 2 3 4 5 6
##
##  enter the order of matrix B : 3 2
##
##  input the elements of the matrix : 7 8 9 10 11 12
##
##  the result of the addition of matrices is :
## 8 10
## 12  14
## 16  18
./a;
##
## 2. multiplication
## 4. magic square
## 1
##
##  enter the order of matrix A : 1 1
##
##  input the elements of the matrix : 3
##
##  enter the order of matrix B : 2 1
##
##  these matrices can't be added
./a;
##
## 2. multiplication
## 4. magic square
## 2
##
##  enter the order of matrix A : 3 2
##
##  input the elements of the matrix : 1 2 3 4 5 6
##
##  enter the order of matrix B : 2 1
##
##  input the elements of the matrix : 10 20
##
## 50
## 110
## 170
./a;
##
## 2. multiplication
## 4. magic square
## 2
##
##  enter the order of matrix A : 3 2
##
##  input the elements of the matrix : 1 2 3 4 5 6
##
##  enter the order of matrix B : 1 2
##
##  input the elements of the matrix : 1 2
##
##  these matrices can't be multiplied
./a;
##
## 2. multiplication
## 4. magic square
## 3
##
##  enter the order of matrix A : 3 2
##
##  input the elements of the matrix : 1 2 3 4 5 6
##
##  the saddle point of the matrix is found at position (2,0) value is 5
./a;
##
## 2. multiplication
## 4. magic square
## 3
##
##  enter the order of matrix A : 3 2
##
##  input the elements of the matrix : 6 5 4 3 2 1
##
##  the saddle point of the matrix is found at position (0,1) value is 5
./a;
##
## 2. multiplication
## 4. magic square
## 3
##
##  enter the order of matrix A : 3 2
##
##  input the elements of the matrix : 1 2 2 1 1 2
##
##  there is no saddle point in the matrix
```

```
+2

source

All Articles