1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
#include <cstdlib>
#include <iostream>
using namespace std;
//-----------------------------------------------
void printNumbers (int Arr[], int count);
void binarySearch (int Arr[], int left, int right);
void readNumbers (int Arr[], int count);
//---------------------------------------------
int main()
{
int Arr[] = {1, 2, 3, 4, 5, 6, 7};
int left = Arr[0];
int right = Arr[6];
int count = sizeof(Arr) / sizeof(Arr[0]);
cout << left << " " << right;
for (;;)
{
cout << endl
<< "Work with numbers:" << endl
<< " 1 - Print numbers" << endl
<< " 2 - Binary search" << endl
<< " 3 - Edit numbers" << endl
<< endl
<< " 0 - Exit" << endl;
string key;
cin >> key;
if (key == "0") return 0;
else if (key == "1") printNumbers (Arr, count);
else if (key == "2") binarySearch (Arr, left, right);
else if (key == "3") readNumbers (Arr, count);
else cout << endl << "Bad command..." << endl;
}
}
//-------------------------------------------------
void readNumbers (int Arr[], int count)
{
cout << "Insert Numbers:" << endl;
for (int i = 0; i < count; i++)
{
cout << (i+1) << "/" << count << ": ";
cin >> Arr[i];
}
}
//------------------------------------------------
void binarySearch(int Arr[], int left, int right) {
int value;
int middle;
cout << "Which number to find: ";
cin >> value;
while (left <= right) {
middle = (left + right) / 2;
if (Arr[middle] == value){
middle = middle;
}
else if (Arr[middle] > value){
right = middle - 1;}
else if (Arr[middle] < value){
left = middle + 1;}
else cout << "Where isn't such number";
}
cout << value << " are " << middle << " array element";
}
//------------------------------------------------
void printNumbers (int Arr[], int count)
{
for (int i = 0; i < count; i++)
cout << Arr[i] << " ";
cout << "\n";
}
|