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

C++ Use the sorted array that you get in Step 2 from question 1 (you can re-use

ID: 3770296 • Letter: C

Question

C++

Use the sorted array that you get in Step 2 from question 1 (you can re-use the relevant portions of previous code to get the sorted array),

1. Print the elements of the array. (same as step 3 from previous question)

2. Use Binary Search function to find a number (passed as one of the parameters), constituted of last 3 digits of your UIN. You have to hard code this inside your code. For example: If your UIN is ******123 -> Search for number 123 a. Print the number (Eg: 123) b. Report if you could find the number (Yes or No) and the array index

3. Perform Binary Search and report, whether or not the user defined value N (positive or negative) is present in the array. Also, if it is present, then print the array index where it was found. Note that, array consists of only positive integers in a specified range as in Program 2. So, even if N given by user is a negative integer, the code should ignore the sign and search for the positive part. Eg: if N = -5, the program should search for 5 in the array.

Explanation / Answer

Please see comments in program in order to execute the program.

<code>

#include<bits/stdc++.h>
using namespace std;
#define UIN 123 //Replace your UIN number here
void print(int array[],int array_size){ //This function will print sorted array
   for(int i = 0;i < array_size;i++){
       cout<<array[i]<<" ";
   }
   cout<<endl;
}
int search(int array[],int left,int right,int number){ //binary search it will return -1 if number is not found otherwise index of number
   int mid;

   if(left > right){
   return -1;
   }
    mid = (left + right) / 2;

    if (array[mid] == number) {
        return mid;
   } else if (number < array[mid]) {
        return search(array, left, mid-1, number);
   } else {
       return    search(array, mid+1, right, number);
    }
}
int binarySearch(int array[],int number,int array_size){ //utility function for binary search
   if (number < 0){
       number == abs(number); //Ignoring negative sign
   }
   return search(array,0,array_size-1,number);
}
int main()
{
   int array[] = {1,2,3,4,5,6,78}; // replace it with your sorted array
   int array_size = sizeof(array)/sizeof(array[0]);
   int uin_search = binarySearch(array,UIN,array_size); // searching your UIN number
   if (uin_search > -1){
       cout<<"YES"<<endl;
   }else{
       cout<<"NO"<<endl;
   }
   int number;
   cout<<"Enter for search Number:";
   cin >> number;
   int num_search = binarySearch(array,number,array_size); // searching user entered number
   if (num_search > -1){
       cout<<"Array index:"<<num_search<<endl;
   }else{
       cout<<"No ";
   }
   return 0;
}

</code>

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