program for direct diagonalization of a matrix - c

I need a c program for direct diagonalization of a matrix.
i tried many ways in solving it but didn't get the desired output. so please help me out.
my code
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
int n,i,j,k;
float a[10][10],x[10],u,m;
printf("Enter the number of equations :");
scanf("%d",&n);
printf("\nEnter the co-efficients of equations\n");
for(i=1;i<=n;i++)
for(j=1;j<=(n+1);j++)
scanf("%f",&a[i][j]);
printf("\nEntered co-efficient matrix is\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=(n+1);j++)
printf("%.2f\t",a[i][j]);
printf("\n");
}
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
{
if(i != k)
{
u=a[i][k]/a[k][k];
for(j=1;j<=(n+1);j++)
{
a[i][j]=a[i][j]-(u*a[k][j]);
}
}
}
for(i=1;i<=n;i++)
{
m=a[i][i];
for(j=1;j<=(n+1);j++)
{
a[i][j]=a[i][j]/m;
}
}
printf("\nDiagonalised matrix is\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=(n+1);j++)
printf("%.2f\t",a[i][j]);
printf("\n");
}
printf("\nsolution vector is\n");
for(i=1;i<=n;i++)
{j=(n+1);
printf("x[%d]=%.2f\n",i,a[i][j]);
}
}
output should be like this, if i enter any coefficient ....lower and upper triangular elements should be zero and i should also need to check whether the determinants value remains same or not.

Related

loop not iterating in c

I have recently started coding and was trying to write a program to find the inverse of a matrix using Gauss Jordan method. I try to transform the given matrix to an identity matrix by using ro transformations and apply the same to corresponding elements of identity matrix.
#include <stdio.h>
void main()
{
float a[5][5],b[5][5]={};
int n,i,j,k,m,o,p,q;
printf("Enter order n of square matrix : ");
scanf("%d",&n);
printf("Enter %d elements : ",n*n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%f",&a[i][j]);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%10f",a[i][j]);
printf("\n");
}
if(n>=3)
{
for(i=0;i<n;i++)
b[i][i]=1;
for(i=0;i<n-1;i++)
{
if(a[i][i]==0)
for(m=i+1;m<n;m++)
if(a[m][i]!=0)
{
for(o=0;o<n;o++)
{
a[m][o]+=a[m][o]+a[i][o];
a[i][o]=a[m][o]-a[i][o];
a[m][o]=a[m][o]-a[i][o];
b[m][o]+=b[m][o]+b[i][o];
b[i][o]=b[m][o]-b[i][o];
b[m][o]=b[m][o]-b[i][o];
}
break;
}
for(j=i+1;j<n;j++)
{if(a[j][i]!=0)
{
for(k=0;k<n;k++)
{
b[j][k]=b[j][k]*a[i][i]-a[j][i]*b[i][k];
a[j][k]=a[j][k]*a[i][i]-a[j][i]*a[i][k];
}
}
for(p=0;p<n;p++)
{
for(q=0;q<n;q++)
printf("%10f",a[p][q]);
printf("\t\t");
for(q=0;q<n;q++)
printf("%10f",b[p][q]);
printf("\n");
}}
}
for(i=n-1;i>0;i--)
for(j=i-1;j>=0;j--)
if(a[j][i]!=0)
{
for(k=0;k<n;k++)
{
b[j][k]=b[j][k]*a[i][i]-a[j][i]*b[i][k];
a[j][k]=a[j][k]*a[i][i]-a[j][i]*a[i][k];
}
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
b[i][j]=(b[i][j])/(a[i][i]);
printf("\n\n\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%10f",b[i][j]);
printf("\n");
}
}
}
It is not giving the required output(correct inverse) and so i tried to print the two matrices after each row transformation(for the beginning part). Then i found that one of the loops was not iterating(the k variableloop) and loop was only running for k=0.I can't seem to find what is wrong.
the input which i was using was 3 , 1 2 3 4 5 6 7 8 8

Designing and utilizing variable matrix in C

I am having some trouble designing a matrix in my project. How would you define a variable matrix? I am using an LED Matrix for a connect four game using HCS12 Dragon 12 Light, and I want to define a matrix that allows me to light up the board. How do I define a variable matrix [8]x[8] with rows x columns? Furthermore, how might I call certain points of this matrix in other functions like if else statements? Thanks for any responses
To clarify further, I am just wondering about syntax. I want to define an 8x8 matrix 2D array that is variable based on the game results. Furthermore, I am wondering how I would be able to call certain entries in the matrix in functions and statements
#include <stdio.h>
void main()
{
int a[25][25],b[25][25],c[25][25],i,j,k,r,s;
int m,n;
printf("Enter the first matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the second matrix\n");
scanf("%d%d",&r,&s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
{
printf("\n Enter the elements of first matrix ");
for(i= 0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("\t%d",&a[i][j]);
}
printf("\n Enetr the elements of second matrix ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("\t%d",&b[i][j]);
}
printf("\n The element of first matrix is");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
}
printf("\n The element of second matrix is");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",b[i][j]);
}
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0;k<m;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("\n Multiplication of two matrix is");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",c[i][j]);
}
}

Output error 0 in a basic c program in finding repititions

