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

In C++ program. ***IMPORTANT***: Please use Class structres, Inheritance, and Co

ID: 3591724 • Letter: I

Question

In C++ program. ***IMPORTANT***: Please use Class structres, Inheritance, and Constructors. because the code will be mark based on these.

Here is the sample run:

1. that sorts a list of natural numbers using the bubble sort algorithm described in project 4, page 557, chapter 9, sixth edition (or project 4, page 483, chapter 9, fifth edition); 2. and then looks for a particular number using the binary search algorithm defined in project 12, page 560, chapter 9, sixth edition (or project 12, page 486, chap ter 9, fifth edition).

Explanation / Answer

#include<iostream>

using namespace std;

class Sort{

   protected:

     int *data;
     int size;

   public:
    Sort(int n){
       data = new int[n];
       size = 0;
   }
   void add(int a){
       data[size] = a;
       size++;
   }
    void sort(){

       int temp;
       for (int i = 0; i<size; i++){
          for (int j = 0; j<size-1; j++){
              if (data[j+1] < data[j]){
                  temp = data[j];
                  data[j] = data[j+1];
                  data[j+1] = temp;
              }
          }
       }
   }
   void disp(){
     for (int i = 0; i<size; i++){
        cout << data[i] << " ";
     }
      cout << endl;
   }
};

class SortAndSearch : public Sort {
     public:
        SortAndSearch (int n) : Sort(n){}
        void search(int a){
        string ch;
         int low = 0;
         int high = size -1;
         int mid = 0;
         while (high >= low){
            disp();
           
            mid = (low + high)/2;
           
            if (data[mid] == a){
              cout << "Number " <<a << " is found at position " << mid << endl;
              return;
            }
            if (a > data[mid])
                low = mid + 1;
            if (a < data[mid])
               high = mid - 1;
           
           
            cout << high << " " << low << endl;
           
         }
         cout << "Number not found ";
       
     }
       
};

int main(){

   int n,a ;
   cout << "Enter the number of naturals :";
   cin >> n;
   SortAndSearch *s = new SortAndSearch(n);
   for (int i=0; i<n; i++){
       cin >> a;
       s->add(a);
   }
   s->sort();
   cout << "Number sorted in ascending order: ";
   s->disp();
cout << "Enter the number to search :";
cin >> a;
   s->search(a);

   return 0;
}