Pages

💡 Now You Can Get your Assignments Sollution here... .💡 100% plagiarism Free Service...💡 Responsibility, punctuality and on-time delivery... 💡 Furnishing you with experts... 💡 experienced in your subject 100% privacy... 💡 helping you with the highest efficiency , professionalism Affordable and budget-friendly costs that do not make a hole in your wallet

Showing posts with label Sorting and Searching. Show all posts
Showing posts with label Sorting and Searching. Show all posts

Swapping,Searching, Replacing in array

#include<iostream>
using namespace std;
void swap(int&,int&);
void swap(int [],int[],int);
void swap(char&,char&);
void swap(char[],char[]);
void printarr(const int[],int);
void printarr(const char[]);
int stringlength(char []);
bool searchsubstring(char[],char[]);
bool replacesubstring(char[],char[],char[]);                             //All green lines arfe function prototypes.
void main()                                                                        //main starts here 
{
char array1[100]={'\0'};                       //here both arrayrs array1 & array2 are of size 100 & 10 ,respectively  
char array2[10]={'\0'};                       //and having value NULL on all of its locations
int a=4,b=32;
int a1[5],b1[5];
for(int i=0;i<5;i++)
{
a1[i]=i;
b1[i]=i+5;
}
char ch1,ch2;
ch1='A';
ch2='z';
char arr1[5]={'A','Z','r','Y'};
char arr2[5]={'b','E','i','s'};
cout<<"Without Swap int\n";
cout<<"a:"<<a<<" b:"<<b<<"\n";
swap(a,b);
cout<<"After Swap\n";
cout<<"a:"<<a<<" b:"<<b<<"\n";
cout<<"________________________________________________________\n";
cout<<"Without swap int[]\narr1:";
for(int i=0;i<5;i++)
cout<<a1[i]<<" ";
cout<<"\narr2:";
for(int i=0;i<5;i++)
cout<<b1[i]<<" ";
swap(a1,b1,5);
cout<<"\nAfter swap int[]\narr1:";
for(int i=0;i<5;i++)
cout<<a1[i]<<" ";
cout<<"\narr2:";
for(int i=0;i<5;i++)
cout<<b1[i]<<" ";
cout<<"\n________________________________________________________\n";
cout<<"Without swap char\nch1:";
cout<<ch1;
cout<<"\nch2:";
cout<<ch2;
cout<<"\nAfter swap char\nch1:";
swap(ch1,ch2);
cout<<ch1;
cout<<"\nch2:";
cout<<ch2;
cout<<"\n________________________________________________________\n";
cout<<"Without swap char[]\narr1:";
for(int i=0;i<5;i++)
cout<<arr1[i]<<" ";
cout<<"\narr2:";
for(int i=0;i<5;i++)
cout<<arr2[i]<<" ";
cout<<"\nAfter swap char[]\narr1:";
swap(arr1,arr2);
for(int i=0;i<5;i++)
cout<<arr1[i]<<" ";
cout<<"\narr2:";
for(int i=0;i<5;i++)
cout<<arr2[i]<<" ";
cout<<"\n________________________________________________________\n";
cout<<"print array 1 of int\n";
cout<<"arr1:";
printarr(a1,5);
cout<<"\n________________________________________________________\n";
cout<<"print array 1 of char\n";
cout<<"arr1:";
printarr(arr1);
cout<<"\n________________________________________________________\n";
cout<<"Enter String:";
cin.getline(array1,100,'\n');
cout<<"length on array1 is:"<<stringlength(array1);
cout<<"\n________________________________________________________\n";
cout<<"search substring in array 1 of char string\n";
cout<<"Enter Sub string:";
cin>>array2;
cout<<searchsubstring(array1,array2);
cout<<"\n________________________________________________________\n";
char array3[10];
cout<<"Search and Replace substring in array 1 of char string\n";
cout<<"Enter Sub string:";
cin>>array2;
cout<<"Enter String to Replace:";
cin>>array3;
replacesubstring(array1,array2,array3);
cout<<"\nReplaced array:";
cout<<array1;
cout<<"\n________________________________________________________\n";
}
void swap(int &a,int& b)
{
a=a+b;
b=a-b;
a=a-b;
}
void swap(int a[],int b[],int size)
{
int temp;
for(int i=0;i<size;i++)
{
temp=a[i];
a[i]=b[i];
b[i]=temp;
}
}
void swap(char& c1,char& c2)
{
char temp;
temp=c1;
c1=c2;
c2=temp;
}
void swap(char a[],char b[])
{
char temp;
for(int i=0;i<5;i++)
{
temp=a[i];
a[i]=b[i];
b[i]=temp;
}
}
void printarr(const int a[],int size)
{
for(int i=0;i<size;i++)
cout<<a[i]<<" ";
}
void printarr(const char a[])
{
for(int i=0;i<5;i++)
cout<<a[i]<<" ";
}
int stringlength(char a[])
{
for(int i=0;i<100;i++)
{
if(a[i]=='\0')
return i;
}
return 0;
}
bool searchsubstring(char a[],char b[])
{
int y=strlen(b);
y--;
for(int i=0;i<100;i++)
{
if(a[i]==b[0])
{
for(int j=0;j<10;j++)
{
if(a[i]==b[j])
{
if(y==j)
{
return 1;
}
i++;
}
}
}
}
return 0;
}
bool replacesubstring(char a[],char b[],char c[])
{
int y=strlen(b);
y--;
for(int i=0;i<100;i++)
{
if(a[i]==b[0])
{
for(int j=0;j<10;j++)
{
if(a[i]==b[j])
{
a[i]=c[j];
if(y==j)
{
return 1;
}
i++;
}
}
}
}
return 0;
}

Binary search

#include<iostream>
using namespace std;
void main()
{
int arr[10],val=0;
for (int i = 0; i < 10; i++)
{
arr[i]=rand()%500;

}
int temp = arr[0];
for (int i = 0; i < 10; i++)
{
for (int j = i; j < 10; j++)
{

if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for (int i = 0; i < 10; i++)
{
cout << arr[i] << "\n";

}
cout << "enter value to search:";
cin >> val;
int s=0, m, e=9;
m = (s + e) / 2;
while (s != e)
{

if (val > arr[m])
{
s = m + 1;
m = (s + e) / 2;
}
if (val < arr[m])
{
e = m;
m = (s + e) / 2;
}
if (val == arr[m] || s == e)
{
cout << "value found\n";
break;
}
}
}