Showing posts with label liner algabra. Show all posts
Showing posts with label liner algabra. Show all posts

Sunday, February 8, 2015

C Program to find the determinant of a Matrix

C Program to find the determinant of a Matrix  Size id Define By User

#include<conio.h>
#include<stdio.h>

int a[20][20],m;
int determinant(int f[20][20],int a);
int main()
{
  clrscr();
  int i,j;
  printf("----------------------------------------------------------------------\n");
  printf("-----------------CODE BY PROGRAMMING SEEKERZZ ------------------------\n");
  printf("----------------------------------------------------------------------\n");
  printf("\n\nEnter order of matrix : ");
  scanf("%d",&m);
  printf("\nEnter the elements of matrix\n");
  for(i=1;i<=m;i++)
  {
  for(j=1;j<=m;j++)
  {
  printf("a[%d][%d] = ",i,j);
  scanf("%d",&a[i][j]);
  }
  }
  printf("\n\n---------- Matrix A is --------------\n");
  for(i=1;i<=m;i++)
     {
 printf("\n");
 for(j=1;j<=m;j++)
 {
      printf("\t%d \t",a[i][j]);
 }
     }
  printf("\n \n");
  printf("\n Determinant of Matrix A is %d .",determinant(a,m));
  getch();
}

int determinant(int f[20][20],int x)
{
  int pr,c[20],d=0,b[20][20],j,p,q,t;
  if(x==2)
  {
    d=0;
    d=(f[1][1]*f[2][2])-(f[1][2]*f[2][1]);
    return(d);
   }
  else
  {
    for(j=1;j<=x;j++)
    {
      int r=1,s=1;
      for(p=1;p<=x;p++)
{
 for(q=1;q<=x;q++)
   {
     if(p!=1&&q!=j)
     {
b[r][s]=f[p][q];
s++;
if(s>x-1)
{
  r++;
  s=1;
 }
      }
    }
}
     for(t=1,pr=1;t<=(1+j);t++)
     pr=(-1)*pr;
     c[j]=pr*determinant(b,x-1);
     }
     for(j=1,d=0;j<=x;j++)
     {
       d=d+(f[1][j]*c[j]);
      }
     return(d);
   }

}


Monday, February 2, 2015

Augmented Matrix Solution By reducing it to Row Echelon Form in Java

Read Also:  


Augmented Matrix Solution By reducing it to Row Echelon Form And then Calculate the result by backward substitution (Code in Java) 



package echolon;
import java.util.Scanner;
/**
 *
 * @author usman
 */
public class Echolon {
     Scanner usman=new Scanner(System.in);
    int i,j,k,n,a;
    float [][] A=new float[20][20];
    float  sum;
    float c;
    float [] x=new float[10];
    
    public  Echolon(){
    sum=0;
    System.out.println("\nEnter the order of matrix: ");
    n=usman.nextInt();
    a=n;
    System.out.println("\nEnter the elements of augmented matrix row-wise:\n\n");
    for(i=1; i<=n; i++)
    {
for(j=1; j<=(n+1); j++)

            if(a<j){System.out.println("\nEnter the Value of B: (1st row):- \n ");}
   System.out.println("A["+i+"]["+j+"] : ");
            
   A[i][j]=usman.nextFloat();
}
    }
    }

    void operation() {
    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];
}
   }
}
    }

    x[n]=A[n][n+1]/A[n][n];
     for(i=n-1; i>=1; i--)
    {
sum=0;
for(j=i+1; j<=n; j++)
{
   sum=sum+A[i][j]*x[j];
}
x[i]=(A[i][n+1]-sum)/A[i][i];
    }
    }

    void result(){
    System.out.println("\nThe solution is: \n");
    for(i=1; i<=n; i++)
    {
System.out.println("\nx"+i+" =  "+x[i]+"\t");
    }
    }

void display()
{
System.out.println("\n The Matrix is :-\n ");
for(int i=1;i<=n;i++){
System.out.println("\n");
for(int j=1;j<=n+1;j++){
System.out.println(A[i][j]+"     ");
}}
}


