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

#include <iostream> using namespace std; void mover(int[],int[]); void bubble_so

ID: 3641507 • Letter: #

Question

#include <iostream> using namespace std;  void mover(int[],int[]); void bubble_sort(int []); void output_list(int [],int []); int linear_search(int[],int); void explanation();  int main() {         int a[]={98,40,44,50,45,90,10,11,77,12};         int b[10];         int value,location;          explanation();          mover(a,b);          bubble_sort(b);                  output_list(a,b);          cout<<"What value would you like to search for?"<<endl;         cin>>value;          location=linear_search(b,value);          cout<<"The search value: "<<value;                  if(location == -1)                 cout<<" was not located in the list";         else                 cout<<" was located in position "<<location<<" of the array";                          cout<<endl<<endl;          return 0; }  void explanation() {         cout<<endl;         cout<<"This program sorts a list of 10 numbers that have been 'hard coded'"<<endl;         cout<<"into the program. The user will then be prompted to enter a value to search"<<endl;         cout<<"the list for. The position the value was found in will be printed "<<endl;         cout<<"(adjusted to be 1 - 10) or if the number is not found,"<<endl;         cout<<"an appropriate message will be printed"<<endl<<endl<<endl; }   int linear_search(int z[],int v) {         int i=0;          while(i<=9 && z[i]!=v)                 i++;          if(z[i]==v)                 return i+1;         else                 return -1; }  void bubble_sort(int z[]) {         int flag=1,i,temp,pass=0;          while(flag)         {                 flag = 0;                 for(i=0;i<=8-pass;i++)                 {                         if(z[i]>z[i+1])                         {                                 temp = z[i];                                 z[i] = z[i+1];                                 z[i+1] = temp;                                 flag = 1;                         }                 }                 pass++;         } }   void mover(int x[],int y[]) {         int i;          for(i=0;i<=9;i++)         {                 y[i]=x[i];         } }  void output_list(int x[],int y[]) {         int i;          cout<<"Original    Sorted"<<endl;         cout<<"-------------------"<<endl;           for(i=0;i<=9;i++)         {                 cout<<"   "<<x[i]<<"         "<<y[i]<<endl;         }          cout<<"-------------------"<<endl<<endl;; }  

Explanation / Answer

#include <iostream>
using namespace std;

void mover(int[],int[]);
void bubble_sort(int []);
void output_list(int [],int []);
int linear_search(int[],int);
int binary_search(int[],int,int,int);
void explanation();

int main()
{
    int a[]={98,40,44,50,45,90,10,11,77,12};
    int b[10];
    int value,location;

    explanation();

    mover(a,b);

    bubble_sort(b);
   
    output_list(a,b);

    cout<<"What value would you like to search for?"<<endl;
    cin>>value;

    //location=linear_search(b,value);
    location = binary_search(b,value,0,9);
    cout<<"The search value: "<<value;
   
    if(location == -1)
        cout<<" was not located in the list";
    else
        cout<<" was located in position "<<location<<" of the array";
       
    cout<<endl<<endl;

    return 0;
}

void explanation()
{
    cout<<endl;
    cout<<"This program sorts a list of 10 numbers that have been 'hard coded'"<<endl;
    cout<<"into the program. The user will then be prompted to enter a value to search"<<endl;
    cout<<"the list for. The position the value was found in will be printed "<<endl;
    cout<<"(adjusted to be 1 - 10) or if the number is not found,"<<endl;
    cout<<"an appropriate message will be printed"<<endl<<endl<<endl;
}


int linear_search(int z[],int v)
{
    int i=0;

    while(i<=9 && z[i]!=v)
        i++;

    if(z[i]==v)
        return i+1;
    else
        return -1;
}

int binary_search(int A[], int key, int imin, int imax)
{
// continue searching while [imin,imax] is not empty
while (imax >= imin)
    {
      /* calculate the midpoint for roughly equal partition */
      int imid = (imin + imax) / 2;

      // determine which subarray to search
      if      (A[imid] < key)
        // change min index to search upper subarray
        imin = imid + 1;
      else if (A[imid] > key )
        // change max index to search lower subarray
        imax = imid - 1;
      else
        // key found at index imid
        return imid+1; // return imid+1 so elements start at 1 vs 0
}
// key not found
return -1;
}

void bubble_sort(int z[])
{
    int flag=1,i,temp,pass=0;

    while(flag)
    {
        flag = 0;
        for(i=0;i<=8-pass;i++)
        {
            if(z[i]>z[i+1])
            {
                temp = z[i];
                z[i] = z[i+1];
                z[i+1] = temp;
                flag = 1;
            }
        }
        pass++;
    }
}


void mover(int x[],int y[])
{
    int i;

    for(i=0;i<=9;i++)
    {
        y[i]=x[i];
    }
}

void output_list(int x[],int y[])
{
    int i;

    cout<<"Original    Sorted"<<endl;
    cout<<"-------------------"<<endl;


    for(i=0;i<=9;i++)
    {
        cout<<"   "<<x[i]<<"         "<<y[i]<<endl;
    }

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