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++)
Related
I have tried to remove the duplicate elements from an array
using c language, here I'am using 3 for loop to remove the duplicate elements.
How can this optimize the code?
any other simple method to remove the duplicate elements?
Thanks.
#include <stdio.h>
#define MAX_SIZE 100 // Maximum size of the array
int main()
{
int arr[MAX_SIZE];
int size;
int i, j, k;
printf("Enter size of the array : ");
scanf("%d", &size);
printf("Enter elements in array : ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
if(arr[i] == arr[j])
{
for(k=j; k<size; k++)
{
arr[k] = arr[k + 1];
}
size--;
j--;
}
}
}
printf("\nArray elements after deleting duplicates : ");
for(i=0; i<size; i++)
{
printf("%d\t", arr[i]);
}
return 0;
}
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.
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
Basically i want to create a 2 dimensional array size NxN, find the maximum value in each row and replace it in the upper triangle or rather replace the elements with the maximum it for that specific row, but above the diagonal.
#include <stdio.h>
#include <stdlib.h>
#define MAXIMUM 100
int main()
{
int n, i, j, temp,m;
float a[MAXIMUM][MAXIMUM], max;
printf("dimensions: ");
scanf("%d",&n);
printf("input elements\n");
for (i=0; i<n; i++){
for (j=0; j<n; j++){
scanf("%f",&a[i][j]);
}
}
max=a[0][0];
for (i=0; i<n; i++){
for (j=0; j<n; j++)
{
if(max<a[i][j]){
max=a[i][j];
}
for(m=0;m<n;m++) //the problem starts here
{
if(max>a[i][m]){
if(i+m>n-1){
a[i][m]=max;
}
}
}
}
}
for(i = 0; i < n; i++) {
printf("\n");
for(j = 0; j < n; j++) {
printf("%f ", a[i][j]);
}
}
return 0;
}
J loop finishes every times, then m loop starts so the value of j is n-1 when it is in m loop.That is a problem.
maybe like this
for (i=0; i<n; i++){
max=a[i][0];
for (j=1; j<n; j++){
if(max<a[i][j]){
max=a[i][j];
}
}
for(m=i;m<n;m++){
a[i][m]=max;
}
}