public static void main(String args[]){
Echolon obj=new Echolon();
obj.operation();
obj.display();
obj.result();
}
}



Augmented Matrix Solution By reducing it to Row Echelon Form in C++

Read Also:  


Augmented Matrix Solution By reducing it to Row Echelon Form And then Calculate the result by backward substitution  (Code In C++)....


#include<iostream.h>
#include<conio.h>
class row{
    int i,j,k,n,a;
    float A[20][20],c,x[10],sum;
    public:
    row(){

    sum=0.0;
    cout<<"\nEnter the order of matrix: ";
    cin>>n;
    a=n;
    cout<<"\nEnter the elements of augmented matrix row-wise:\n\n";
    for(i=1; i<=n; i++)
    {
for(j=1; j<=(n+1); j++)
{
  if(a<j){cout<<"\nEnter the Value of B: (1st row):- \n ";}
   cout<<"A["<<i<<"]["<<j<<"] : ";
   cin>>A[i][j];
}
    }
    }

    void operation() {
    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];
}
   }
}
    }

    x[n]=A[n][n+1]/A[n][n];
     for(i=n-1; i>=1; i--)
    {
sum=0;
for(j=i+1; j<=n; j++)
{
   sum=sum+A[i][j]*x[j];
}
x[i]=(A[i][n+1]-sum)/A[i][i];
    }
    }

    void result(){
    cout<<"\nThe solution is: \n";
    for(i=1; i<=n; i++)
    {
cout<<"\nx"<<i<<" =  "<<x[i]<<"\t";
    }
    }

void display()
{
cout<<"\n The Matrix is :-\n ";
for(int i=1;i<=n;i++){
cout<<endl;
for(int j=1;j<=n+1;j++){
cout<<A[i][j]<<"     ";
}}
}

};

void main()
{
clrscr();
row obj;
obj.operation();
obj.display();
obj.result();
getch();

}


Sunday, February 1, 2015

GPA Calculator in JAVA


