I need to modify the code below to perform a binary search instead of a linear search.
#include <iostream>
using namespace std;
int searchList(int[], int, int);
int main()
{
const int NUMS = 10;
int Picks[NUMS] = { 13579, 26791, 26792, 33445,
55555,
62483,
77777, 79422, 85647, 93121 };
int WinNums,
Search;
cout << "Enter this week's winning five-digit
number: ";
cin >> WinNums;
Search = searchList(Picks, NUMS, WinNums);
if (Search == -1)
cout << "Sorry, no winning
ticket this week.\n";
else
{
cout <<
"Congratulations!\nYou have the winning five-digit number: "
<<
Picks[Search] << endl;
}
system("pause");
return 0;
}
int searchList(int list[], int size, int value)
{
int index = 0;
int position = -1;
bool found = false;
while (index < size && !found)
{
if (list[index] == value)
{
position =
index;
found =
true;
}
index++;
}
return position;
}
#include <iostream>
#include <algorithm>
using namespace std;
int binarySearchList(int[], int, int);
int main()
{
const int NUMS = 10;
int Picks[NUMS] = { 13579, 26791, 26792, 33445, 55555,
62483, 77777, 79422, 85647, 93121 };
int WinNums,
Search;
cout << "Enter this week's winning five-digit number: ";
cin >> WinNums;
Search = binarySearchList(Picks, NUMS, WinNums);
if (Search == -1)
cout << "Sorry, no winning ticket this week.\n";
else
{
cout << "Congratulations!\nYou have the winning five-digit number: "
<< Picks[Search] << endl;
}
system("pause");
return 0;
}
int binarySearchList(int list[], int size, int value)
{
int index = 0;
int position = -1;
int low = 0, high = size-1;
sort(list, list+size);
while (low <= high)
{
int mid = low + (high-low)/2;
if (list[mid] == value)
return mid;
if (list[mid] < value)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
==================================================
SEE OUTPUT

Thanks, PLEASE COMMENT if there is any
concern.
I need to modify the code below to perform a binary search instead of a linear...
Please code in C++! I'm not too sure what the blue box session
is but I included it. Please help if you can
8.2: Lottery Winners A lottery ticket buyer purchases 10 tickets a week, always playing the same 10 5- digit "lucky" combinations. Write a program that initializes an array or a vector with these numbers and then lets the player enter this week's winning 5- digit number. The program should perform a linear search through the list of...
// This program performs a linear search on a character array // Place Your Name Here #include <iostream> using namespace std; int searchList(char[], int, char); // function prototype const int SIZE = 8; int main() { char word[SIZE] = "Harpoon"; int found; char ch; cout << "Enter a letter to search for:" << endl; cin >> ch; found = searchList(word, SIZE, ch); if (found == -1) cout << "The letter " << ch << " was not found in the...
I have to type and explain in class each code in every detail filled with // commentary. Explains how does work in every codes. 1) What does the below print #include <iostream> using namespace std ; int main() { int var1 = 20 ; int var2 = 30 ; int* ptr1 ; int* ptr2 ; int* temp ; ptr1 = &var1 ; ptr2 = &var2 ; cout << *ptr1 << endl ;...
C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...
/* * Program5 for Arrays * * This program illustrates how to use a sequential search to * find the position of the first apparance of a number in an array * * TODO#6: change the name to your name and date to the current date * * Created by Li Ma, April 17 2019 */ #include <iostream> using namespace std; //global constant const int ARRAY_SIZE = 10; //TODO#5: provide the function prototype for the function sequentialSearch int main() {...
* This program illustrates how to use a sequential search to find the position of the first apparance of a number in an array TODO#6: change the name to your name and date to the current date * * Created by John Doe, April 17 2019 */ #include using namespace std; //global constant const int ARRAY_SIZE = 10; //TODO#5: provide the function prototype for the function sequentialSearch int main() { //TODO#1: declare an integer array named intList with size of...
Must be written in JAVA Code Modify Fig. 19.2 to use recursive method recursiveLinear-Search to perform a linear search of the array. The method should receive the search key and starting index as arguments. If the search key is found, return its index in the array; otherwise, return –1. Each call to the recursive method should check one index in the array. Figure 19.2 import java.security.SecureRandom; import java.util.Arrays; import java.util.Scanner; public class LinearSearchTest{ public static int linearSearch(int data[], int searchKey)...
Please help with my C++ homework! 1. The following function is supposed to perform binary search. It has no errors and will execute correctly. int binarySearch(int array[], int size, int value) { int first = 0, // First array element last = size - 1, // Last array element middle, // Mid point of search position = -1; // Position of search value bool found = false; // Flag middle = (first + last)...
C programming (you don't need to write program) Problem 1 [Linear Search with Early Stop] Below you will find a linear search function with early stop. A linear search is just a naive search - you go through each of the elements of a list one by one. Early stop works only on sorted list. Early stop means, instead of going through whole list, we will stop when your number to search can no longer be possibly found in the...
I need help of how to include the merge sort code into this counter code found below: #include<iostream> using namespace std; int bubble_counter=0,selection_counter=0; // variables global void bubble_sort(int [], int); void show_array(int [],int); void selectionsort(int [], int ); int main() { int a[7]={4,1,7,2,9,0,3}; show_array(a,7); //bubble_sort(a,7); selectionsort(a,7); show_array(a,7); cout<<"Bubble counter = "<<bubble_counter<<endl; cout<<"Selection Counter = "<<selection_counter<<endl; return 0; } void show_array(int array[],int size) { for( int i=0 ; i<size; i++) { cout<<array[i]<< " "; } cout<<endl; } void bubble_sort( int array[],...