Pages

Showing posts with label CS/IT. Show all posts
Showing posts with label CS/IT. Show all posts

Saturday, 31 March 2018

Inverse of a Matrix using Gauss Jordan Method in C/C++

PROGRAM :

/*To write a C/C++ program to find the inverse of a matrix using Gauss Jordan Method.*/

#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
    int i, j, k, n;
    float a[10][10] = { 0 }, d;
    cout << "No of rows ";
    cin >> n;
    cout << "\nEnter the elements of the matrix row-wise:\n";
    for (i = 1; i <= n; i++)
        for (j = 1; j <= n; j++)
            cin >> a[i][j];

    for (i = 1; i <= n; i++)
        for (j = 1; j <= 2 * n; j++)
            if (j == (i + n))
                a[i][j] = 1;

    for (i = n; i > 1; i--)
    {
        if (a[i - 1][1] < a[i][1])
            for (j = 1; j <= n * 2; j++)
            {
                d = a[i][j];
                a[i][j] = a[i - 1][j];
                a[i - 1][j] = d;
            }
    }
    cout << "pivoted output: " << endl;
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n * 2; j++)
            cout << a[i][j] << "    ";
        cout << endl;
    }

    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n * 2; j++)
            if (j != i)
            {
                d = a[j][i] / a[i][i];
                for (k = 1; k <= n * 2; k++)
                    a[j][k] -= a[i][k] * d;
            }
    }
    for (i = 1; i <= n; i++)
    {
        d = a[i][i];
        for (j = 1; j <= n * 2; j++)
            a[i][j] = a[i][j] / d;
    }

    cout <<"\nInverse of the matrix is as follows :\n";
    for (i = 1; i <= n; i++)
    {
        for (j = n + 1; j <= n * 2; j++)
            cout << a[i][j] << "    ";
        cout << endl;
    }
    return 0;
}



OUTPUT :

No of rows 3

Enter the elements of the matrix row-wise:
 2    2    3
 2    1    1
 1    3    5

pivoted output:
 2    2    3    1    0    0
 2    1    1    0    1    0
 1    3    5    0    0    1

Inverse of the matrix is as follows :
 2    -1    -1
-9     7     4
 5    -4    -2

Gauss Jordan Method in C/C++

PROGRAM :

/*To write a C/C++ program to find the solution of
the system of linear equations using Gauss Jordan
Method.*/

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int n,i,j,k;
    cout<<"\nEnter the no. of equations\n";        
    cin>>n;
    double a[n][n+1],x[n];
    cout<<"\nEnter the elements of the augmented-matrix row-wise:\n";
    for (i=0;i<n;i++)
        for (j=0;j<=n;j++)    
            cin>>a[i][j];
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < n+1; j++)
            if (j != i)
            {
                double d = a[j][i] / a[i][i];
                for (k = 0; k < n+1; k++)
                    a[j][k] -= a[i][k] * d;
            }
    }
    cout<<"\n\nThe matrix after gauss-jordan elimination is as follows:\n";
    for (i=0;i<n;i++)
    {
        for (j=0;j<=n;j++)
            printf("%4f\t",a[i][j]);
printf("\n");
    }
    for(i=1;i<n;i++)
    {
    for (j=0;j<i;j++)
    {
    a[i][j]=0;
}
}
    for (i=n-1;i>=0;i--)
    {
x[i]=a[i][n];
for (j=i+1;j<n;j++)
            if (j!=i)
x[i]=x[i]-a[i][j]*x[j];
        x[i]=x[i]/a[i][i];
    }
    
    cout<<"\nThe values of the variables are as follows:\n";
    
    for (i=0;i<n;i++){
        cout<<(x[i])<<endl;
}
    return 0;
}



OUTPUT :

Enter the no. of equations
4

Enter the elements of the augmented-matrix row-wise:
10 -7 3 5 6
-6 8 -1 -4 5
3 1 4 11 2
5 -9 -2 4 7


The matrix after gauss-jordan elimination is as follows:
10.000000    0.000000    0.000000    0.000000    50.000000
0.000000     3.800000    0.000000    0.000000    15.200000
0.000000     0.000000    2.447368    0.000000   -17.131579
0.000000     0.000000    0.000000    9.924731     9.924731

