Write in C++ only A2Output.txt is Thank You Program 4 - Binary Search - 10 pts •
ID: 3670975 • Letter: W
Question
Write in C++ only
A2Output.txt is
Thank You
Program 4 - Binary Search - 10 pts • Read the sorted integer values from the file A2Output.txt and store them into an integer array. • Write a program that will take an integer number from the user, search that number from the array using Binary search algorithm, and output the position(s) of that number. • If the user input appears multiple times in the file, report one index position. Also if the user input is not available in the file, generate an error message "Number is not available! " Bonus: If the user input appears multiple times in the file, report all the index positions (5 points) Bonus Program - 15 points Declare three integer arrays A = {1,2,3,4}, B = {0, 2, 4, 6, 8}, and C = {3, 5, 7, 9, 11}. Write a program to compute the following (Do not use C++ templates. You have to use basic integer arrays ): 1. (An B) UC. 2. (B – C) U A. 3. (AU ByExplanation / Answer
Here is the code for program 4.
#include <iostream>
#include <fstream>
using namespace std;
int BinarySearch(int Array[], int low, int high, int key)
{
if(low <= high)
{
int mid = (low+high)/2;
if(Array[mid] == key)
return mid;
else if(key < Array[mid])
return BinarySearch(Array, low, mid-1, key);
else
return BinarySearch(Array, mid+1, high, key);
}
return -1;
}
int main()
{
int Array[50];
cout<<"Enter the name of the file: "; //Read the file name.
string fileName;
cin>>fileName;
ifstream ipFile;
ipFile.open(fileName); //Opens the file for input stream.
int count = 0;
while(!ipFile.eof()) //While the file is not empty.
ipFile>>Array[count++]; //Read the elements into the integer array.
cout<<"Enter the key to search for: "; //Read the key to search for.
int key;
cin>>key;
int pos = BinarySearch(Array, 0, count-1, key); //Call the Binary search.
if(pos == -1)
cout<<"Number is not available."<<endl;
else
cout<<"Number found at position: "<<pos<<endl;
//And to print all the positions:
cout<<"Number also found at positions: ";
while(Array[pos--] == key);
pos += 2;
while(Array[pos++] == key)
cout<<pos<<" ";
cout<<endl;
}
The logic for the Bonus program is:
#include <iostream>
#include <array>
using namespace std;
int BinarySearch(int Array[], int low, int high, int key)
{
if(low <= high)
{
int mid = (low+high)/2;
if(Array[mid] == key)
return mid;
else if(key < Array[mid])
return BinarySearch(Array, low, mid-1, key);
else
return BinarySearch(Array, mid+1, high, key);
}
return -1;
}
int main()
{
int A[] = {1, 2, 3, 4};
int B[] = {0, 2, 4, 6, 8};
int C[] = {3, 5, 7, 9, 11};
int temp[10], count = 0;
int lengthA = sizeof(A)/sizeof(A[0]);
int lengthB = sizeof(B)/sizeof(B[0]);
int lengthC = sizeof(C)/sizeof(C[0]);
for(int i = 0; i < lengthA; i++) //For each element in A.
{
int pos = BinarySearch(B, 0, lengthB-1, A[i]); //If it is also found in B.
if(pos != -1)
temp[count++] = A[i]; //Add it to the new Array temp.
}
cout<<"(A INTERSECTION B) UNION C: ";
for(int i = 0; i < lengthC; i++)
cout<<C[i]<<" ";
for(int i = 0; i < count; i++)
cout<<temp[i]<<" ";
cout<<endl;
count = 0;
for(int i = 0; i < lengthB; i++) //For each element in B.
{
int pos = BinarySearch(C, 0, lengthC-1, B[i]); //If it is not found in C.
if(pos == -1)
temp[count++] = B[i]; //Add it to the new Array temp.
}
cout<<"(B - C) UNION A: ";
for(int i = 0; i < count; i++)
cout<<temp[i]<<" ";
for(int i = 0; i < lengthA; i++)
cout<<A[i]<<" ";
cout<<endl;
count = 0;
for(int i = 0; i < lengthA; i++) //Copy all the elements in A.
temp[count++] = A[i];
for(int j = 0; j < lengthB; j++) //Copy all the elements in B.
temp[count++] = B[j];
for(int i = 0; i < lengthC; i++)
{
int pos = BinarySearch(temp, 0, count-1, C[i]); //If it is not found in temp.
if(pos == -1)
cout<<C[i]<<" ";
}
cout<<endl;
}
If you need any further refinements, just get back to me.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.