Monday, December 1, 2014

C++ using Matrix & Binary search & array pro



Different ways to pass an array to a function

#include
#include
using namespace std;
void UpdateArray(int array[])
{
for(int i=0;i<=9;i++)
array[i]=array[i]*array[i];
}
int main()
{
int array[10];
for(int i=0;i<=9;i++)
array[i]=i+1;
UpdateArray(array);
for(int i=0;i<=9;i++)
cout<
return 0;
}

Matrix multiplication code in C++ using 2D arrays

#include
#include
using namespace std;
int main()
{
    const int row=2,col=2;
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;
            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;
}

Add two matrix in C++ code using 2D arrays

#include
#include
using namespace std;
int main()
{
    const int row=2,col=2;
cout<<"Size of Matrices : "< cout<<"Enter Value For First Matrix:"< int first[row][col], second[row][col];
int i,j;
clrscr();
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];
    }
}
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;
}

Binary search code C++ Program

#include
#include
 using namespace std;
  void bubbleSort(int array[], int size);
  bool binarySearch(int array[], int size,int key);
  int main()
{
      cout<<"Enter 5 numbers randomly : "<
            int array[5]; 
     for(int i=0; i<5 i="" span="">
      {
       cout<<"\t";  cin>>array[i]; 
      }
       bubbleSort(array,5);
    cout<<"\n\t\t\tEnter Key To Search: ";
    int key;
    cin>>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;
}
void bubbleSort(int array[], int size)
{
      cout<<"  Input array is: "<
      for(int j=0; j
      {
              cout<<"\t\t\tValue at "<
      }
      cout<
      int temp;
     for(int i2=0; i2
   {
     for(int j=0; j
     {
           if(array[j]>array[j+1])
       {
        temp=array[j];
        array[j]=array[j+1];
        array[j+1]=temp;
       }
     }
   }
      cout<<"  Sorted Array is: "<
     for(int i3=0; i3
   {
    cout<<"\t\t\tValue at "<
   }
}
bool binarySearch(int array[],int size, int key)
{
         int start=1, end=size;
         int mid=(start+end)/2;
  while(start<=end&&array[mid]!=key)
   {
        if(array[mid]
        {
          start=mid+1;
      }
     else
         {
          end=mid-1;
          }
       mid=(start+end)/2;
     }
   if(array[mid]==key)
    return true; 
    else
   return false;
   cout<<"\n\n\n";
}

No comments:

Post a Comment