The values of the variables are as follows:
5
4
-7
1

Gauss Elimination Method in C/C++

PROGRAM :

/*To write a C/C++ program to find the solution of
the system of linear equations using Gauss Elimination
Method.*/

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int n,i,j,k;
    cout<<"\nEnter the no. of equations\n";        
    cin>>n;
    float a[n][n+1],x[n];
    cout<<"\nEnter the elements of the augmented-matrix row-wise:\n";
    for (i=0;i<n;i++)
        for (j=0;j<=n;j++)    
            cin>>a[i][j];
    for (i=0;i<n-1;i++){
        for (k=i+1;k<n;k++)
            {
                double t=a[k][i]/a[i][i];
                for (j=0;j<=n;j++)
                    a[k][j]=a[k][j]-t*a[i][j];
            }
}
    cout<<"\n\nThe matrix after gauss-elimination is as follows:\n";
    for (i=0;i<n;i++)
    {
        for (j=0;j<=n;j++)
            printf("%4f\t",a[i][j]);
printf("\n");
    }
    for(i=1;i<n;i++)
    {
    for (j=0;j<i;j++)
    {
    a[i][j]=0;
}
}
    for (i=n-1;i>=0;i--)
    {
x[i]=a[i][n];
for (j=i+1;j<n;j++)
            if (j!=i)
x[i]=x[i]-a[i][j]*x[j];
        x[i]=x[i]/a[i][i];
    }
    
    cout<<"\nThe values of the variables are as follows:\n";
    
    for (i=0;i<n;i++){
    if (i==n-1)
    cout<<floor(x[i])<<endl;
    else 
        cout<<round(x[i])<<endl;
}
    return 0;

}


OUTPUT :

Enter the no. of equations
4

Enter the elements of the augmented-matrix row-wise:
10 -7 3 5 6
-6 8 -1 -4 5
3 1 4 11 2
5 -9 -2 4 7


The matrix after gauss-elimination is as follows:
10.000000   -7.000000    3.000000    5.000000    6.000000
0.000000     3.800000    0.800000   -1.000000    8.600000
-0.000000   -0.000000    2.447368   10.315789   -6.815791
0.000000    -0.000000   -0.000000    9.924731    9.924732

The values of the variables are as follows:
5
4
-7
1

Newton Raphson Method in C/C++

PROGRAM :

/*To write a C/C++ program to find roots of an equation using 
Newton Raphson method.*/

#include<iostream>
#include<cmath>
using namespace std;
float a,b,err,x1,x2,x0;
int maxitr,i=1,flag=0;
float f(float x){
return x*log10(x)-1.2;}
float df(float x){
return log10(x)+0.43429;}
int main(){
cout<<"Enter value of a and b :";
cin>>a>>b;
if(f(a)*f(b)>0)
cout<<"No Root or Even Root";
else{
cout<<"Enter maximum iteration and allowed error :";
cin>>maxitr>>err;
x0=(a+b)/2;
while(maxitr>0){
x1=x0;
x2=x1-(f(x1)/df(x1));
cout<<"After "<<i<<" iteration value of x ="<<x2<<"\n";
if(fabs(x1-x2)<err){
cout<<"After "<<i<<" iteration value of x is"<<x2<<"\n";
flag=1;
break;
}
x0=x2;
i++;
maxitr--;
}
if(flag==0)
cout<<"maximum iteration is insufficient";}
return 0;

}


OUTPUT :

Enter value of a and b :5 1
Enter maximum iteration and allowed error :25
0.0001
After 1 iteration value of x = 2.74615
After 2 iteration value of x = 2.74065
After 3 iteration value of x = 2.74065

After 3 iteration value of x is 2.74065

Regula-Falsi Method in C/C++

PROGRAM :

//To write a C/C++ program to find roots of an equation using
//Regula-Falsi method.

