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

write a program create a vector with random numbers. Use merge sortto reorder th

ID: 3671782 • Letter: W

Question

write a program create a vector with random numbers. Use merge sortto reorder the vector. Prompt user to enter a number to search in the vector. If there are more than one number in the vector equal to the search value, display the indices remove the repeated numbers. If found just one matching number, display the index. If no matching number, prompt user for 2 options: add to the vector or replace a value in the vector. Write your own functions for the program. I can only use the member function of the vector. Display the vector at every change. Do not use any global variable. Pass the vector and variables to the function appropriately.

Explanation / Answer

//

// File:   VectorMerg.h

// Author: HARE KRISHNA

//

// Created on 26 February, 2016, 8:21 AM

//

#ifndef _VECTORMERG_H

#define                _VECTORMERG_H

#include <iostream>

#include <vector>

using namespace std;

void print(vector<int> v)

{

                for(int i = 0; i < v.size(); i++) cout << v[i] << " ";

                cout << endl;

}

vector<int> merge(vector<int> left, vector<int> right)

{

   vector<int> result;

   while ((int)left.size() > 0 || (int)right.size() > 0) {

      if ((int)left.size() > 0 && (int)right.size() > 0) {

         if ((int)left.front() <= (int)right.front()) {

            result.push_back((int)left.front());

            left.erase(left.begin());

         }

                else {

            result.push_back((int)right.front());

          right.erase(right.begin());

         }

      } else if ((int)left.size() > 0) {

            for (int i = 0; i < (int)left.size(); i++)

               result.push_back(left[i]);

            break;

      } else if ((int)right.size() > 0) {

            for (int i = 0; i < (int)right.size(); i++)

               result.push_back(right[i]);

            break;

      }

   }

   return result;

}

vector<int> mergeSort(vector<int> m)

{

   if (m.size() <= 1)

      return m;

   vector<int> left, right, result;

   int middle = ((int)m.size()+ 1) / 2;

   for (int i = 0; i < middle; i++) {

      left.push_back(m[i]);

   }

   for (int i = middle; i < (int)m.size(); i++) {

      right.push_back(m[i]);

   }

   left = mergeSort(left);

   right = mergeSort(right);

   result = merge(left, right);

   return result;

}

vector<int>vectorSearch(void)

{

int mfy_value;

cout<<" Enter Value is to be search:-";

cin>>mfy_value;

int search=0;

for(int i=0;search<s;i++)

{

    if(mfy_value==d[i])

       {

      cout<<"search value found";

      }

   search=i+1;

   cout<<"index of search also found here"<<endl;

}

}

int main()

{

   vector<int> v;

   v.push_back(38);

   v.push_back(27);

   v.push_back(43);

   v.push_back(3);

   v.push_back(9);

   v.push_back(82);

   v.push_back(10);

   print(v);

   cout << "------------------" << endl;

   v = mergeSort(v);

   print(v);

   v.vectorSearch()

}

#endif   /* _VECTORMERG_H */