Question

In: Computer Science

Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace...

Correct this Binary Search (C++)

// This program demostrates linear search algorithm

#include <iostream>

using namespace std;
// Binary search algorith
// f is the first , l is the last , t is the target
int binarySearch(int stgrade[], int f, int l, int t)
{
while (f <= l)
{
int m = f + (l - l) / 2;

// Check if x is present at mid
if (stgrade[m] == t)
return m;

// If x greater, ignore left half
if (stgrade[m] < t)
f = m + 1;

// If x is smaller, ignore right half
else
l = m - 1;
}

// if we reach here, then element was not present
return -1;
}
int main(void)
{
int target;
int stgrade[] = { 100, 73, 94, 100, 70, 86, 55, 90, 74 };
// size of the array
int s = sizeof(stgrade) / sizeof(stgrade[0]);

cout << "This is your list" << endl;
for (int i = 0; i < s; i++)
cout << stgrade[i] << " ";
cout << endl;

cout << "Input the target to find , in or out side of your list" << endl;
cin >> target;

// 0 is the index of 1st element
// s is the size and s-1 is in dext of last element
// target is what you want to find

int result = binarySearch(stgrade, 0, s - 1 , target);

if (result == -1)
cout << "Your target NOT FOUND in the list" << endl;
else
cout << "Your target is in the list " << result + 1 << endl;

system("pause");

return 0;
}

Solutions

Expert Solution


#include <iostream>

using namespace std;


int binarySearch(int stgrade[], int f, int l, int t)
{
while (f <= l)
{
int m = f + (l - l) / 2;

// Check if x is present at mid
if (stgrade[m] == t)
return m;

// If x greater, ignore left half
if (stgrade[m] < t)
return binarySearch(stgrade, m + 1, l, t);


// If x is smaller, ignore right half
else
return binarySearch(stgrade, f, m - 1, t);

}

// if we reach here, then element was not present
return -1;
}


int main(void)
{
int target;
int stgrade[] = { 100, 73, 94, 100, 70, 86, 55, 90, 74 };
// size of the array
int s = sizeof(stgrade) / sizeof(stgrade[0]);

cout << "This is your list" << endl;
for (int i = 0; i < s; i++)
cout << stgrade[i] << " ";
cout << endl;

cout << "Input the target to find , in or out side of your list" << endl;
cin >> target;

// 0 is the index of 1st element
// s is the size and s-1 is in dext of last element
// target is what you want to find

int result = binarySearch(stgrade, 0, s - 1 , target);

if (result == -1)
cout << "Your target NOT FOUND in the list" << endl;
else
cout << "Your target is in the list " << result + 1 << endl;

system("pause");

return 0;
}


Related Solutions

// This program demonstrates a Binary Search //PLACE YOUR NAME HERE #include<iostream> using namespace std; int...
// This program demonstrates a Binary Search //PLACE YOUR NAME HERE #include<iostream> using namespace std; int binarySearch(int [], int, int);  // function prototype const int SIZE = 16; int main() { int found, value; int array[] = {34,19,19,18,17,13,12,12,12,11,9,5,3,2,2,0}; // array to be searched cout << "Enter an integer to search for:" << endl; cin >> value; found = binarySearch(array, SIZE, value); //function call to perform the binary search   //on array looking for an occurrence of value if (found == -1) cout...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must be very basic. Please don't use math sqrt. Assume that the user does not input anything less than 0. For example: the integer square root of 16 is 4 because 4 squared is 16. The integer square root of 18 is 5 because 4 squared is 16 and 5 squared is 25, so 18 is bigger than 16 but less than 25.  
How do i make this program accept strings? #include <iostream> #include <algorithm> using namespace std; int...
How do i make this program accept strings? #include <iostream> #include <algorithm> using namespace std; int binaryToOctal(char digits[], int a) { int number; if (digits[0] == '0') { if (digits[1] == '1') if (digits[2] == '0') return 2; //found "010" else return 3; //found "011" else if (digits[1] == '0') if (digits[2] == '1') return 1; // found "001" else return 0; //found "000" } else { if (digits[0] == '1') if (digits[1] == '1') if (digits[2] == '0') return...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char plain[50], cipher[50]="", decrypt[50]=""; int subkeys[50], len;       cout<<"Enter the plain text:"<<endl; cin>>plain;    cout<<"Enter the first subkey:"<<endl; cin>>subkeys[0];    _strupr(plain);    len = strlen(plain);    /**********Find the subkeys**************/    for(int i=1; i<len; i++) { if ((plain[i-1]>='A') && (plain[i-1]<='Z')) { subkeys[i] = plain[i-1]-65; } }    /****************ENCRYPTION***************/       for(int i=0; i<len; i++) { if ((plain[i]>='A') && (plain[i]<='Z')) {    cipher[i] = (((plain[i]-65)+subkeys[i])%26)+65; }...
Take the following C++ program and translate it into PEP/9 assembly language #include <iostream> using namespace...
Take the following C++ program and translate it into PEP/9 assembly language #include <iostream> using namespace std; int num; char letter; int main() {    cin >> num;    cin >> letter;    cout << "You inputted " << num << endl;    cout << "Option " << letter << endl;    if (letter == '*')       cout << "Multiplied by 2 " << num*2 << endl;    return 0; }
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std;...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std; void leapYear(int x); int main() { int x; cout << "Enter a year: "; cin >> x; leapYear (x);   return 0; } void leapYear(int x ) {    if (x % 400 == 0)    {    cout << "This is a leap Year";}    else if    ((x % 4 == 0) && (x % 100 != 0))    {    cout <<...
I want to indent this c++ program #include<iostream> using namespace std; class Rectangle{ private: double width;...
I want to indent this c++ program #include<iostream> using namespace std; class Rectangle{ private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; double getPerimeter() const; bool isSquare() const; }; void Rectangle::setWidth(double w){ width = w; } void Rectangle::setLength(double l){ length = l; } double Rectangle::getWidth() const{ return width; } double Rectangle::getLength() const{ return length; } // Added Method definitions double Rectangle::getArea() const{ return (width * length); } double Rectangle::getPerimeter() const{...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell {...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell { int val; Cell *next; }; int main() { int MAX = 10; Cell *c = NULL; Cell *HEAD = NULL; srand (time(NULL)); for (int i=0; i<MAX; i++) { // Use dynamic memory allocation to create a new Cell then initialize the // cell value (val) to rand(). Set the next pointer to the HEAD and // then update HEAD. } print_cells(HEAD); }
C++ finish the AVL Tree code: #include "AVLNode.h" #include "AVLTree.h" #include <iostream> #include <string> using namespace...
C++ finish the AVL Tree code: #include "AVLNode.h" #include "AVLTree.h" #include <iostream> #include <string> using namespace std; AVLTree::AVLTree() { root = NULL; } AVLTree::~AVLTree() { delete root; root = NULL; } // insert finds a position for x in the tree and places it there, rebalancing // as necessary. void AVLTree::insert(const string& x) { // YOUR IMPLEMENTATION GOES HERE } // remove finds x's position in the tree and removes it, rebalancing as // necessary. void AVLTree::remove(const string& x) {...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an array. Use number of data N at least more than 10. The function Binary Search should return the index of the search key V.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT