This code works only if the system has unique solutions. When there is no solution or infinitely many solutions, it should print "Has no unique solution." but the code below prints "nan" or "inf". How can I do that?
#include<stdio.h>
int main()
{
int i,j,k,n;
double A[20][20],c,x[10];
printf("\nEnter the size of matrix: ");
scanf("%d",&n);
printf("\nEnter the elements of augmented matrix row-wise:\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=(n+1); j++)
{
printf(" A[%d][%d]:", i,j);
scanf("%lf",&A[i][j]);
}
}
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];
}
}
}
}
printf("\nThe solution is:\n");
for(i=1; i<=n; i++)
{
x[i]=A[i][n+1]/A[i][i];
printf("\n x%d=%0.3f\n",i,x[i]);
}
return(0);
}
Perform a check for the whether the number is valid or not before printing.If not valid, print your desired message.You can modify the last for loop in your code as follows:
for(i=1; i<=n; i++)
{
x[i]=A[i][n+1]/A[i][i];
//Nan and inf check
if((A[i][i]!=A[i][i]) || (A[i][i] ==0))
break;
else
printf("\n x%d=%0.3f\n",i,x[i]);
}
printf("Has no unique solution");
return(0);
}
Handling Infinity and Nan
Related
I already finished creating the sum, difference, and product. What I am having trouble with is the quotient. because when I run it, It will just give me 0.0. I tried making the arrA and arrB a float which makes the quotient work but the others (sum, difference, and product) do not work. Please help me on how I could get the quotient to work.
#include <stdio.h>
int main()
{
int arrA[2][2];
int arrB[2][2];
float arrSum[2][2];
float arrDiff[2][2];
float arrProd[2][2];
float arrQuo[2][2];
int i,j;
//1st Matrix
printf("Enter Elements of 1st Matrix\n");
for (i=1; i<3; i++) // size of rows
{
for(j=1; j<3; j++) // size of columns
{
printf("Enter a[%d][%d]: ",i,j);
scanf("%d", &arrA[i][j]); // to get value and save to array[][]
}
//printf("\n");
}
//Second Matrix
printf("Enter Elements of 2nd Matrix\n");
for (i=1; i<3; i++) // size of rows
{
for(j=1; j<3; j++) // size of columns
{
printf("Enter b[%d][%d]: ",i,j);
scanf("%d", &arrB[i][j]); // to get value and save to array[][]
}
//printf("\n");
}
// Sum of Matrix
printf("Sum of Matrix: \n");
for (i=1; i<3; i++) // size of rows
{
for(j=1; j<3; j++) // size of columns
{
arrSum[i][j] = arrA[i][j] + arrB[i][j];
printf("%.1f ", arrSum[i][j]);
}
printf("\n");
}
//Difference of Matrix
printf("Difference of Matrix: \n");
for (i=1; i<3; i++) // size of rows
{
for(j=1; j<3; j++) // size of columns
{
arrDiff[i][j] = arrA[i][j] - arrB[i][j];
printf("%.1f ", arrDiff[i][j]);
}
printf("\n");
}
//Product of Matrix
printf("Product of Matrix: \n");
for (i=1; i<3; i++) // size of rows
{
for(j=1; j<3; j++) // size of columns
{
arrProd[i][j] = arrA[i][j] * arrB[i][j];
printf("%.1f ", arrProd[i][j]);
}
printf("\n");
}
//Quotient of Matrix
printf("Quotient of Matrix: \n");
for (i=1; i<3; i++) // size of rows
{
for(j=1; j<3; j++) // size of columns
{
arrQuo[i][j] = arrA[i][j] / arrB[i][j];
printf("%.2f ", arrQuo[i][j]);
}
printf("\n");
}
return 0;
}
here is the result:
The output
here is what it should look like:
The supposed output
write a c program which finds the locations and values of largest and second largest element in a two dimensional array.
#include<stdio.h>
int main()
{
int row,col,i,j,max1=0,max2=0,flr,flc,slr=0,slc=0;
printf("Enter the value of row: ");
scanf("%d",&row);
printf("Enter the value of column: ");
scanf("%d",&col);
int a[col][row];
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
printf("Value of %d and %d is:",i,j);
scanf("%d",&a[i][j]);
}
}
printf("The matrix is: \n");
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
if(a[i][j]>max1)
{
max2=max1;
max1=a[i][j];
flr=i;
flc=j;
}
if(a[i][j]>max2&&a[i][j]<max2)
{
max2=a[i][j];
slr=i;
slc=j;
}
}
}
printf("Largest number of this matrix is:%d ; Location is a[%d][%d]\n",max1,flr+1,flc+1);
printf("Second Largest number of this matrix is:%d ; Location is a[%d][%d]\n",max2,slr+1,slc+1);
}
I find out the largest value and its location. I also find out the second largest value .I try but I can't find out the location of second largest value. plz help me to find where I did wrong.
I wrote a program to find a cofactor matrix for an particular element of the given matrix. Here is my code:
#include<stdio.h>
int main()
{
int n;
printf("Enter the no of rows and columns:");
scanf("%d",&n);
int A[n][n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
printf("\nEnter the (%d,%d) element:",i+1,j+1);
scanf("%d",&A[i][j]);
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
printf(" %d",A[i][j]);
}
printf("\n");
}
int B[20][20],k=0,l=0,a=0,b=0;
for(int i=0;i<n;i++)
{
if(i!=a)
{
for(int j=0;j<n;j++)
{
if(j!=b)
{
B[k][l]=A[i][j];
l++;
}
}
k++;
}
}
n--;
for(int i=0;i<n;i++)
{for(int j=0;j<n;j++)
{
printf(" %d",B[i][j]);
}
printf("\n");
}
}
Now here I have declared the element as (0,0) and have tried to find its co-factor matrix but it is giving me junk values after the first output line. Please correct the programme.
You forgot to reinitialize l after the first row. Change
for(int j=0;j<n;j++)
to
for (int l=0, j=0; j<n; j++)
Assume the matrices to be of the form,
The required output on stdout using C language should be
My code:
#include<stdio.h>
int main(void)
{
int size, i=0, j=0;
printf("Enter the size of the array:");
scanf("%d",&size);
int A[size][size], B[size][size], C[size][size];
//Entering the values in A[][]
printf("enter the elements of Array A:\n");
for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
scanf("%d",&A[i][j]);
//Entering the values in B[][]
printf("enter the elements of Array B:\n");
for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
scanf("%d",&B[i][j]);
// Calculating C[][]=A[][]+B[][]
for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
C[i][j]=A[i][j]+B[i][j];
i=0;
j=0;
while(i<size)
{
if( i==size/2)
printf("%*c%*c\n\n",6*size+1,'+',13*(size-1),'=');
while(j<size)
printf("%d\t",A[i][j++]);
j=0;
while(j<size)
printf("%d\t",B[i][j++]);
j=0;
while(j<size)
printf("%d\t",C[i][j++]);
j=0;
printf("\n\n");
i++;
}
}
But this code only gives the desired output when size=2.
But I need a solution which works for all values of size entered by the user.
you use a tab (8 columns) to go to the next column, so replace
printf("%*c%*c\n\n",6*size+1,'+',13*(size-1),'=');
by
printf("%*c%*c\n\n",8*size-4,'+',8*size,'=');
example :
of course that supposes all numbers need up to 7 columns
If you want to always have 1 line between each line with numbers modify the while to have :
while(i<size)
{
if( i==size/2)
printf("%*c%*c\n",8*size-4,'+',8*size,'=');
else
putchar('\n');
while(j<size)
printf("%d\t",A[i][j++]);
j=0;
while(j<size)
printf("%d\t",B[i][j++]);
j=0;
while(j<size)
printf("%d\t",C[i][j++]);
j=0;
putchar('\n');
i++;
}
producing :
Out of that I encourage you to check scanf always return 1 else you do not detect an invalid input
This code will solve my question,
int size_odd_or_even=size%2;
int check=1;
while(i<size)
{
if( size_odd_or_even==0 && i==size/2 && check==1)
{
printf("\n%*c%*c\n",8*size-4,'+',8*size,'=');
check=0;
continue;
}
if( size_odd_or_even==1 && i== (int)(size/2) )
{
while( j<size-1)
printf("%d\t",A[i][j++]);
printf("%d + ",A[i][j]);
}
else
{
while( j<size)
printf("%d\t",A[i][j++]);
}
j=0;
if( size_odd_or_even==1 && i== (int)(size/2))
{
while( j<size-1)
printf("%d\t",B[i][j++]);
printf("%d = ",B[i][j]);
}
else
{
while(j<size)
printf("%d\t",B[i][j++]);
}
j=0;
while(j<size)
printf("%d\t",Sum[i][j++]);
j=0;
if( size_odd_or_even==0 && !(i==size/2-1) )
printf("\n\n");
else if(size_odd_or_even==1)
printf("\n\n");
i++;
}
This code gives the outputs for size=4 and size=5 respectively as,
If this code can be optimised then please do tell.
#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.