Basic programs



Linear search – one dimensional
#include <iostream>

using namespace std;

int main()
{
    int a[5],i,num,flag,count=0;
    cout<<"enter the 5 elements in the array"<<endl;
    for(i=0;i<5;i++)
    {
        cin>>a[i];
    }
    cout<<"enter the element to be searched"<<endl;
    cin>>num;
    for(i=0;i<5;i++)
    {
        if(a[i]==num)
        {
            cout<<"element is present";
            count++;
        }
        else
        {
            flag=1;
        }
    }
    if(flag==1&&count==0)
    {
        cout<<"the element is not present";
    }
    return 0;
}

one dimensional
#include <iostream>

using namespace std;

int main()
{
    int a[5],i,num,flag,count=0;
    cout<<"enter the 5 elements in the array"<<endl;
    for(i=0;i<5;i++)
    {
        cin>>a[i];
    }
    cout<<"enter the element to be searched"<<endl;
    cin>>num;
    for(i=0;i<5;i++)
    {
        if(a[i]==num)
        {
            cout<<"element is present";
            count++;
        }
        else
        {
            flag=1;
        }
    }
    if(flag==1&&count==0)
    {
        cout<<"the element is not present";
    }
    return 0;
}

Two dimensional
#include <iostream>

using namespace std;

int main()
{
    int a[2][2],i,j,num,count=1,flag;
    cout<<"enter 4 elements in the 2 dimensional array"<<endl;
    for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            cin>>a[i][j];
        }
    }
    cout<<"enter an element to be searched"<<endl;
    cin>>num;
    for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            if(a[i][j]==num)
            {
                cout<<"element exists in row "<<i+1<<" column "<<j+1;
                flag=1;
            }
            else
            {
                count++;
            }


        }
    }
    if(count>1&&flag!=1)
    {
        cout<<"the elemenjt is not present";
    }


}




Matrix addition
#include <iostream>

using namespace std;

int main()
{
    int a[2][2],b[2][2],i,j,c[2][2];
    cout<<"enter the elements of first matrix"<<endl;
    for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            cin>>a[i][j];
        }
    }
    cout<<"enter the elements of the second matrix"<<endl;
    for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            cin>>b[i][j];
        }
    }
     for(i=0;i<2;i++)
     {
         for(j=0;j<2;j++)
         {
             c[i][j]=a[i][j]+b[i][j];
         }
     }
     cout<<"the result is "<<endl;
    for(i=0;i<2;i++)
      {
          for(j=0;j<2;j++)
          {
              cout<<c[i][j];
              cout<<'\t';
          }
          cout<<'\n';
      }
    return 0;
}



Matrix subtraction
#include <iostream>

using namespace std;

int main()
{
    int a[2][2],b[2][2],i,j,c[2][2];
    cout<<"enter the elements of first matrix"<<endl;
    for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            cin>>a[i][j];
        }
    }
    cout<<"enter the elements of the second matrix"<<endl;
    for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            cin>>b[i][j];
        }
    }
     for(i=0;i<2;i++)
     {
         for(j=0;j<2;j++)
         {
             c[i][j]=a[i][j]-b[i][j];
         }
     }
     cout<<"the result is "<<endl;
    for(i=0;i<2;i++)
      {
          for(j=0;j<2;j++)
          {
              cout<<c[i][j];
              cout<<'\t';
          }
          cout<<'\n';
      }
    return 0;
}

Perfect square
#include <iostream>

using namespace std;

int main()
{
    int num,i,flag,count=0;
    cout<<"enter the number to be checked"<<endl;
    cin>>num;
    for(i=0;i<=num/2;i++)
    {
        if(num==i*i)
        {
            cout<<"perfect square";
            count++;
        }
        else
        {
            flag=1;
        }

    }
    if(count==0&&flag==1)
    {
        cout<<"not a perfect square";
    }

}