package usman1;
import java.util.Scanner;
public class Usman1 {
public static void main (String args[]){

  String grade = "";
  double credit1;
  double credit2;
  double credit3;
  double credit4;
  double gradeValue=0;
  double totPtsClass1=0;
  double totPtsClass2=0;
  double totPtsClass3=0;
  double totPtsClass4=0;
  double totPts=0;
  double totalCredits= 0;
  double gpa;

  Scanner console = new Scanner (System.in);
  System.out.println("Please enter the number of credits of the class 1 (A number)");
  credit1 = console.nextDouble();
  System.out.println("Please enter your grades for the class 1(Capital letters such as A,B+, C-)");
  grade = console.next();

  if (grade.equals ("A"))
    gradeValue= 4.00;
  else if (grade.equals("A-"))
    gradeValue= 3.67;
  else if (grade.equals("B+"))
    gradeValue = 3.33;
  else if (grade.equals("B"))
    gradeValue = 3.00;
  else if (grade.equals ("B-"))
    gradeValue = 2.67;
  else if (grade.equals("C+"))
    gradeValue = 2.33;
  else if (grade.equals("C"))
    gradeValue = 2.00;
  else if (grade.equals ("D+"))
  gradeValue = 1.33;
  else if (grade.equals ("D"))
    gradeValue = 1.00;
  else if (grade.equals ("F"))
    gradeValue = 0;
  else if (grade.equals ("FX"))
    gradeValue = 0;
  else
    System.out.println ("Invalid Grade");

  totPtsClass1 = gradeValue * credit1;

  System.out.println("Please enter the number of credits of the class 2 (A number)");
  credit2 = console.nextDouble();
  System.out.println("Please enter your grades for the class 2 (Capital letters such as A,B+, C-)");
  grade = console.next();

  if (grade.equals ("A"))
    gradeValue= 4.00;
  else if (grade.equals("A-"))
    gradeValue= 3.67;
  else if (grade.equals("B+"))
    gradeValue = 3.33;
  else if (grade.equals("B"))
    gradeValue = 3.00;
  else if (grade.equals ("B-"))
    gradeValue = 2.67;
  else if (grade.equals("C+"))
    gradeValue = 2.33;
  else if (grade.equals("C"))
    gradeValue = 2.00;
  else if (grade.equals ("D+"))
  gradeValue = 1.33;
  else if (grade.equals ("D"))
    gradeValue = 1.00;
  else if (grade.equals ("F"))
    gradeValue = 0;
  else if (grade.equals ("FX"))
    gradeValue = 0;
  else
  System.out.println ("Invalid Grade");

  totPtsClass2 = gradeValue * credit2;

  System.out.println("Please enter the number of credits of the class 3 (A number)");
  credit3 = console.nextDouble();
  System.out.println("Please enter your grades for the class 3 (Capital letters such as A,B+, C-)");
  grade = console.next();

  if (grade.equals ("A"))
    gradeValue= 4.00;
  else if (grade.equals("A-"))
    gradeValue= 3.67;
  else if (grade.equals("B+"))
    gradeValue = 3.33;
  else if (grade.equals("B"))
    gradeValue = 3.00;
  else if (grade.equals ("B-"))
    gradeValue = 2.67;
  else if (grade.equals("C+"))
    gradeValue = 2.33;
  else if (grade.equals("C"))
    gradeValue = 2.00;
  else if (grade.equals ("D+"))
  gradeValue = 1.33;
  else if (grade.equals ("D"))
    gradeValue = 1.00;
  else if (grade.equals ("F"))
    gradeValue = 0;
  else if (grade.equals ("FX"))
    gradeValue = 0;
  else
    System.out.println ("Invalid Grade");

    totPtsClass3 = gradeValue * credit3;

  System.out.println("Please enter the number of credits of the class 4 (A number)");
  credit4 = console.nextDouble();
  System.out.println("Please enter your grades for the class 4 (Capital letters such as A,B+, C-)");
  grade = console.next();

  if (grade.equals ("A"))
    gradeValue= 4.00;
  else if (grade.equals("A-"))
    gradeValue= 3.67;
  else if (grade.equals("B+"))
    gradeValue = 3.33;
  else if (grade.equals("B"))
    gradeValue = 3.00;
  else if (grade.equals ("B-"))
    gradeValue = 2.67;
  else if (grade.equals("C+"))
    gradeValue = 2.33;
  else if (grade.equals("C"))
    gradeValue = 2.00;
  else if (grade.equals ("D+"))
  gradeValue = 1.33;
  else if (grade.equals ("D"))
    gradeValue = 1.00;
  else if (grade.equals ("F"))
    gradeValue = 0;
  else if (grade.equals ("FX"))
    gradeValue = 0;
  else
    System.out.println ("Invalid Grade");
  totPtsClass4 = gradeValue * credit4;

  totPts= totPtsClass1+totPtsClass2+totPtsClass3+totPtsClass4;
  totalCredits = credit1+credit2+credit3+credit4;
  gpa= totPts / totalCredits;

  System.out.printf("Your GPA is: %.2f\n", + gpa);
}
}


Transpose Of A Matrix in C++

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10][10];
int b[10][10];
int row=0,col=0,i,j;
cout<<"\n Enter No of Rows And Colomn: ";
cin>>row>>col;
cout<<"\n Enter the values in matrix: (row by row) ";
for(i=1;i<=row;i++){
for(j=1;j<=col;j++){
cin>>a[i][j];
}}
for(i=1;i<=row;i++){
cout<<endl;
for(j=1;j<=col;j++){
cout<<a[i][j]<<"  \t";
}
}
for(i=1;i<=col;i++){
for(j=1;j<=row;j++){
b[j][i]=a[i][j];
} }
cout<<endl;
cout<<"\n Transpose Of The Matrix You Enter:-\n";
for(i=1;i<=row;i++){
cout<<endl;
for(int j=1;j<=col;j++){
cout<<b[i][j]<<"  \t";
}
}
getch();
      }

Another Screen Short of Code in C.



Monday, January 26, 2015

Scientific Calculator in C++

Read Also:  



Scientific Calculator in C++ Programming SeekerzZ




