Showing posts with label array. Show all posts
Showing posts with label array. Show all posts
Monday, February 16, 2015
C Program to reverse all the strings stored in an array
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
char a[3][50];
int i,j,k,len;
cout<<"Enter 3 strings:
";
for(i=0;i<3;i++)
{
gets(a[i]);
}
cout<<"
The list of orignal strings:
" ;
for(i=0;i<3;i++)
{
cout<<a[i]<<"
";
}
cout<<"
The list of changed string:
";
for(i=0;i<3;i++)
{
len=strlen(a[i]);
for(j=0,k=len-1;k>=0;j++,k--)
{
cout<<a[i][k];
}
cout<<"
";
}
getch();
}
Saturday, February 14, 2015
C Program to explain Binary search in Array
#include<conio.h>
#include<iostream.h>
void main()
{
int search(int [],int,int);
clrscr();
int n,i,a[100],e=-3,res;
cout<<"How Many Elements:";
cin>>n;
cout<<"
Enter Elements of Array in Accending order
";
for(i=0;i<n;++i)
{
cin>>a[i];
}
cout<<"
Enter element to search:";
cin>>e;
res=search(a,n,e);
if(res!=0)
cout<<"
Element is Founded at "<<res+1<<"st position";
else
cout<<"
Element is not found....!!!";
getch();
}
int search(int a[],int n,int e)
{
int f,l,m;
f=0;
l=n-1;
while(f<=l)
{
m=(f+l)/2;
if(e==a[m])
return(m);
else
if(e>a[m])
f=m+1;
else
l=m-1;
}
return 0;
}
Subscribe to:
Posts (Atom)