Tuesday, November 11, 2014

Matrices , Binary c++ program



Simple Binary search code example in cpp


#include
#include  
   int main()
{
      cout<<"enter 5 numbers randomly : "<
      size can be change by replacing 5
      int array[5]; //declaring array
      for(int i=0; i<5 i="" span="">
      {
       cout<<"\t";  cin>>array[i];  
       initializing array
      }

     passing arrary for sorting
       bubblesort(array,5);

array has sorted at this point
    cout<<"\n\t\t\tenter key to search: ";
    int key;
    cin>>key;

passing array, size and key to search key
int result=binarysearch(array,5,key);

if(result==1)
cout<<"\n\t\t\tkey found in array "<
else
cout<<"\n\t\t\tkey not found in array "<


return 0;
}


c++ program to add two matrices
 
 
# include
#include 

int main()
{

using const int for array size

    const int row=2,col=2;

if not use const error found



cout<<"size of matrices : "<
cout<<"enter value for first matrix:"<


int first[row][col], second[row][col];



int i,j;

for( i=0;i
{
    cout<<"enter value for row number: "<
    for( j=0;j
{
        cin>>first[i][j];
    }

}


cout<<"\n\n\nenter value for second matrix:"<
for( i=0;i
{

    cout<<"enter value for row number: "<

    for( j=0;j
{
        cin>>second[i][j];

    }

}
resultant matrix
for( i=0;i
{

    for( j=0;j
{
        first[i][j]=first[i][j]+second[i][j];

    }

}

cout<<"\n\n\t\tresultant matrix:"<
for( i=0;i
{
    cout<< endl;
    for( j=0;j
{
        cout<<"\t\t"<
    }

}
return 0;
}

Matrices Multiplication in C++ Code Tutorial

#include
#include 

int main()
{
    const int row=2,col=2;
if not use const error found
cout<<"size of matrices : "<
cout<<"enter value for firstmatrix matrix:"<
 
    int firstmatrix[row][col];
    int secondmatrix[row][col];
    int resultantmatrix[row][col], var;
 
int i,j;
    for( i=0;i
{
        cout<<"enter value for row number: "<
        for( j=0;j
{
            cin>>firstmatrix[i][j];
        }
    }
cout<<"\n\n\nenter value for secondmatrix matrix:"<
    for( i=0;i
{
        cout<<"enter value for row number: "<
        for( j=0;j
{
            cin>>secondmatrix[i][j];
        }
    }
var=0;
resultantmatrixant matrix
        for( i=0;i
{
            for( j=0;j
{
                for(int k=0;k
{
                   var=var+(firstmatrix[i][k]*secondmatrix[k][j]);
                   cout<
                    }
                 resultantmatrix[i][j]=var;
                 var=0;
            }
        }
cout<<"\n\n\t\tresultant matrix:"<
 
    for( i=0;i
{
        cout<< endl;
        for( j=0;j
 
            cout<<"\t\t"<
        }
    }
return 0;
}

No comments:

Post a Comment