Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main()
- {
- // Search for a specific number in an array
- int array[19] = {2, 5, 3, 8, 6, 4, 7, 1, 1, 3, 4, 8, 1, 3, 4, 1, 1, 1, 1};
- //Searching for number 5;
- int search_number = 1;
- int temp;
- int length = 19;
- //First we want to sort the array through bubble-sorting
- for(int x = 0; x < length - 1; x++)
- {
- for(int y = x + 1; y < length; y++)
- {
- if(array[x] > array[y])
- {
- temp = array[x];
- array[x] = array[y];
- array[y] = temp;
- }
- }
- }
- for(int z = 0; z < length; z++)
- cout << array[z] << " ";
- //system("cls");
- cout << endl;
- //After sorting the array you can now do the method of finding the number
- int variable = (length/2);
- cout << variable << endl;
- int middle = length/2;
- while(array[variable] != search_number)
- {
- if(array[variable] > search_number)
- {
- variable = variable/2;
- }
- else
- {
- variable = (variable / 2) + middle;
- }
- }
- cout << "The program found the search_number in index " << variable << " in the array." <<
- endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement