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

The instructions to the original program are listed below and this is the instru

ID: 3604829 • Letter: T

Question

The instructions to the original program are listed below and this is the instructions to the new assignment Modify the sequential search and binary search programs that we did in class so that they use pointer notation to access array elements. Do not use [ ] to retrieve an element of the array. Use * and / or & instead.

1. Prepare a file called unsortedNumbers.txt containing 10 numbers.

2. Your program should read the 10 numbers from the file unsortedNumbers.txt into an

array.

3. Sort the array using either bubble sort or selection sort.

4. Save the sorted array in a file called sortedNumbers.txt

5. Display on the screen the values of the minimum and maximum found in the file.

Since the array has already been sorted, you can select the minimum and maximum values

without going through the entire array.

6. Ask the user to enter a number. Use binary search, extended to the left and right, to find the

number of times that number appears in the sorted array. This is explained in more detail below.

Explanation / Answer

#include<iostream>
#include<fstream>

using namespace std;

void binsearch(int arr[], int x)
{
int l = 0;
int r = 9;
int count = 0;
while (l <= r)
{
    int m = l + (r-l)/2;
    if (arr[m] == x)
        count++;
    if (arr[m] < x)
        l = m + 1;
    else
         r = m - 1;
}
cout << x << " appeared " << x << " times ";
}

int main(){

    ifstream fin("unsortedNumbers.txt");
    ofstream fout("sortedNumbers.txt");
    int data[10];
    int temp;
    int n;

    if (!fin) {
        cout << "Error in opening file ";
        return 0;
    }
    for (int i = 0; i<10; i++){
       fin >> data[i];
    }
    fin.close();
    for (int i = 0; i<10; i++){
       for(int j=i+1; j<10; j++){
          if (data[i] > data[j]){
            temp = data[i];
            data[i] = data[j];
            data[j] = temp;
          }
       }
    }
    for (int i = 0; i<10; i++){
       fout << data[i] << " ";
    }   
    fout << endl;
    fout.close();
    cout << "Minimum :" << data[0] << endl;
    cout << "Maximum :" << data[9] << endl;

    cout << "Enter the number to search : " ;
    cin >> n;
    binsearch(data,n);
    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