Showing posts with label programs of array. Show all posts
Showing posts with label programs of array. Show all posts

program for binary search of elements which are already sorted

Tuesday, September 1, 2009

#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{ clrscr();
 int a[100],n,item,beg,mid,loc=-1,end;
 cout<<"enter the number of items: ";
 cin>>n;
 cout<<"enter the sorted element: "<<endl;
 for(int i=0;i<n;i++)
 cin>>a[i];
 cout<<"list of the item is"<<endl;
 for(i=0;i<n;i++)
 cout<<"element at location '"<<i<<"' = "<<a[i]<<endl;
 cout<<"enter the item to be searched";
 cin>>item;
 beg=0;
 end=n-1;
 while(beg<=end&&loc==-1)
 {  mid=(beg+end)/2;
    if(item==a[mid])
    {loc=mid;
     cout<<"location of the element is: "<<loc;
     exit;
     }
     else if(item<a[mid])
     end=mid-1;
     else
     beg=mid+1;
  }
    if(loc==-1)
    cout<<"item could not found";
  getch();
  }

program to insert the element in the array

#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int a[100],n,pos,item;
 cout<<"enter the total number of elements ";
 cin>>n;
 for(int i=0;i<n;i++)
 {
  cout<<"enter the '"<<i<<"'th position element ";
  cin>>a[i];
  }
  cout<<"enter the position at which the element to be inserted: ";
  cin>>pos;
  if(pos<=n)
  {
  cout<<"enter the element to be inserted: ";
  cin>>item;
  a[pos]=item;
  for(i=n+1;i>pos;i--)
  { a[i+1]=a[i];}
  cout<<"elements after insertion is"<<endl;
  for(i=0;i<=n;i++)
  cout<<a[i]<<"  ";
  }
  else
  cout<<"array could not be filled by jumping one or more position";
  getch();
  }

program to find the product of matrix

#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int a[50][50],b[50][50],c[50][50],p,q,r,s;
 cout<<"enter the rows and columns of first matrix"<<endl;
 cin>>p>>q;
 cout<<"enter the elements of first matrix"<<endl;
 for(int i=0;i<p;i++)
  for(int j=0;j<q;j++)
   cin>>a[i][j];
 cout<<"first matrix is"<<endl;
 for(i=0;i<p;i++)
  {
   for(j=0;j<q;j++)
   cout<<a[i][j]<<'\t';
   cout<<endl;
   }
 cout<<endl<<endl;
 cout<<"enter the rows and columns of second matrix"<<endl;
 cin>>r>>s;
 cout<<"enter the elements of second matrix"<<endl;
 for(i=0;i<r;i++)
  for(j=0;j<s;j++)
  cin>>b[i][j];
  cout<<"second matrix is"<<endl;
  for(i=0;i<r;i++)
  {
   for(j=0;j<s;j++)
   cout<<b[i][j]<<'\t';
   cout<<endl;
  }
  if(q==r)
  {
    for(i=0;i<p;i++)
     for(j=0;j<s;j++)

program to search a number present in number of times in the array and also find its location

#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int x[50],n,m;
 cout<<"enter number of elements in the array: ";
 cin>>n;
 cout<<"enter the elements"<<endl;
 for(int i=0;i<n;i++)
 cin>>x[i];
 cout<<"enter the element to be searched: ";
 cin>>m;
 int count=0;
 for(int j=0;j<n;j++)
 {
   if(x[j]==m)
   {cout<<m<<" is present at location "<<"x["<<j<<"]"<<endl;
   count+=1;}
   }
   cout<<m<<" present the number of times= "<<count;
   getch();
   }

program of seletion sort

#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int a[100],smal,n,temp;
 cout<<"enter the number of elements: ";
 cin>>n;
 cout<<"enter the elements: "<<endl;
 for(int i=0;i<n;i++)
 cin>>a[i];
 for(i=0;i<n;i++)
 {
  smal=a[i];
   for(int j=i+1;j<n;j++)
    {
     if(smal>a[j])
      {
    temp=smal;
    smal=a[j];
    a[j]=temp;
       }
     }
  a[i]=smal;
  }
  cout<<"elements after selection sort"<<endl;
  for(i=0;i<n;i++)
  cout<<a[i]<<" ";
  getch();
  }

program to sort the array using function

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
 int x[10],n;
 cout<<"enter number of elements:";
 cin>>n;
 void sort(int x[],int);
 cout<<"enter the elements:";
 for(int i=0;i<n;i++)
  cin>>x[i];
  sort(x,n);
  cout<<"elements after sorted become:";
  for(int j=0;j<n;j++)
  cout<<x[j]<<'\t';
  getch();
  }
  void sort(int x[],int a)
  {
   for(int i=0;i<a-1;i++)
    {
     for(int j=i+1;j<a;j++)
      {
    if(x[i]>x[j])
    { int m;
     m=x[j];
     x[j]=x[i];
     x[i]=m;
     }
       }
     }
    }

program to find the sum of diagonal elements of the matrix

#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int x[10][10],m,n,sum=0;
 cout<<"number of rows and columns"<<endl;
 cin>>m>>n;
 for(int i=0;i<m;i++)
  for(int j=0;j<n;j++)
  {
   cin>>x[i][j];
   if(i==j)
   sum+=x[i][j];
   }
  cout<<"sum of diagonal elements: ";
  cout<<sum;
  getch();
  }

program to find the sum of rows of the matrix

#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int x[10][10],n,m;
 cout<<"number of rows and colomns"<<endl;
 cin>>m>>n;
 cout<<"enter the elements: ";
 for(int i=0;i<m;i++)
 for(int j=0;j<n;j++)
 cin>>x[i][j];
 cout<<"sum of rows"<<endl;
 for(i=0;i<m;i++)
 {
  int sum=0;
   for(j=0;j<n;j++)
   {
    cout<<x[i][j]<<"\t";
    sum+=x[i][j];
    }
   cout<<"="<<sum<<endl;
  }
 cout<<"sum of colomns"<<endl;
 for(j=0;j<n;j++)
 {
  int sum=0;
  for(i=0;i<m;i++)
   {
    cout<<x[i][j]<<"\t";
    sum+=x[i][j];
    }
    cout<<"="<<sum<<endl;
  }
 getch();
 }

program to find the largest number in each row of the matrix?

#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int x[10][10],m,n;
 cout<<"number of rows and columns"<<endl;
 cin>>m>>n;
 for(int i=0;i<m;i++)
 {
  int largest=0;
  for(int j=0;j<n;j++)
  {
   cin>>x[i][j];
   if(x[i][j]>largest)
   largest=x[i][j];
   }
 cout<<"largest number of "<<i+1<<" row: "<<largest<<endl;
 }
 getch();
 }

program to find the transpose of a matrix

#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int x[10][10],m,n;
 cout<<"number of rows and columns"<<endl;
 cin>>m>>n;
 cout<<"enter the elements: ";
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>x[i][j];
cout<<"matrix is: "<<endl;
for(i=0;i<m;i++)
{  for(j=0;j<n;j++)
   cout<<x[i][j]<<"\t";
   cout<<endl;
 }
 cout<<endl<<endl<<endl<<endl;
 cout<<"transpose of this matrix is: "<<endl;
 for(j=0;j<n;j++)
 { for(i=0;i<m;i++)
   cout<<x[i][j]<<"\t";
   cout<<endl;
 }
 getch();
 }