140k views
2 votes
Write C++ program to perform sequential and Binary searches

the program MUST implement methods/function
1) a method/function to enable the user to populate an int array with any 20 numbers
(assume that the numbers are already sorted)
2) a method or afunction to perform a sequential search based on the user's input.
3 ) a method or afunction to perform a binary search based on the user's input
the output must address
a) when the target is found and where it is found.
b) display a message if target is not found
c) number of searches performed

1 Answer

6 votes

Final answer:

To write a C++ program to perform sequential and binary searches, you need to create methods or functions to populate an array, perform a sequential search, and perform a binary search. The program should display the search output and the number of searches performed.

Step-by-step explanation:

To write a C++ program to perform sequential and binary searches, you can follow these steps:


  1. Create a method or function to populate an int array with 20 numbers. You can use a loop to take user input.

  2. Create another method or function to perform a sequential search based on user input. Iterate through the array and compare each element with the target value.

  3. Create a third method or function to perform a binary search based on user input. Use the binary search algorithm by dividing the array in half and comparing the middle element with the target value.

In the output, you should:


  1. If the target is found, display a message indicating its position in the array.

  2. If the target is not found, display a message indicating that it was not found.

  3. Count the number of searches performed and display it as the output as well.

User Prathamesh Shetye
by
8.3k points