I am trying to write a C program to print the number of repetitions for a given number in an array. what ever value I give as the input , the function returns the value only 0 to me.. what might be the error..the code is attached
#include<Stdio.h>
#include<conio.h>
int occurence(int n, int arr[], int x);
void main()
{
int x,arr[100],n,i;
clrscr();
printf("\nEnter the number of elements: ");
scanf("%d",&n);
printf("\nEnter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",arr[i]);
}
printf("\nEnter the element to be searched for repetitions: ");
scanf("%d",&x);
printf("%d",occurence(n,arr,x));
getch();
}
int occurence(int n,int arr[100], int x)
{
int i,rep=0;
for(i=0;i<n;i++)
{
if(x == arr[i])
{
rep++;
}
}
return rep;wenter code here
}
Problem is on the line 13 it is supposed to be scanf("%d",&arr[i]);

Program to accept an array of n elements and count number of palindromes

#include<conio.h>
#include<iostream.h>
void main()
{
clrscr();
int x,d,a[10],i,sum=0,count=0,n;
cout<<"Enter no of numbers:";
cin>>n;
for(i=0;i<n;++i)
{
cout<<"Enter number"<<i+1<<":";
cin>>a[i];
}
for(i=0;i<n;++i)
{
x=a[i];
while(x!=0)
{
d=x%10;
sum=sum*10+d;
x=x/10;
}
if(sum==a[i])
count++;
}
cout<<"No of palidromes:"<<count;
}
I entered 121,134 and 1331 but the output was always 1. In fact I tried more numbers and still got only 1. Please tell me what's wrong.
Add sum = 0; after your x=a[i];

Trying to insert element in an array using pointers, but not successfull

Help me out....There is no compile time error but there is some logical error which i am not able to sort out.
Input is taken from user without any problem but the elements are not getting inserted.
Output is unchanged array that user inputted.
void insert(int*,int);
void main()
{
int a[10];
int i,n,pos,x,j,z;
clrscr();
printf("Enter Size Of an array: ");
scanf("%d",&n);
printf("Enter Elements of an array: ");
for(i=0;i<n;i++)
{
scanf("%d",a+i);
}
insert(a,n);
printf("\nArray after Insertion of elements at 2nd & 5th Position\n");
for(i=0;i<n;i++)
{
printf("\t%d\t",*a+i);
}
getch();
}
void insert(int *b, int n)
{
if(n>=1)
{
printf("Insert Element at 2nd Position: ");
scanf("%d",b+1);
}
if(n>=4)
{
printf("Insert Element at 5th Position: ");
scanf("%d",b+4);
}
}
Your output is wrong, and it's caused by *a+i being interpreted as (*a)+i. This also shows up without any modification when you enter something other than a direct sequence:
Enter Size Of an array: 3
Enter Elements of an array: 1 2 9
gives the output:
1 2 3
which is clearly not right.
The solution, as mentioned in another couple of answers is to wrap your pointer-arithmetic with paranthesis: *(a+i).
"Output is unchanged array that user inputted."
Fix :-
for(i=0;i<n;i++)
{
printf("\t%d\t",*(a+i)); //Notice `()`
}
FYI..this is not insertion, this is just an over-write !
For Insertion you could have something like following :
/*
b= original array
n= size of array (must be large enough)
pos = position of insertion
After call make sure to scan array till n+1
*/
void insert(int *b, int n, int pos)
{
int val,c;
printf("Enter the value to insert\n");
scanf("%d", &val);
for (c = n - 1; c >= pos - 1; c--)
b[c+1] = b[c];
b[pos-1] = val;
}
You should try using parenthesis around the a+i, e.g.
void main()
{
int a[10];
int i,n,pos,x,j,z;
clrscr();
printf("Enter Size Of an array: ");
scanf("%d",&n);
printf("Enter Elements of an array: ");
for(i=0;i<n;i++)
{
scanf("%d",a+i);
}
insert(a,n);
printf("\nArray after Insertion of elements at 2nd & 5th Position\n");
for(i=0;i<n;i++)
{
printf("\t%d\t", /*SEE HERE */ *(a+i));
}
getch();
}
for(i=0;i<n;i++)
{
printf("\t%d\t",*(a+i)); //parenthesis added.
}
The parenthesis around the "(a+i)" are of utmost importance as together with the asterisk it indicates the value within that address. Errors like this are hard to find as they are very small but have a huge impact on the code.
Finally I solved the problem with the help of mistake pointed out by #P0W and #Mats and also applying concept of inserting element at any user defined position. Thanks Guys!!
So Here is the Corrected code. (And it is inserting element and not replacing it).
void insert(int*,int);
void main()
{
int a[10];
int i,n,pos,x,j,z;
clrscr();
printf("Enter Size Of an array: ");
scanf("%d",&n);
printf("Enter Elements of an array: ");
for(i=0;i<n;i++)
{
scanf("%d",a+i);
}
insert(a,n);
printf("\nArray after Insertion of elements at 2nd & 5th Position\n");
for(i=0;i<n;i++)
{
printf("\t%d\t",*(a+i));
}
getch();
}
void insert(int *b, int n)
{
int j;
if(n>=1)
{
printf("Insert Element at 2nd Position: ");
for(j=n-1;j>=1;j--)
{
b[j+1]=b[j];
}
scanf("%d",b+1);
}
if(n>=4)
{
printf("Insert Element at 5th Position: ");
for(j=n-1;j>=4;j--)
{
b[j+1]=b[j];
}
scanf("%d",b+4);
}
}

Resources