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

Using this code //TO WRITE A PROGRAM TO SEARCH FOR A GIVEN ELEMENT FROM AN ARRAY

ID: 3862409 • Letter: U

Question

Using this code

//TO WRITE A PROGRAM TO SEARCH FOR A GIVEN ELEMENT FROM AN ARRAY USING BINARY
//SEARCH TECHNIQUE

#include <iostream>
#include <conio.h>
using namespace std;


void binsearch(int ar[], int size, int ele)
{
   int lb = 0, ub = size - 1, mid; //lb=>lower bound,ub=>upper bound

   for (; lb<ub;)
   {
       mid = (lb + ub) / 2;

       if (ar[mid] == ele)
       {
           cout << " SEARCH SUCCESSFUL";
           break;
       }

       else
           if (ar[mid] < ele)
           {
               ub = mid - 1;
           }
           else
               if (ar[mid] > ele)
               {
                   lb = mid + 1;
               }
   }

   if (ub<lb)

       cout << " SEARCH UNSUCCESSFUL";

}

void sort(int ar[], int size) //sorts the array in ascending array using bubble sort
{
   int temp;

   for (int i = 0; i<size; i++)
       for (int j = 0; j<size - i - 1; j++)
           if (ar[j]>ar[j + 1])
           {
               temp = ar[j];
               ar[j] = ar[j + 1];
               ar[j + 1] = temp;

           }

}

void display(int ar[], int size)
{
   for (int i = 0; i<size; i++)
       cout << ' ' << ar[i];
}

void input(int ar[], int size)
{
   for (int i = 0; i<size; i++)
       cin >> ar[i];
}

void main()
{

   int size;
   cout << " ENTER THE NUMBER OF ELEMENTS REQUIRED IN THE ARRAY :";
   cin >> size;

   int *ar = new int(size);

   cout << " ENTER THE ELEMENTS OF THE ARRAY : ";

   input(ar, size); //takes the input from the array

   sort(ar, size); //sorts the array in the ascending order

   int ele;
   cout << " ENTER THE ELEMENT TO BE FOUND : ";
   cin >> ele;

  

}

-Modify your program so that the data is entered from a file rather than from the keyboard. The first line of the file should be the size of the integer array. The second line should contain the integer searched for in the data set. Finally, the array elements are to start on the third line. Make sure you separate each array element with a space. The output, as described in should be sent to a file. Submit all files.

Explanation / Answer

#include <iostream>
#include<fstream>
using namespace std;

void binsearch(int ar[], int size, int ele)
{
  
int lb = 0, ub = size - 1, mid; //lb=>lower bound,ub=>upper bound
for (; lb<ub;)
{
  
mid = (lb + ub) / 2;
if (ar[mid] == ele)
{
cout << " SEARCH SUCCESSFUL ";
break;
}
else
if (ar[mid] > ele)
{
ub = mid - 1;
}
else
if (ar[mid] < ele)
{
lb = mid + 1;
}
}
if (ub<lb)
cout << " SEARCH UNSUCCESSFUL ";
}
void sort(int ar[], int size) //sorts the array in ascending array using bubble sort
{
int temp;
for (int i = 0; i<size; i++)
for (int j = 0; j<size - i - 1; j++)
if (ar[j]>ar[j + 1])
{
temp = ar[j];
ar[j] = ar[j + 1];
ar[j + 1] = temp;
}
}
void display(int ar[], int size)
{
for (int i = 0; i<size; i++)
cout << ' ' << ar[i];
}
void input(int ar[], int size)
{
for (int i = 0; i<size; i++)
cin >> ar[i];
}
int main()
{
int size;
ifstream inputFile;
inputFile.open("numbers.txt");
int n;
int ele;
int *ar;
if (inputFile.is_open()) {
inputFile >> n;
size = n;

ar = new int(size);
inputFile >> n;
ele = n;
int i=0;
while (!inputFile.eof()) {


inputFile >> n;
ar[i] = n;
i++;

}
}


// input(ar, size); //takes the input from the array
sort(ar, size); //sorts the array in the ascending order
cout<<"Array elements after sorting: "<<endl;
for(int i=0; i<size; i++){
cout<<ar[i]<<" ";
}
cout<<endl;
binsearch(ar, size, ele);
inputFile.close();
return 0;
}

Output:

g++ -o main *.cpp                                                                                                                                                                                                                              

sh-4.2$ main                                                                                                                                                                                                                                                             

Array elements after sorting:                                                                                                                                                                                                                                            

1 2 3 4 5 6 7 8 9 10                                                                                                                                                                                                                                                     

                                                                                                                                                                                                                                                                         

SEARCH SUCCESSFUL