#include<iostream.h>
#include<math.h>
#include<conio.h>
int main ()
{
clrscr();
float a,b,PI;
int c;
cout<<endl;
cout<<"******************************* Calculator *************************************\n";
cout<<"================================================================================\n";
cout<<"Operations\t"<<"\tTrigonometric Functions"<<"\t\tLogarithmic Functions\n";
cout<<"================================================================================\n";
cout<<"1: Division\t\t"<<"7: Sin\t\t"<<"\t\t13: Log"<<endl;
cout<<endl;
cout<<"2: Multiplication\t"<<"8: Cos\t\t"<<"\t\t14: Log with base 10"<<endl;
cout<<endl;
cout<<"3: Subtraction\t\t"<<"9: Tan\t\t"<<endl;
cout<<endl;
cout<<"4: Addition\t\t"<<"10: Inverse of Sin"<<endl;
cout<<endl;
cout<<"5: Exponent\t\t"<<"11: Inverse of Cos"<<endl;
cout<<endl;
cout<<"6: Square root\t\t"<<"12: Inverse of Tan"<<endl;
cout<<endl;
cout<<"Enter the function that you want to perform : ";
cin>>c;
cout<<endl;
PI=3.14159265;
switch(c)
{
case 1:
cout<<"Enter 1st number : ";
cin>>a;
cout<<endl;
cout<<"Enter 2nd number : ";
cin>>b;
cout<<endl;
cout<<"Division = "<<a/b<<endl;
break;
case 2:
cout<<"Enter 1st number : ";
cin>>a;
cout<<endl;
cout<<"Enter 2nd number : ";
cin>>b;
cout<<endl;
cout<<"Multiplication = "<<a*b<<endl;
break;
case 3:
cout<<"Enter 1st number : ";
cin>>a;
cout<<endl;
cout<<"Enter 2nd number : ";
cin>>b;
cout<<endl;
cout<<"Subtraction = "<<a-b<<endl;
break;
case 4:
cout<<"Enter 1st number : ";
cin>>a;
cout<<endl;
cout<<"Enter 2nd number : ";
cin>>b;
cout<<endl;
cout<<"Addition = "<<a+b<<endl;
break;
case 5:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Enter the exponent : ";
cin>>b;
cout<<endl;
cout<<"Exponent = "<<pow(a,b)<<endl;
break;
case 6:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Square Root = "<<sqrt(a)<<endl;
break;
case 7:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Sin = "<<sin(a)<<endl;
break;
case 8:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Cos = "<<cos(a)<<endl;
break;
case 9:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Tan = "<<tan(a)<<endl;
break;
case 10:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Inverse of Sin = "<<asin(a)*180.0/PI<<endl;
break;

case 11:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Inverse of Cos = "<<acos(a)*180.0/PI<<endl;
break;
case 12:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Inverse of tan = "<<atan(a)*180.0/PI<<endl;
break;
case 13:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Log = "<<log(a)<<endl;
break;
case 14:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Log with base 10 = "<<log10(a)<<endl;
break;
default:
cout<<"Wrong Input"<<endl;
}
 getch();
 return 0;

}

Friday, September 26, 2014

Linear Algebra toolkit

This Linear Algebra Toolkit is composed of the modules listed below. Each module is designed to help a linear algebra student learn and practice a basic linear algebra procedure, such as Gauss-Jordan reduction, calculating the determinant, or checking for linear independence.
=====================================================
One of the most Important Tool used for solving Matrix and Matrix operations:

"Matrix Calculator"

Click Here-                             2 Click Here-                   3  Click Here-
=====================================================
Systems of linear equations and matrices
Row operation calculatorInteractively perform a sequence of elementary row operations on the given m x n matrix A.
Transforming a matrix to row echelon formFind a matrix in row echelon form that is row equivalent to the given m x n matrix A.
Transforming a matrix to reduced row echelon formFind the matrix in reduced row echelon form that is row equivalent to the given m x n matrix A.
Solving a system of linear equationsSolve the given system of m linear equations in n unknowns.
Calculating the inverse using row operationsFind (if possible) the inverse of the given n x n matrix A.
Determinants
Calculating the determinant using row operationsCalculate the determinant of the given n x n matrix A.