#include<stdio.h>
#include<math.h>
float bisect(float a,float b);
float f(float x);
float f(float x)
{
float f;
f=cos(x)-x*exp(x);
return f;
}
float bisect(float a,float b)
{
float c=((b*f(a))-(a*f(b)))/(f(a)-f(b));
return c;
}
void main()
{
float a,b,x0,ae,x1;
int itr,maxitr;
printf("Enter a & b :");
scanf("%f%f",&a,&b);
if (f(a)*f(b)<0)
{
printf("Enter the no. of iterations required and Allowed error");
scanf("%d%f",&maxitr,&ae);
x0=bisect(a,b);
printf("Iteration no. 0 value of x is %f\n",x0);
for(itr=1;itr<=maxitr;itr++)
{
if (f(x0)*f(a)<0)
b=x0;
else a=x0;
x1=bisect(a,b);
printf("Iteration no. %d value of x is %f\n",itr,x1);
if ((fabs(x1-x0))<=ae)
{
printf("root is %f",x1);
break;
}
x0=x1;
}
if (itr==maxitr && fabs(x1-x0)>ae)
printf("Number of iterations is not sufficient");
}


}


OUTPUT :

Enter a & b :0 1
Enter the no. of iterations required and Allowed error25
0.0001

Iteration no. 0 value of x is 0.314665
Iteration no. 1 value of x is 0.446728
Iteration no. 2 value of x is 0.494015
Iteration no. 3 value of x is 0.509946
Iteration no. 4 value of x is 0.515201
Iteration no. 5 value of x is 0.516922
Iteration no. 6 value of x is 0.517485
Iteration no. 7 value of x is 0.517668
Iteration no. 8 value of x is 0.517728

root is 0.517728

Bisection Method in C/C++

PROGRAM :

//To write a C/C++ program to find roots of a polynomial using
//bisection method.


#include<stdio.h>
#include<math.h>
float bisect(float a,float b);
float f(float x);
float f(float x)
{
float f;
f=pow(x,3)-4*x-9;
return f;
}
float bisect(float a,float b)
{
float c=(a+b)/2;
return c;
}
void main()
{
float a,b,x0,ae,x1;
int itr,maxitr;
printf("Enter a & b :");
scanf("%f%f",&a,&b);
if (f(a)*f(b)<0)
{
printf("Enter the no. of iterations required and Allowed error");
scanf("%d%f",&maxitr,&ae);
x0=bisect(a,b);
printf("Iteration no. 0 value of x is %f\n",x0);
for(itr=1;itr<=maxitr;itr++)
{
if (f(x0)*f(a)<0)
b=x0;
else a=x0;
x1=bisect(a,b);
printf("Iteration no. %d value of x is %f\n",itr,x1);
if ((fabs(x1-x0))<=ae)
{
printf("root is %f",x1);
break;
}
x0=x1;
}
if (itr==maxitr && fabs(x1-x0)>ae)
printf("Number of iterations is not sufficient");
}

}




OUTPUT :

Enter a & b :2 3
Enter the no. of iterations required and Allowed error25
0.00001
Iteration no. 0 value of x is 2.500000
Iteration no. 1 value of x is 2.750000
Iteration no. 2 value of x is 2.625000
Iteration no. 3 value of x is 2.687500
Iteration no. 4 value of x is 2.718750
Iteration no. 5 value of x is 2.703125
Iteration no. 6 value of x is 2.710938
Iteration no. 7 value of x is 2.707031
Iteration no. 8 value of x is 2.705078
Iteration no. 9 value of x is 2.706055
Iteration no. 10 value of x is 2.706543
Iteration no. 11 value of x is 2.706299
Iteration no. 12 value of x is 2.706421
Iteration no. 13 value of x is 2.706482
Iteration no. 14 value of x is 2.706512
Iteration no. 15 value of x is 2.706528
Iteration no. 16 value of x is 2.706535

root is 2.706535



Tuesday, 14 February 2017

Triangle Of Asterisk '*' in C Language

#include<stdio.h>
int main ()
{
int n,t,i,j,k;
printf("Enter a number");
scanf("%d",&n);
n=n-1;
k=n;
for (i=0;i<=n;i++)
{ j=k;
k=k-1;
while(j!=0)
{printf(" ");
j=j-1;}
for(t=0;t<=i;t++)
{printf("* ");
}
printf("\n");
}
}





Output

Enter a number 
5
    *
   * * 
  * * *
 * * * * 
* * * * *

Tuesday, 26 January 2016

Matrix addition


# To do addition in matrices # First change your font type as Courier

Wednesday, 20 January 2016

Hollow Diamond Of Asterick '*'

