April 2019


C Program For Insertion Sort Using time.h Function and Generating Random Number

#include <math.h> 
#include<time.h>
#include<stdlib.h>
#include <stdio.h>
void insertionSort(int arr[], int n)
{
    int i, key, j;
     for (i = 1; i < n; i++)
    {
        key = arr[i];
         j = i - 1;
         while (j >= 0 && arr[j] > key)
        {
            arr[j + 1] = arr[j];
            j = j - 1;
         }
         arr[j + 1] = key;
     }
}
void printArray(int arr[], int n)
{
    int i;
    for (i = 0; i < n; i++)
         printf("%d  ", arr[i]);
     printf("\n");
}
int main()
{
     int arr[100],n,i;
     time_t t;
     printf("Enter the max number \n");
     scanf("%d",&n);
     srand((unsigned) time(&t));
     for(i=0;i<n;i++)
     {
         arr[i]=rand()%100;
     }
     printf("\nThe random numbers are:\n");
     for(i=0;i<n;i++)
     {
         printf("%d  ",arr[i]);
     }
     insertionSort(arr, n);
    printf("\n\nTime taken to complete the insertion sort %u\n",clock()/CLOCKS_PER_SEC);
    printf("\nThe sorted list is:\n");
    printArray(arr, n);
     return 0;
}

Related Posts

Quick Sort

Bubble Sort

Merge Sort

Selection Sort

C Program For Selection Sort | C Programming

C Program For Quick Sort | C Programming

C Program For Merge Sort | C Programming

C Program For Bubble Sort | C Programming


Eigenvalues

Eigenvalues are a special set of scalars associated with a linear system of equation (i.e., a matrix equation) that are sometimes also known as characteristic roots, characteristic values, proper values, or latent roots.


The determination of the eigenvalues and eigenvectors of a system is extremely important in physics and engineering, where it is equivalent to matrix diagonalization and arises in such common applications as stability analysis, the physics of rotating bodies, and small oscillations of vibrating systems, to name only a few. 

Each eigenvalue is paired with a corresponding so-called eigenvector (or, in general, a corresponding right eigenvector and a corresponding left eigenvector; there is no analogous distinction between left and right for eigenvalues.)

The scalar that is used to transform (stretch) an Eigenvector.

Eigenvectors and eigenvalues are used to reduce noise in data. They can help us improve efficiency in computationally intensive tasks. They also eliminate features that have a strong correlation between them and also help in reducing over-fitting.

The decomposition of a square matrix A into eigenvalues and eigenvectors is known in this work eigendecomposition, and the fact that this decomposition is always possible as long as the matrix consisting of the eigenvectors of A is square is known as the eigendecomposition theorem.


Eigenvectors

Every vector (list of numbers) has a direction when it is plotted on an XY chart. Eigenvectors are those vectors when a linear transformation (such as multiplying it to a scalar) is performed on them then their direction does not change. 

Eigenvectors are a special set of vectors associated with a linear system of the equations (i.e., matrix equations) that are sometimes also known as characteristic vectors, proper vectors, or latent vectors. The determination of the eigenvectors and eigenvalues of a system is extremely important in physics and engineering, where it is equivalent to matrix diagonalization and arises in such common applications as stability analysis, the physics of rotating bodies, and small oscillations of vibrating systems, to name only a few. 

Each eigenvector is paired with a corresponding so-called eigen value. Mathematically, two different kinds of eigenvectors need to be distinguished: left eigenvectors and right eigen vectors. However, for many problems in physics and engineering, it is sufficient to consider only right eigenvectors. The term "eigenvector" used without qualification in such applications can therefore be understood to refer to a right eigenvector.The decomposition of a square matrix A into eigenvalues and eigenvectors is known in this work as eigen decomposition, and the fact that this decomposition is always possible as long as the matrix consisting of the eigenvectors of A is square is known as the eigen decomposition theorem.



C program of eigen value and vector

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{

int i,j,n;
float A[40][40],x[40],z[40],e[40],zmax,emax;
printf("\nEnter the order of matrix:");
scanf("%d",&n);
printf("\nEnter matrix elements row-wise\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
printf("A[%d][%d]=", i,j);
scanf("%f",&A[i][j]);
}
}
printf("\nEnter the column vector\n");
for(i=1; i<=n; i++)
{
printf("X[%d]=",i);
scanf("%f",&x[i]);
}
do
{
for(i=1; i<=n; i++)
{
z[i]=0;
for(j=1; j<=n; j++)
{
z[i]=z[i]+A[i][j]*x[j];
}
}
zmax=fabs(z[1]);
for(i=2; i<=n; i++)
{
if((fabs(z[i]))>zmax)
zmax=fabs(z[i]);
}
for(i=1; i<=n; i++)
{
z[i]=z[i]/zmax;
}
for(i=1; i<=n; i++)
{
e[i]=0;
e[i]=fabs((fabs(z[i]))-(fabs(x[i])));
}
emax=e[1];
for(i=2; i<=n; i++)
{
if(e[i]>emax)
emax=e[i];
}
for(i=1; i<=n; i++)
{
x[i]=z[i];
}
}
while(emax>0.001);
printf("\n The required eigen value is %f",zmax);
printf("\n\nThe required eigen vector is :\n");
for(i=1; i<=n; i++)
{
printf("%f\t",z[i]);
}
getch();

C++ program of eigen value and vector


#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
cout.precision(2);
cout.setf(ios::fixed);
int n,i,j;
cout<<"\nEnter the order of the matrix:\n"; //Get the order of the square matrix
cin>>n;
double a[n][n],b[n],c[n],k,eps,y; //declare arrays and other variables to be used
cout<<"\nEnter the elements of matrix row-wise:\n";
for (i=0;i<n;i++) //Get the elements of the matrix
for (j=0;j<n;j++)
cin>>a[i][j];
cout<<"\nEnter the initial values of the eigen-vector:\n";
for (i=0;i<n;i++) //Get the iinitial guess for the eigen vector
cin>>b[i];
cout<<"\nEnter the accuracy desired:\n";
cin>>eps;
k=b[0]; //Assign some initial value to the eigen value, 'k'
do
{
y=k;
for (i=0;i<n;i++) //Calculate axb where a is the matrix and b its eigen vector
{
c[i]=0;
for (j=0;j<n;j++)
c[i]=c[i]+a[i][j]*b[j]; //After all the iterations axb=c
}
k=abs(c[0]);
for (i=1;i<n;i++)
k=abs(c[i])>k?abs(c[i]):k; //Find the largest element of c and assign it to k where k is the eigen value
for (i=0;i<n;i++)
b[i]=c[i]/k; //Calculate the new Eigen Vector
}while (abs(k-y)>eps); //Check if the error in eigen value is within the tolerance limit
cout<<"The eigen-value is: "<<k<<endl;
cout<<"\nAnd the Eigen-vector is [";
for (i=0;i<n;i++)
cout<<b[i]<<setw(9);
cout<<"]\n";
return 0;
}