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

Write a program to fill a vector with 5000 random numbers. Then display: . The s

ID: 3919038 • Letter: W

Question

Write a program to fill a vector with 5000 random numbers. Then display: . The smallest number The largest number . The number of odd values . The number of even values . The total of the values . The average of the values . Ask the user for an integer and display the subscript of the cell where it was found or NOT FOUND! if it isn't found. You will be using a binary search to locate the index of the integer requested by the user. Since you will be using a binary search, the vector will have to be in order. Under "Materials" in this week's module, there is a file that contains two functions that you will need to include in your code to sort your vector shellSort() and swapper). You can download the file, "Activity 25 Sort Functions.cpp," directly, of click the following link for a .zip file that contain:s t: Download and unzip the shellSort and swapper function Each of the preceding values should be determined by a function and returned to main where thev

Explanation / Answer

#include<iostream>
#include<vector>
#include<stdlib.h>
#include<time.h>
#include<iomanip>

using namespace std;


int binarySearch(vector<int> arr, int l, int r, int x)
{
   if (r >= l)
   {
        int mid = l + (r - l)/2;
        if (arr[mid] == x)
            return mid;

        if (arr[mid] > x)
            return binarySearch(arr, l, mid-1, x);

        return binarySearch(arr, mid+1, r, x);
   }
   return -1;
}

int findmax(vector<int> data){
    int max = 0;
    for (int i = 0; i<data.size(); i++){
        if (data[i] > max)
           max = data[i];
    }
    return max;
}

int findmin(vector<int> data){
    int min = data[0];
    for (int i = 0; i<data.size(); i++){
        if (data[i] < min)
           min = data[i];
    }
    return min;
}

double average(vector<int> data){
    int sum = 0;
    for (int i = 0; i<data.size(); i++){
        sum = sum + data[i];
    }
    return sum/data.size();
}

int total(vector<int> data){
    int sum = 0;
    for (int i = 0; i<data.size(); i++){
        sum = sum + data[i];
    }
    return sum;
}

int counteven(vector<int> data){
    int count = 0;
    for (int i = 0; i<data.size(); i++){
        if (data[i] % 2 == 0)
           count++;
    }
    return count;
}

int countodd(vector<int> data){
    int count = 0;
    for (int i = 0; i<data.size(); i++){
        if (data[i] % 2 != 0)
           count++;
    }
    return count;
}

int shellsort(vector<int> arr, int n)
{

    for (int gap = n/2; gap > 0; gap /= 2)
    {
         for (int i = gap; i < n; i += 1)
        {
            int temp = arr[i];
            int j;           
            for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
                arr[j] = arr[j - gap];
            arr[j] = temp;
        }
    }
    return 0;
}


int main(void)
{
   vector<int> arr;
   srand(time(NULL));
   for (int i = 0; i<5000; i++){
       int a = rand() / 100000;
       arr.push_back(a);
   }
   int sum = total(arr);
   int nodd = countodd(arr);
   int neven = counteven(arr);
   int max = findmax(arr);
   int min = findmin(arr);
   shellsort(arr,arr.size());


   double avg = average(arr);
   cout << "The smallest number is " << min << endl;
   cout << "The largest number is " << max << endl;
   cout << "The number of odd values is " << nodd << endl;
   cout << "The number of even values is " << neven << endl;
   cout << "The total of values is " << sum << endl;
   cout << "The average of the values is " << fixed << setprecision(2) << avg << endl;
   cout << "Enter an integer:";
   int n;
   cin >> n;
   int result = binarySearch(arr, 0, 4999, n);
   if (result == -1)
       cout << "Element is not present in vector ";
   else
       cout << "Element is present at index " << result << endl;
   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