C++ ONLY 1) write your own string handling function that takes in two character
ID: 3560232 • Letter: C
Question
C++ ONLY
1) write your own string handling function that takes in two character arrays as arguments and returns (with the return statement) a character array composed of concatenating the two strings arguments together. do not use the strcat function. do this manually with pointer or array manipulation.
2) Write a function censorByte that takes in the name of a file and a byte value. this function should then create a new file with the .redacted extension and save the previous file to this file, with all bytes of the appropriate value replaced with the value 0.
3) selection sort each iteration normally finds the smallest element left in the unsorted part and puts it at the end of the sorted part. write the variant of this sort that finds the smallest and second smallest elements in the unsorted part and adds them both to the sorted part each iteration.
4) write a boolean recursive function that takes an array, the size of the array and a number. the recursive function should return true if the number is in the array and false otherwise.
5) write a function that takes as arguments a STL queue and an STL stack. the function should then essentially "swap" the structures, putting all the elements in the stack into the queue and vice versa. the end ordering of the elements doesnt matter, as long as the end of your function, every elememt that was in the stack is in the queue and vice versa.
6) write a template for a function that takes in an array as an argument and the size of the array, the function should then see if there are any duplicate elements in the array. if it finds a duplicate, throw an exception.
Explanation / Answer
1)
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
private:
char st1[15];
char st2[15];
char st[30];
public:
void read();
void concat();
void cmpr();
};
void string ::read()
{
cout<<"enter First String :"<<endl;
cin>>st1;
cout<<"enter Second String :"<<endl;
cin>>st2;
}
void string::concat()
{
strcpy(st,st1);
strcat(st," ");
strcat(st,st2);
cout<<"After concatenate :"<<st<<endl;
}
void string::cmpr()
{
if (strcmp(st1,st2)==0)
cout<<"Both String are Identical "<<endl;
else
cout<<"both String are different "<<endl;
}
void main()
{
string x;
clrscr();
x.read();
x.concat();
x.cmpr();
getch();
}
6)
3)
void bubbleSort(int array[], int size);
bool binarySearch(int array[], int size,int key);
int main(){
cout<<"Enter 5 numbers randomly : "<<endl;
// Size can be change by replacing 5
int array[5]; //Declaring array
for(int i=0; i<5; i++)
{
cout<<" "; cin>>array[i]; // Initializing array
}
//Passing Arrary for Sorting
bubbleSort(array,5);
// Array has Sorted At This Point
cout<<" Enter Key To Search: ";
int key;
cin>>key;
//Passing Array, size and key To Search Key
int result=binarySearch(array,5,key);
if(result==1)
cout<<" Key Found in Array "<<endl;
else
cout<<" Key NOT Found in Array "<<endl;
return 0;
}
void bubbleSort(int array[], int size){
cout<<" Input array is: "<<endl;
for(int j=0; j<size; j++)
{
//Displaying Array
cout<<" Value at "<<j<<" Index: "<<array[j]<<endl;
}
cout<<endl;
// Bubble Sort Starts Here
int temp;
for(int i2=0; i2<size; i2++)
{
for(int j=0; j<size-1; j++)
{
//Swapping element in if statement
if(array[j]>array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
// Displaying Sorted array
cout<<" Sorted Array is: "<<endl;
for(int i3=0; i3<size; i3++)
{
cout<<" Value at "<<i3<<" Index: "<<array[i3]<<endl;
}
}// Sort Function Ends Here
bool binarySearch(int array[],int size, int key){
int start=1, end=size;
int mid=(start+end)/2;
while(start<=end&&array[mid]!=key){
if(array[mid]<key){
start=mid+1;
}
else{
end=mid-1;
}
mid=(start+end)/2;
}// While Loop End
if(array[mid]==key)
return true; //Returnig to main
else
return false;//Returnig to main
cout<<" ";
}// binarySearch Function Ends Here
Edit & Run
#include <iostream> #include <set> int main () { std::set<int> myset; std::set<int>::iterator it; std::pair<std::set<int>::iterator,bool> ret; // set some initial values: for (int i=1; i<=5; ++i) myset.insert(i*10); // set: 10 20 30 40 50 ret = myset.insert(20); // no new element inserted if (ret.second==false) it=ret.first; // "it" now points to element 20 myset.insert (it,25); // max efficiency inserting myset.insert (it,24); // max efficiency inserting myset.insert (it,26); // no max efficiency inserting int myints[]= {5,10,15}; // 10 already in set, not inserted myset.insert (myints,myints+3); std::cout << "myset contains:"; for (it=myset.begin(); it!=myset.end(); ++it) std::cout << ' ' << *it; std::cout << ' '; return 0; } 3)
void bubbleSort(int array[], int size);
bool binarySearch(int array[], int size,int key);
int main(){
cout<<"Enter 5 numbers randomly : "<<endl;
// Size can be change by replacing 5
int array[5]; //Declaring array
for(int i=0; i<5; i++)
{
cout<<" "; cin>>array[i]; // Initializing array
}
//Passing Arrary for Sorting
bubbleSort(array,5);
// Array has Sorted At This Point
cout<<" Enter Key To Search: ";
int key;
cin>>key;
//Passing Array, size and key To Search Key
int result=binarySearch(array,5,key);
if(result==1)
cout<<" Key Found in Array "<<endl;
else
cout<<" Key NOT Found in Array "<<endl;
return 0;
}
void bubbleSort(int array[], int size){
cout<<" Input array is: "<<endl;
for(int j=0; j<size; j++)
{
//Displaying Array
cout<<" Value at "<<j<<" Index: "<<array[j]<<endl;
}
cout<<endl;
// Bubble Sort Starts Here
int temp;
for(int i2=0; i2<size; i2++)
{
for(int j=0; j<size-1; j++)
{
//Swapping element in if statement
if(array[j]>array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
// Displaying Sorted array
cout<<" Sorted Array is: "<<endl;
for(int i3=0; i3<size; i3++)
{
cout<<" Value at "<<i3<<" Index: "<<array[i3]<<endl;
}
}// Sort Function Ends Here
bool binarySearch(int array[],int size, int key){
int start=1, end=size;
int mid=(start+end)/2;
while(start<=end&&array[mid]!=key){
if(array[mid]<key){
start=mid+1;
}
else{
end=mid-1;
}
mid=(start+end)/2;
}// While Loop End
if(array[mid]==key)
return true; //Returnig to main
else
return false;//Returnig to main
cout<<" ";
}// binarySearch Function Ends Here
Edit & Run
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.