Saturday, 18 August 2012

matrix


#include<iostream>
#include<conio.h>
using namespace std;
void add(int a[10][10], int b[10][10])
{int c[10][10];
     for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
cout<<"The new matrix C is : ";
for(int i=0;i<3;i++)
{cout<<"\n";
for(int j=0;j<3;j++)
cout<<c[i][j]<<" ";
}
}
void sub(int a[10][10], int b[10][10])
{int c[10][10];
     for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
c[i][j]=a[i][j]-b[i][j];
cout<<"The new matrix C is : ";
for(int i=0;i<3;i++)
{cout<<"\n";
for(int j=0;j<3;j++)
cout<<c[i][j]<<" ";;
}
}
void multn(int a[10][10], int b[10][10])
{int c[10][10];
     for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
c[i][j]=a[i][j]*b[j][i];
cout<<"The new matrix C is : ";
for(int i=0;i<3;i++)
{cout<<"\n";
for(int j=0;j<3;j++)
cout<<c[i][j]<<" ";
}
}
void trace(int a[10][10])
{int s=0,i;
for(i=0;i<3;i++)
s+=a[i][i];
cout<<"The diagonal elements : "<<s;
}
void trnsp(int a[10][10])
{int c[10][10];
     for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
c[i][j]=a[j][i];
cout<<"The new matrix C is : ";
for(int i=0;i<3;i++)\
{cout<<"\n";
for(int j=0;j<3;j++)
cout<<c[i][j]<<" ";
}
}
void main()
{int a[10][10],b[10][10],i,j,ch;
char m;
cout<<"Enter the matrix A : ";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>a[i][j];
cout<<"Enter the matrix B : ";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>b[i][j];
cout<<"\nMENU:\n1)Addn\n2)Subtn\n3)Multpn\n4)Trace\n5)Transpose\nEnter your choice : ";
cin>>ch;
switch(ch)
{case 1 : add(a,b);
 break;
case 2 : sub(a,b);
 break;
case 3 : multn(a,b);
     break;
case 4 : cout<<"Which matrix to trace (A/B) ?? : ";
 cin>>m;
 if(m=='a' || m=='A')
 trace(a);
 else if(m=='b' || m=='B')
 trace(b);
 else
 cout<<"Invalid!!";
 break;
case 5 : cout<<"Which matrix to transpose (A/B) ?? : ";
 cin>>m;
 if(m=='a' || m=='A')
 trnsp(a);
 else if(m=='b' || m=='B')
 trnsp(b);
 else
 cout<<"Invalid!!";
 break;
    }
getch();
}

1 comment:

  1. #include
    #include
    #include
    #include
    struct treenode
    {
    int key_value;
    struct treenode *left;
    struct treenode *right;
    };
    typedef struct treenode node;
    node *root=NULL;

    int c=0;//flag used in findin the min
    void insert(int , node *);
    //1) To insert root node to the tree
    void insertfirst(int key)
    {
    if(root!=NULL)
    insert(key, root);
    else
    {
    root=(node*)malloc(sizeof(node));
    root->key_value=key;
    root->left=NULL;
    root->right=NULL;
    printf("\nroot node added");
    }
    }
    //2) To insert to the tree
    void insert(int key, node *leaf)
    {
    if(key< leaf->key_value)
    {
    if(leaf->left!=NULL)
    insert(key, leaf->left);
    else
    {
    node *temp=(node*)malloc(sizeof(node));
    temp->key_value =key;
    temp->left =NULL;
    temp->right=NULL;
    leaf->left=temp;
    printf("\nelemnet inserted to the left");
    }
    }

    else if(key>=leaf->key_value)
    {
    if(leaf->right!=NULL)
    insert(key, leaf->right);
    else
    {
    node *temp=(node*)malloc(sizeof(node));
    temp->key_value =key;
    temp->left =NULL;
    temp->right=NULL;
    leaf->right=temp;
    printf("\nelemnet inserted to the right");
    }
    }
    }

    //3)To search for a node( Recursive)
    void find(int x,node *leaf)
    {
    if (leaf==NULL)
    printf("\nElement not found !");
    else
    {
    if (x key_value)

    find(x,leaf->left);


    else if ( x> leaf->key_value)

    find(x,leaf->right);


    else
    printf("\nElement Found-> !");
    }
    }

    void main()
    {
    int ch,i=1;
    printf("1)Insert element at the beginning.\n");
    printf("2)Insert element at the specified location.\n");
    printf("3)To search for a node( Non Recursive).\n");
    printf("4)Exit.");
    while(i!=0)
    {
    printf("\nEnter your choice:");
    scanf("%d",&ch);
    switch(ch)
    {
    case 1:int k;
    printf("Enter the element to be inserted:");
    scanf("%d",&k);
    insertfirst(k);
    break;
    case 2:
    int c;
    printf("Enter the element to be inserted:");
    scanf("%d",&c);
    insert(c,root);
    break;
    case 3:int d;
    printf("Enter the element to be found:");
    scanf("%d",&d);
    find(d,root);
    break;
    case 4:exit(4);
    default:printf("Wrong choice.");
    break;
    }
    }
    getch();
    }

    ReplyDelete