Friday 20 January 2012

string concatenation without strcat()


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char str[20],str1[40];
cout<<"Enter source string"<<endl;
gets(str);
cout<<"Enter target string"<<endl;
gets(str1);
int l=0;
for(int i=0;str1[i]!='\0';i++)
{
l++;
}
int j;
  for(i=0,j=l;str[i]!='\0';i++,j++)
  {
  str1[j]=str[i];
  }
  str1[j]='\0';
  cout<<"The concatenated string is "<<str1;
  getch();
    }

Tuesday 17 January 2012

multiplication of matrices


#include<iostream.h>
#include<conio.h>
void main()
{
int mat1[3][3],mat2[3][3],mat3[3][3],i,j,k;
clrscr();
cout<<"Enter the elements in the first matrix"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>mat1[i][j];
}
}
cout<<"Enter the elements in the second matrix"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>mat2[i][j];
}
}
//loop to multiply
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
mat3[i][j]=0;
for(k=0;k<3;k++)
{
mat3[i][j]=mat3[i][j]+(mat1[i][k]*mat2[k][j]);
}
}
}
//loop to display the final matrix
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<mat3[i][j]<<"\t";
}
cout<<endl;
}
getch();
}

sum of two matrices


// program to enter two 2D arrays and display their sum in matrix form
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int mat1[3][3],mat2[3][3],sum[3][3],i,j;
cout<<"Enter elements in first matrix"<<endl;
for(i=0;i<3;i++)        //Row
{
for(j=0;j<3;j++)     //Column
{
cin>>mat1[i][j];
}
}
cout<<"Enter elements in second matrix"<<endl;
for(i=0;i<3;i++)        //Row
{
for(j=0;j<3;j++)     //Column
{
cin>>mat2[i][j];
}
}

// loop to sum two matrices
for(i=0;i<3;i++)        //Row
{
for(j=0;j<3;j++)     //Column
{
      sum[i][j]=mat1[i][j]+mat2[i][j];
}
}
//loop to display sum in matrix form
cout<<"The sum of two matrices is "<<endl;
for(i=0;i<3;i++)        //Row
{
for(j=0;j<3;j++)     //Column
{
cout<<sum[i][j]<<"\t";
}
cout<<endl;
}
getch();
}

matrix transpose


#include<iostream.h>
#include<conio.h>
void main()
{
int mat[3][3],matt[3][3],i,j;
clrscr();
cout<<"Enter the elements in the matrix"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>mat[i][j];
}
}
//loop to transpose a matrix
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
matt[i][j]=mat[j][i];
}
}

cout<<"The Transpose matrix is "<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<matt[i][j]<<"\t";
}
cout<<endl;

}
getch();
}

binary search method


//program to search an element using binary search
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[10],i,j,temp,flag=0;
cout<<"Enter ten numbers"<<endl;
for(i=0;i<10;i++)
{
cin>>arr[i];
}
//loop to sort array
for(i=0;i<10;i++)
{
for(j=0;j<10-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
cout<<"The number in sorted form are"<<endl;
for(i=0;i<10;i++)
{
cout<<arr[i]<<endl;
}

cout<<"Enter a number to search"<<endl;
int num;
cin>>num;
int l=0,h=9,mid;
while(l<=h)
{
mid =(l+h)/2;
if (num>arr[mid])
l=mid+1;
else if (num<arr[mid])
h=mid-1;
else
{
flag=1;
cout<<"The number is found at "<<mid+1<<" position"<<endl;
break;
}
}
if(flag==0)
{
cout<<"The number is not found in the list"<<endl;
}
getch();
}

linear search method


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[10],i,num,flag=0;
cout<<"Enter ten numbers in the array"<<endl;
for(i=0;i<10;i++)
{
cin>>arr[i];
}
cout<<"Enter the number to search"<<endl;
cin>>num;
cout<<"The array elements are"<<endl;
for(i=0;i<10;i++)
{
cout<<arr[i]<<endl;
}

// loop to search the number
for(i=0;i<10;i++)
{
if(num==arr[i])
{
flag=1;
cout<<"The number is found at "<<i+1<<" location"<<endl;
break;
}
}
  if(flag==0)
  {
  cout<<"The number is not found"<<endl;
  }


getch();
}

bubble sort method


#include<iostream.h>
#include<conio.h>
void main()
{
int arr[10],i,j,temp;
cout<<"Enter ten numbers"<<endl;
for(i=0;i<10;i++)
{
cin>>arr[i];
}
//loop to sort array using bubble sort method
for(i=0;i<10;i++)
{
for(j=0;j<10-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
cout<<"the array elements after sorting are"<<endl;
for(i=0;i<10;i++)
{
cout<<arr[i]<<endl;
}
getch();
}

Sunday 15 January 2012

linear search method


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[10],i,num,flag=0;
cout<<"Enter ten numbers in the array"<<endl;
for(i=0;i<10;i++)
{
cin>>arr[i];
}
cout<<"Enter the number to search"<<endl;
cin>>num;
cout<<"The array elements are"<<endl;
for(i=0;i<10;i++)
{
cout<<arr[i]<<endl;
}

// loop to search the number
for(i=0;i<10;i++)
{
if(num==arr[i])
{
flag=1;
cout<<"The number is found at "<<i+1<<" location"<<endl;
break;
}
}
  if(flag==0)
  {
  cout<<"The number is not found"<<endl;
  }


getch();
}

sorting using bubble sort method


#include<iostream.h>
#include<conio.h>
void main()
{
int arr[10],i,j,temp;
cout<<"Enter ten numbers"<<endl;
for(i=0;i<10;i++)
{
cin>>arr[i];
}
//loop to sort array using bubble sort method
for(i=0;i<10;i++)
{
for(j=0;j<10-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
cout<<"the array elements after sorting are"<<endl;
for(i=0;i<10;i++)
{
cout<<arr[i]<<endl;
}
getch();
}