Write a c program to satisfy the following 1 Write a program

Write a c++ program to satisfy the following:

1. Write a program that will read each line from A3.txt.
2. Read the number in the first line, and search that number in the line next to it.
3. Use Binary search algorithm (using recursion) for searching.
4. So, each number in the odd lines(line 1, line 3, line 5, etc) will be searched in the even lines (line 2, line 4, line 6, etc) next to it respectively.
5. Show the positions where you found that number. If a number appears multiple times, show the last position in the input list.
6. Save your results in B3.txt.
Note: In C++, array index starts from 0 but the input file positions start from 1.

A1.txt:

Sample Input:

41

0 24 27 34 41 45 50 58 61 62 64 67 69 78 81

91

16 18 20 21 27 36 40 42 53 82 91 91 92 95 95

27

11 12 22 26 31 33 35 38 47 67 69 71 73 94 99

Sample Output:

5

12

Not Found

Solution

// C++ code open file and find number using binary search and output to the other file

#include <iostream>
#include <string.h>
#include <fstream>
#include <limits.h>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <stdlib.h>
#include <vector>

using namespace std;

int binarySearch(int array[], int l, int r, int number)
{
   if (r >= l)
   {
        int mid = l + (r - l)/2;

        if (array[mid] == number) return mid;

      
        if (array[mid] > number) return binarySearch(array, l, mid-1, number);

        return binarySearch(array, mid+1, r, number);
   }

   return -1;
}


int main ()
{
int position;
int number;
int array[16];

ofstream outFile (\"B3.txt\");
ifstream inputFile (\"A3.txt\");


if (inputFile.is_open())
{
    while ( true )
    {
      if(inputFile.eof())
        break;

      inputFile >> number;
      for (int i = 1; i < 16; ++i)
      {
         inputFile >> array[i];
      }

      position = binarySearch(array, 1, 15, number);
      if(position == -1)
        outFile << \"Not found\ \";
      else
        outFile << position << endl;
    }
    inputFile.close();
}

else cout << \"Unable to open file\";

outFile.close();

return 0;
}

/*
A3.txt
41
0 24 27 34 41 45 50 58 61 62 64 67 69 78 81
91
16 18 20 21 27 36 40 42 53 82 91 91 92 95 95
27
11 12 22 26 31 33 35 38 47 67 69 71 73 94 99
41
11 23 29 37 41 41 44 47 53 57 59 62 64 68 78
64
5 6 16 29 35 40 42 42 46 48 64 70 88 90 90
98
1 23 29 31 40 48 50 54 56 66 76 81 84 93 98
39
15 18 23 26 29 33 37 38 39 39 40 41 44 58 82
78
12 21 24 29 30 45 62 70 72 73 73 77 77 86 97
36
24 31 36 41 50 50 52 55 55 61 66 67 74 86 90
37
6 16 24 28 29 36 38 38 39 39 40 41 44 58 82

B3.txt
5
12
Not found
6
11
15
10
Not found
3
Not found


*/

Write a c++ program to satisfy the following: 1. Write a program that will read each line from A3.txt. 2. Read the number in the first line, and search that num
Write a c++ program to satisfy the following: 1. Write a program that will read each line from A3.txt. 2. Read the number in the first line, and search that num
Write a c++ program to satisfy the following: 1. Write a program that will read each line from A3.txt. 2. Read the number in the first line, and search that num

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site