Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MUST BE WRITTEN IN C++ MUST HAVE ALL THE FILES MUST WORK 1) Write an object-orie

ID: 657167 • Letter: M

Question

MUST BE WRITTEN IN C++

MUST HAVE ALL THE FILES

MUST WORK

1) Write an object-oriented C++ program (i.e. a class and a main function to use it), using pointer variables, that program that performs specific searching and sorting exercises of an array of integers.

This program has six required outputs.

Start by initializing an array with the following integers, in this order:

23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49.

Your array input may be hardcoded in your program

After initializing your array in main, create a class instance and pass the array to the instance where the unsorted values are displayed. This is required output #1 of 6 for this program. Be sure to label it as such in your program output.

Using a linear, or sequential, search of the unsorted array, determine and report the 1-relative (i.e. 1st, 2nd, 3rd, 4th, etc.) positions of the following numbers in the array (or

Explanation / Answer

#include <iostream>
using namespace std;

class SearchAndSort
{
int array[50];
int n;
public:
   SearchAndSort(int* p,int nn);      
   LinearSearch(int* p,int nn, int item);
   BinarySearch(int* p,int nn, int item);
};

SearchAndSort :: SearchAndSort(int* p,int nn)
{
n=nn;
       cout<<"Output #1 of 6"<<endl<<"Unsorted elements ";
       for(int i=0;i<n;i++)
       {
           array[i]=*(p+i);
           cout<<array[i]<<" ";
       }
}

int SearchAndSort:: LinearSearch(int* p,int nn,int item,int & d)
{
   for(int count=0;count<nn;count++)
{
   d++;
if(element==*(p+count))
{
return count;
}
}

return -1;
}

void SearchAndSort::BubbleSort(int* p,int nn)
{
   int i, j, flag = 1; // set flag to 1 to start first pass
int temp; // holding variable

for(i = 1; (i <= nn) && flag; i++)
{
flag = 0;
for (j=0; j < (nn -1); j++)
{
if (*(p+j+1) > (*p+j)) // ascending order simply changes to <
{
temp = (*p+j); // swap elements
(*p+j) = (*p+j+1);
(*p+j) = temp;
flag = 1; // indicates that a swap occurred.
}
}
}
return;
}
int SearchAndSort::BinarySearch(int* p,int nn, int item, int & d)
{
   int Mid,Lbound=0,Ubound=nn-1;
   int f=0;

   while(Lbound<=Ubound)
   {
       d++;
       Mid=(Lbound+Ubound)/2;
       if(item>*(p+Mid))
           Lbound=Mid+1;
       else if(item<*(p+Mid))
           Ubound=Mid-1;
       else
       {
           f=1;
           return 1;
       }
          
   }

   return -1;
}
}
int main()
{
int numbers[]={23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, 49};
int* ptr=numbers;
int item1=25,item2= 30,item3= 50,item4=75,item5=92;

int nn=sizeof(numbers);
SearchAndSort ss=new SearchAndSort(ptr,nn);

cout<<endl<<"Output #2 of 6";
int c1=0,c2=0,c3=0,c4=0,c5=0;
int pos1=ss.LinearSearch(ptr, nn,item1,&c1);
cout<<"no.of searches:"<<c1;
if(pos1!=-1)
cout<<item1<<" is found at position:"<<pos1;
else
cout<<item1<<" is not found";
int pos2=ss.LinearSearch(ptr, nn,item2,&c2);
cout<<"no.of searches:"<<c2;
if(pos2!=-1)
cout<<item2<<" is found at position:"<<pos2;
else
cout<<item2<<" is not found";
int pos3=ss.LinearSearch(ptr, nn,item3,&c3);
cout<<"no.of searches:"<<c3;
if(pos3!=-1)
cout<<item3<<" is found at position:"<<pos3;
else
cout<<item3<<" is not found";
int pos4=ss.LinearSearch(ptr, nn,item4,&c4);
cout<<"no.of searches:"<<c4;
if(pos4!=-1)
cout<<item4<<" is found at position:"<<pos4;
else
cout<<item4<<" is not found";
int pos5=ss.LinearSearch(ptr, nn,item5,&c5);
if(pos5!=-1)
cout<<item5<<" is found at position:"<<pos5;
cout<<"no.of searches:"<<c5;
else
cout<<item5<<" is not found";

cout<<endl<<"Output #3 of 6";
cout<<"total number of Searches linear search:"<<c1+c2+c3+c4+c5;

cout<<endl<<"Output #4 of 6";
ss.BubbleSort(ptr,nn);
for(int i=0;i<n;i++)
       {
           cout<<*(ptr+i)<<" ";
       }


cout<<endl<<"Output #5 of 6";
c1=0,c2=0,c3=0,c4=0,c5=0;
pos1=ss.BinarySearch(ptr, nn ,item1,&c1);
cout<<"no.of searches:"<<c1;
if(pos1!=-1)
cout<<item1<<" is found at position:"<<pos1;
else
cout<<item1<<" is not found";
pos2=ss.BinarySearch(ptr, nn,item2,&c2);
cout<<"no.of searches:"<<c2;
if(pos2!=-1)
cout<<item2<<" is found at position:"<<pos2;
else
cout<<item2<<" is not found";
pos3=ss.BinarySearch(ptr, nn,item3,&c3);
cout<<"no.of searches:"<<c3;
if(pos3!=-1)
cout<<item3<<" is found at position:"<<pos3;
else
cout<<item3<<" is not found";
pos4=ss.BinarySearch(ptr, nn,item4,&c4);
cout<<"no.of searches:"<<c4;
if(pos4!=-1)
cout<<item4<<" is found at position:"<<pos4;
else
cout<<item4<<" is not found";
pos5=ss.BinarySearch(ptr, nn,item5,&c5);
if(pos5!=-1)
cout<<item5<<" is found at position:"<<pos5;
cout<<"no.of searches:"<<c5;
else
cout<<item5<<" is not found";

cout<<endl<<"Output #6 of 6";
cout<<"total number of Searches Binary search:"<<c1+c2+c3+c4+c5;

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote