Question

Add binary_search() (page 462) to your program. Modify main() to prompt the user for a number...

Add binary_search() (page 462) to your program. Modify main() to prompt the user for a number to search (until ^D) and display the position of the number in the sorted vector. Try your program for the following user input:

1
15
18
40
30
50
^D 

The output should be:

-1
2
-1
7
5
-1 

int binary_search(vector<int> v, int from, int to, int value)
{
if (from > to)
return -1;
int mid = (from + to) / 2;
if (v[mid] == value)
return mid;
else if (v[mid] < value)
return binary_search(v, mid + 1, to, value);
else
return binary_search(v, from, mid - 1, value);
}

#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
//selection sort function prototype
void selectionSort(vector<int>&v);
//Main method
int main()
{
   //file read object
   ifstream file;
   //vector declaration
   vector<int>numbers;
   //name of the filr given the object to open
   file.open("sort.txt");
   //file is not found error
   if (!file) {
       cout << "File not found" << endl;
       return 0;
   }
   //Other wise read file data and store in a vector
   else {
       while (!file.eof()) {
           int line;
           if (file >> line){
               numbers.push_back(line);
           }

       }
       //Vector values before sort
       cout << "Before sorting:" << endl;
       for (int i = 0; i < numbers.size(); i++) {
           cout << numbers[i] << " ";
       }

       cout << endl;
   }
   //call sort function
   selectionSort(numbers);
   cout << "After sorting:" << endl;
   //Vector values after sort
   for (int i = 0; i < numbers.size(); i++) {
       cout << numbers[i] << " ";
   }

   cout << endl;

    return 0;
}
//Selection sort implementation
void selectionSort(vector<int> &v) {
   int vecsize = v.size();
   for (int i = 0; i < vecsize-1; i++)
       for (int j = i + 1; j < vecsize; j++)
           if (v[i] > v[j])
               swap(v[i], v[j]);
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PLASE GIVE THUMBS UP, THANKS, FOR ANY QUERY LEAVE COMMENT.

AS YOUR sort.txt file is not given in question so i can't execute according to your input because it will not give that exact location.

i create my own list and input. you can check on your given sort.txt it will work 100% smooth

SAMPLE OUTPUT:

CODE :

#include<iostream>
#include<fstream>
#include<vector>
#include <cstdlib>
using namespace std;
//selection sort function prototype
void selectionSort(vector<int>&v);
int binary_search(vector<int> v, int from, int to, int value);
//Main method
int main()
{
//file read object
ifstream file;
//vector declaration
vector<int>numbers;
//name of the filr given the object to open
file.open("sort.txt");
//file is not found error
if (!file) {
cout << "File not found" << endl;
return 0;
}
//Other wise read file data and store in a vector
else {
while (!file.eof()) {
int line;
if (file >> line){
numbers.push_back(line);
}
}
//Vector values before sort
cout << "Before sorting:" << endl;
for (int i = 0; i < numbers.size(); i++) {
cout << numbers[i] << " ";
}
cout << endl;
}
//call sort function
selectionSort(numbers);
cout << "After sorting:" << endl;
//Vector values after sort
for (int i = 0; i < numbers.size(); i++) {
cout << numbers[i] << " ";
}
cout << endl;
int value;
while(1)
{
    cout<<"Enter number to search : ";
    if(cin>>value){
   }
    else
    break;
    cout<<binary_search(numbers,0,numbers.size()-1,value)<<endl;
}

return 0;
}
//Selection sort implementation
void selectionSort(vector<int> &v) {
int vecsize = v.size();
for (int i = 0; i < vecsize-1; i++)
for (int j = i + 1; j < vecsize; j++)
if (v[i] > v[j])
swap(v[i], v[j]);
}

int binary_search(vector<int> v, int from, int to, int value)
{
if (from > to)
return -1;
int mid = (from + to) / 2;
if (v[mid] == value)
return mid;
else if (v[mid] < value)
return binary_search(v, mid + 1, to, value);
else
return binary_search(v, from, mid - 1, value);
}

Add a comment
Know the answer?
Add Answer to:
Add binary_search() (page 462) to your program. Modify main() to prompt the user for a number...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Modify this C++ below to show the selection sorting algorithm method. Then write a test program...

    Modify this C++ below to show the selection sorting algorithm method. Then write a test program and show that it works. HINT: Replace the bubbleSort function with the selectionSort. Your test program should perform the following steps: Create an array of random numbers. Display the array in its original order. Pass the array to your sorting function. Display the array again to show that it has been sorted. Provide a screen shot showing that the above steps are working. CODE:...

  • Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative perfo...

    Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative performance of the algorithms for a variety of data sets. Need Help With this Sorting Algorithm task for C++ Base Code for sorting.cpp is given. The header file is not included in this. Help would be much appreciated as I have not started on this due to personal reasons #include <cstdlib> #include <iostream> #include <getopt.h> using namespace std; long compares; // for counting...

  • In this lab we are going to complete a profile of two sorting algorithms by running...

    In this lab we are going to complete a profile of two sorting algorithms by running some tests to collect empirical data. 1. First we need to be able to generate some random integers. You can do this by including the following library : #include Now first run the following to generate a seed : srand (time(NULL)) You can then generate a random number using the function rand() 2. We will use two sort algorithms - Selection Sort and Bubble...

  • /* Implementation of the main() method of the program. */ #include <iostream> #include <string> #include <vector>...

    /* Implementation of the main() method of the program. */ #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; class Student { public:    // default constructor    Student()    {        studentID = 0;        studentFirst = "First";        studentMiddle = "Middle";        studentLast = "Last";    }    void SetStudentID(int number) { studentID = number; }    void SetStudentFirst(string name) { studentFirst = name; }    void SetStudentMiddle(string name) { studentMiddle =...

  • Update your first program to dynamically allocate the item ID and GPA arrays. The number of...

    Update your first program to dynamically allocate the item ID and GPA arrays. The number of items will be the first number in the updated “student2.txt” data file. A sample file is shown below: 3 1827356 3.75 9271837 2.93 3829174 3.14 Your program should read the first number in the file, then dynamically allocate the arrays, then read the data from the file and process it as before. You’ll need to define the array pointers in main and pass them...

  • Am I getting this error because i declared 'n' as an int, and then asking it...

    Am I getting this error because i declared 'n' as an int, and then asking it to make it a double? This is the coude: #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <vector> using namespace std; void sort(double grades[], int size); char calGrade(double); int main() {    int n;    double avg, sum = 0;;    string in_file, out_file;    cout << "Please enter the name of the input file: ";    cin >> in_file;   ...

  • Selection sort is often the first sorting algorithm covered in introductory computer science courses. Java code...

    Selection sort is often the first sorting algorithm covered in introductory computer science courses. Java code that uses selection sort to place the elements of an integer array into non-decreasing order is shown here: public void swapNumbers(int i, int j) { ​int temp = numbers[i];​ /* put numbers[i] somewhere to keep it safe */ ​numbers[i] = numbers[j]; /* put numbers[j] at index i */ ​numbers[j] = temp;​ /* put numbers[i] at index j */ } public void selectionSort(int[] numbers) {...

  • can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define...

    can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define SIZE 100 using namespace std; //declare struct struct word_block {    std::string word;    int count; }; int getIndex(word_block arr[], int n, string s); int main(int argc, char **argv) {    string filename="input.txt";    //declare array of struct word_block    word_block arr[SIZE];    int count = 0;    if (argc < 2)    {        cout << "Usage: " << argv[0] << "...

  • So. I'm sick and I need help figuring out whatever is going on with this. #include...

    So. I'm sick and I need help figuring out whatever is going on with this. #include using namespace std; //prototypes int getExchangesInBubbleSort(int[], int); int getExchangesInSelectionSort(int[], int); int main() {    char choice;       cout << "1. Search Benchmarks\n";    cout << "2. Sorting Benchmarks\n";    do    {        int input;        cout << "Please enter the program you would like to run. \n";        cin >> input;               if (input == 1)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT