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

Write a program that has an array of 20 integers. Ask the user to enter the valu

ID: 3625674 • Letter: W

Question


Write a program that has an array of 20 integers. Ask the user to enter the values to fill the array with. It should then prompt the user to enter a value to search four. It should call a function that uses the linear search algorithm to locate that value. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that uses the binary search algorithm to locate the same value. It should also keep count of the number of comparisons
it makes. Display these values on the screen. Sample output of the program is provided below. Note that your output may vary slightly depending on the implementation details of your algorithms.

Sample Input/Output
Enter 20 integers: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Enter an integer to search for: 15
Number of comparisons necessary in linear search: 16
Number of comparisons necessary in binary search: 7

different program

Write a program that creates two arrays of 20 integers and asks the user to enter the values to fill the arrays with (both arrays will contain the same values). It should then call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order. The function should count the number of exchanges it makes. The program should then call a function that uses the selection sort algorithm to sort the other array. It should also count the number of exchanges it makes. Display these
values on the screen.

Sample Input/Output
Enter 20 integers: 10 8 7 4 3 2 1 6 5 9 0 19 13 12 17 18 14 15 16 11
Number of exchanges necessary in bubble sort: 61
Number of exchanges necessary in selection search: 19

Explanation / Answer

Program one

----------------------------------------------

#include <iostream>

using namespace std;

int LinearSearch (int [], int);
int BinarySearch (int [], int);

int LinearSearch (int a [],int i)
    {
    int k = 0;
    for (; ((a [k] != i) && (k < 20)); k++)
        {
        }
    if (a [k] == i)
        return (k + 1);
    else
        return -1;
    }

int BinarySearch (int a [],int i)
    {
    int First = 0;
    int Last = 19;
    int Middle = 0;
    int count = 0;

    do {
        Middle = First + ((Last - First)/2);
        count++;
        if (a [Middle] > i)
            Last = Middle;
        else
            if (a [Middle] < i)
                First = Middle;
            else
                return count;
        } while (count < 20);
  
    }



void main ()
    {
    int Array [20];
    int Comp = 0;
    int Search = 0;
    cout << "Enter 20 intergers: ";
    for (int i = 0; i < 20; i++)
        {
        cin >> Array [i];
        }

    cout << "Enter an integer to search for: ";
    cin >> Search;

    Comp = LinearSearch (Array, 12);
    if (Comp > 0)
        cout << "Number of comparisons necessary in linear search: " << Comp << endl;
    else
        cout << "The function did not find the number searched for";

  
    Comp = BinarySearch (Array, 12);
    if (Comp > 0)
        cout << "Number of comparisons necessary in binary search: " << Comp << endl;
    else
        cout << "The function did not find the number searched for";
    }

-----------------------------------------------

Program two

--------------------------------------------

#include <iostream>

using namespace std;

int BubbleSort (int []);
int SelectionSort (int []);

int BubbleSort (int a [])
    {
    int temp = 0;
    int count = 0;

    for (int i = 0; i < 19; i++)
        {
        if (a [i] > a [i+1])
            {
            temp = a [i];
            a [i] = a [i+1];
            a [i+1] = temp;
            count++;
            i = -1;
            }
        else;
        }

    return count;
    }

int SelectionSort (int a [])
    {
int iPos;
int iMin;
int temp = 0;
    int count = 0;


for (iPos = 0; iPos < 20; iPos++)
{

iMin = iPos;
for (int i = iPos+1; i < 20; i++)
    {
      if (a[i] < a[iMin])
        {
          iMin = i;
        }
    }

if ( iMin != iPos )
{
    temp = a [iPos];
    a [iPos] = a [iMin];
    a [iMin] = temp;
    count++;
}
}
    return count;
    }



void main ()
    {
    int Array1 [20];
    int Array2 [20];
    int Exch = 0;
    int Search = 0;
    cout << "Enter 20 intergers: ";
    for (int i = 0; i < 20; i++)
        {
        cin >> Array1 [i];
        Array2 [i] = Array1 [i];
        }

    Exch = BubbleSort (Array1);
    cout << "Number of exchanges necessary in bubble sort: " << Exch << endl;

    Exch = SelectionSort (Array2);
    cout << "Number of exchanges necessary in selection search: " << Exch << endl;

   
    }

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