Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Need help with this. This is a previously completed assignment. The only change I need is for case 11 to be Binary Search. Right now
Need help with this. This is a previously completed assignment. The only change I need is for case 11 to be Binary Search. Right now it is Linear Search. I am struggling because I can't seem to implement it without messing up other parts of the program. The Binary Search must be HARDCODED and written in C++. Thank you.
#include#include #include #include using namespace std; int random_number(int number) { return 1 + rand() % number; } void bubblesort(int a[], int n) { for(int i = n-1; i >= 0; i--) { for(int j = 0; j < i; j++) { if(a[j] > a[j+1]) { int temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } }; int main() { srand((unsigned)time(NULL)); //Declaring variables string fileName; cout << "Enter file name: "; cin >> fileName; //Opening file ofstream myfile; myfile.open(fileName.c_str()); int array1[1000], x; for (int i = 0; i < 1000; i++) \ { x = random_number(1000); myfile << x << " "; array1[i] = x; } //Closing file myfile.close(); int option; do { cout<<"1:output all value 2:sum of all value 3:all odd numbers 4:all even numbers "; cout<<"5:output middle value 6:output first value 7:output last value 8:Highest value and its location "; cout<<"9:Lowest value and its location 10:Sort and output 11:Binary Search 12:Exit "; cout << "Enter choice : "; cin >> option; switch(option) { case 1: { for(int i = 0; i < 1000; i++) { cout << array1[i] << " "; } cout << " "; break; } case 2: { int sum = 0; for(int i = 0; i < 1000; i++) { sum += array1[i]; } cout<<" The sum of all values is " << sum <<" "; break; } case 3: { for(int i = 0; i < 1000; i++) { if(array1[i]%2) cout << array1[i]<<" "; } cout << " "; break; } case 4: { for(int i = 0; i < 1000; i++) { if(array1[i]%2 == 0) cout << array1[i]<< " "; } cout << " "; break; } case 5: { cout << "The middle value is " << array1[500] << " "; break; } case 6: { cout << "The first value is " << array1[0] << " "; break; } case 7: { cout<<"The maximum value is "<< array1[999]<< " "; break; } case 8: { int mx = 0; for(int i = 0; i < 1000; i++) { if(array1[i] > array1[mx]) mx = i; } cout << "The maximum value is "<< array1[mx] <<" at location "<< mx <<" "; break; } case 9: { int mn = 0; for(int i = 0; i < 1000; i++) { if(array1[i] < array1[mn]) mn = i; } cout << "The minimum value is "<< array1[mn] <<" at location "<< mn <<" "; break; } case 10: { bubblesort(array1,1000); for(int i = 0; i < 1000; i++) { cout << array1[i] << " "; } cout << " "; break; } case 11: { int data, i; cout << "Enter value to search in array : "; cin >> data; for( i = 0; i < 1000; i++) { if(array1[i] == data) break; } if(i == 1000) cout << data << " not found in array "; else cout << data <<" is found in array at index = "<< i <<" "; break; } case 12: { break; } default: { cout << "Re enter your option "; break; } } } while(option!=12); return 0; };
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started