Saturday, December 26, 2020

Search input Data from array by using binary method with Eaglesoft programmers.

 


#include <iostream>
using namespace std;
int main()
{
    int num[10] = {345672234445776};
    int s = 0l = 10midloc = 0;
    int _input;

    // run infinty time of program
    while (true)
    {
        // take input form user
        cout << "Enter a value that you want to find in array: " << endl;
        cin >> _input;

        // search data from array
        while (s <= l)
        {
            mid = (s + l) / 2;
            if (_input == num[mid])
            {
                // cout<<"Found at position: "<<mid<<endl;
                loc = mid;
                break;
            }
            else if (_input > num[mid])
            {
                s = ++mid;
            }
            else
            {
                l = --mid;
            }
        }

        // print data on screen getting from array
        if (num[loc] == _input)
        {
            cout << "Found at position: " << loc << endl;
        }
        else
        {
            cout << "Value not found in array!" << endl;
        }
        cout << "******************************************" << endl;
    }

    return 0;
}

No comments:

Post a Comment