Square root
#include<iostream>
using namespace std;
int main()
{
    int num;
    float low,high,i,inc=1,ans;
    int count=0;
    cout<<"enter a positive number"<<endl;
    cin>>num;                                                                             
    for(i=1;count<5;i=i+inc)
    {
        if(i*i==num)
        {
            ans=i;
            cout<<"ans is"<<ans<<".0000"<<endl;
            break;
        }
        else if(i*i<num)
        {

        }
        else if(i*i > num)
        {
            high = i;
            low = i-inc;
            i=i-inc;
            inc = inc/10;
            count++;
        }
    }
    if(count>0)
    {
        ans=low;
        cout<<"answer is"<<ans;
    }


}

Transpose

#include <iostream>

using namespace std;

int main()
{
    int a[10][10],b[10][10],i,j;
    cout<<"enter the elements of first matrix"<<endl;
    for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            cin>>a[i][j];
        }
    }
     for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            b[i][j]=a[j][i];

        }
    }
    cout<<"the result is"<<endl;
         for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            cout<<b[i][j];
            cout<<'\t';
        }
        cout<<'\n';
    }



}

matrix multiplication
#include <iostream>

using namespace std;

int main()
{
    int p[5][5],q[5][5],r[5][5],a,b,c,d,i,j,k,sum=0;
    cout<<"enter the rows and columns of first matrix"<<endl;
    cin>>a>>b;
    cout<<"enter the elements of first matrix"<<endl;
    for(i=0;i<a;i++)
    {
        for(j=0;j<b;j++)
        {
            cin>>p[i][j];
        }
    }
    cout<<"enter the rows and columns of second matrix"<<endl;
    cin>>c>>d;
    cout<<"enter the elements of second matrix"<<endl;
    for(i=0;i<c;i++)
    {
        for(j=0;j<d;j++)
        {
            cin>>q[i][j];
        }
    }
    if(b!=c)
    {
        cout<<"matrix multipliction cannot be done with this order"<<endl;
    }
    else
    {
        for(i=0;i<a;i++)
        {
            for(j=0;j<d;j++)
            {
                r[i][j]=0;
                for(k=0;k<a;k++)
                {
                   r[i][j]=r[i][j]+p[i][k]*q[k][j];
                }

            }
        }
        cout<<"the result is"<<endl;
        for(i=0;i<a;i++)
        {
            for(j=0;j<d;j++)
            {
               cout<<r[i][j];
               cout<<'\t';
            }
            cout<<'\n';
        }
    }
}



SWAP USING TEMP
#include<iostream>
using namespace std;
void swap(int,int);
void swap1(int &,int &);
int main()
{
                int a,b;
        cout<<"Enter Value Of 1st no.:";
        cin>>a;
        cout<<"Enter Value of 2nd no.:";
        cin>>b;
        swap(a,b);
        swap1(a,b);
}
void swap(int a,int b)
{
    int c;
    c=a;
    a=b;
    b=c;
    cout<<"\nCALL BY VALUE";
    cout<<"\nValue of 1st no. is "<<a<<"\nValue of 2nd no. is "<<b;
}
void swap1(int &a,int &b)
{
    int c;
    c=a;
    a=b;
    b=c;
    cout<<"\nCALL BY REFERENCE";
    cout<<"\nValue of 1st no. is "<<a<<"\nValue of 2nd no. is "<<b;

}

SWAP WITHOUT TEMP

#include <iostream>

using namespace std;
void swap(int,int);
void swap1(int &,int &);
int main()
{
                int a,b;
        cout<<"Enter Value Of 1st no.:";
        cin>>a;
        cout<<"Enter Value of 2nd no.:";
        cin>>b;
        swap(a,b);
        swap1(a,b);
}
void swap(int a,int b)
{
    b=a+b;
    a=b-a;
    b=b-a;
    cout<<"\nCALL BY VALUE";
    cout<<"\nValue of 1st no. is "<<a<<"\nValue of 2nd no. is "<<b;
}
void swap1(int &a,int &b)
{
    b=a+b;
    a=b-a;
    b=b-a;
    cout<<"\nCALL BY REFERENCE";
    cout<<"\nValue of 1st no. is "<<a<<"\nValue of 2nd no. is "<<b;

}





Comments