Sunday, September 21, 2014

C Program to find the Inverse of the Matrix

    #include<stdio.h> #include<math.h> float detrminant(float[][], float); void cofactors(float[][], float); void trans(float[][], float[][], float); void main() { float a[25][25], n, d; int i, j; printf("Enter the order of the matrix:\n"); scanf("%f", &n); printf("Enter the elemnts into the matrix:\n"); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { scanf("%f", &a[i][j]); } } d = detrminant(a, n); printf("\nTHE DETERMINANT IS=%2f", d); if (d == 0) printf("\nMATRIX IS NOT INVERSIBLE\n"); else cofactors(a, n); } float detrminant(float a[25][25], float k) { float s = 1, det = 0, b[25][25]; int i, j, m, n, c; if (k == 1) { return (a[0][0]); } else { det = 0; for (c = 0; c < k; c++) { m = 0; n = 0; for (i = 0; i < k; i++) { for (j = 0; j < k; j++) { b[i][j] = 0; if (i != 0 && j != c) { b[m][n] = a[i][j]; if (n < (k - 2)) n++; else { n = 0; m++; } } } } det = det + s * (a[0][c] * detrminant(b, k - 1)); s = -1 * s; } } return (det); } void cofactors(float num[25][25], float f) { float b[25][25], fac[25][25]; int p, q, m, n, i, j; for (q = 0; q < f; q++) { for (p = 0; p < f; p++) { m = 0; n = 0; for (i = 0; i < f; i++) { for (j = 0; j < f; j++) { b[i][j] = 0; if (i != q && j != p) { b[m][n] = num[i][j]; if (n < (f - 2) n++; else { n = 0; m++; } } } } fac[q][p] = pow(-1, q + p) * detrminant(b, f - 1); } } trans(num, fac, f); } void trans(float num[25][25], float fac[25][25], float r) { int i, j; float b[25][25], inv[25][25], d; for (i = 0; i < r; i++) { for (j = 0; j < r; j++) { b[i][j] = fac[j][i]; } } d = detrminant(num, r); inv[i][j] = 0; for (i = 0; i < r; i++) { for (j = 0; j < r; j++) { inv[i][j] = b[i][j] / d; } } printf("\nTHE INVERSE OF THE MATRIX:\n"); for (i = 0; i < r; i++) { for (j = 0; j < r; j++) { printf("\t%2f", inv[i][j]); } printf("\n"); } }

Sunday, July 6, 2014

How to add two matrix (size is define by user)

#include<iostream.h>

int main()
{
int l,m,z,n;
int matrixA[10][10];
int matrixB[10][10];
int matrixC[10][10];

cout<<"enter the dimension of the first matrix"<<endl;
cin>>l>>m;
cout<<"enter the dimension of the second matrix"<<endl;
cin>>z>>n;
if(m!=z||z!=m){
cout<<"error in the multiblication enter new dimensions"<<endl;
cout<<"enter the dimension of the first matrix"<<endl;
cin>>l>>m;
cout<<"enter the dimension of the second matrix"<<endl;
cin>>z>>n;
}

else{
cout<<"enter the first matrix"<<endl;
for(int i=0;i<l;i++){
for(int j=0;j<m;j++){
     cin>>matrixA[i][j];
     }
     }
cout<<"enter the second matrix"<<endl;
for(int i=0;i<z;i++){
for(int j=0;j<n;j++){
    cin>>matrixB[i][j];
}
}
for(int i=0;i<l;i++){
for(int j=0;j<n;j++){
        matrixC[i][j]=0;
        for(int k=0;k<m;k++){
matrixC[i][j]=matrixC[i][j]+(matrixA[i][k] * matrixB[k][j]);
}
}
}

cout<<"your matrix is"<<endl;
for(int i=0;i<l;i++){
for(int j=0;j<n;j++){
cout<<matrixC[i][j]<<" ";
}
cout<<endl;
}
}
//system("pause");
return 0;
}