# To print a Hollow diamond of Asterick '*'
# Set your font type As Courier

n=int(raw_input("Enter a no"))
x,y=" ","*"
n=(n-1)/2
b=2
q=-1
m,o=n,n
print (m+1)*x,y
for i in range(n) :
    a=n*x
    n=n-1
    p=q*x
    q=q+1
    if q==0 :
        print a,y,y
    else :
        print a,y,p,p,y
for j in range(m,0,-1) :
    c=b*x
    b=b+1
    r=(m-3)*x
    m=m-1
    if m==0:
        print (o+1)*x,y
    elif m==1 :
        print (o)*x,y,y
    else :
        print c,y,r,r,y

Output
Enter a number 9
      *
     * *
    *   *
   *     *
  *       *
   *     *
    *   *
     * *
      *

Diamond OF Asterick '*'

# To print a diamond of Asterick '*'
# Set your font type As Courier
n=int(raw_input("Enter a no."))
n=(n-1)/2
w=n
x=1
for i in range(n) :
    for j in range(w,0,-1) :
        print " ",
    w=w-1
    for k in range(-1,i*2) :
        print "*",
    print
for i in range(n,-1,-1) :
    for j in range(0,w) :
        print " ",
    w=w+1
    for k in range(i*2-1,-2,-1) :
        print "*",
    print

Output
Enter a no. 9
        *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *
  * * * * * * *
    * * * * *
      * * *
        *

Diamond Number 2

# To print a diamond of numbers
# Set your font type As Courier

n=int(raw_input("Enter a number"))
n=(n)/2
w=n
for i in range(1,n+2) :
    for j in range(w,0,-1) :
        print " ",
    w=w-1
    for l in range(i,0,-1) :
        print l,
    for k in range(2,i+1) :
        print k,

    print
for i in range(n,-1,-1) :
    for j in range(0,w+2) :
        print " ",
    w=w+1
    
    for l in range(i,0,-1) :
        print l,
    for k in range(2,i+1) :
        print k,
    print

Output
Enter a number 9
        1
      2 1 2
    3 2 1 2 3
  4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
  4 3 2 1 2 3 4
    3 2 1 2 3
      2 1 2
        1

         



Diamond number 1

# To print a diamond of numbers
# Set your font type As Courier
n=int(raw_input("Enter a number"))
n=(n)/2
w=n
for i in range(1,n+2) :
    for j in range(w,0,-1) :
        print " ",
    w=w-1
    for k in range(1,i) :
        print k,
    for l in range(i,0,-1) :
        print l,
    print
for i in range(n,-1,-1) :
    for j in range(0,w+2) :
        print " ",
    w=w+1
    for k in range(1,i) :
        print k,
    for l in range(i,0,-1) :
        print l,
    print

Output
Enter a number 9
        1
      1 2 1
    1 2 3 2 1
  1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
  1 2 3 4 3 2 1
    1 2 3 2 1
      1 2 1
        1

         

Tuesday, 19 January 2016

Substitute for Split function on strings in python 2.7

def anurag_split(z,x=" ") :
    z=z+x
    L=[]
    w=len(z)
    for i in range(w) :
        a=""
        n=0
        if z[i-1]==x :
            while(z[i+n]!=x) :
                a=a+z[i+n]
                n=n+1
            L.append(a)
    return L

Bubble Sort in Python 2.7

def bubble_sort(L) :
    for j in range(len(L)-1) :
        print "------ Iteration",j+1,"------"
        for i in range(len(L)-1) :
            if L[i]>L[i+1] :
                a=L[i+1]
                L[i+1]=L[i]
                L[i]=a
            print "List after pass",i+1,":",L

Insertion Sort

def insertion_sort(L) :
    for j in range(1,len(L)) :
        a=L[j]
        i=j
        while L[i-1]>a and i>=1 :
            L[i]=L[i-1]
            i-=1
        L[i]=a
        print "List after pass",j,":",L

Selection Sort in python 2.7

def selection_sort(L) :
    s=0
    y=0
    for j in range(len(L)) :
        for i in range(y,len(L)) :
            if L[s]>L[i] :
                s=i
        a=L[s]
        L[s]=L[y]
        L[y]=a
        y+=1
        print "List after pass",j+1,":",L