Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int searchList(int[], int, int); // function prototype
- int binarySearch(int [], int, int);
- void selectionSortArray(int [], int);
- void displayArray(int[], int);
- const int SIZE = 50;
- int main()
- {
- int numbers;
- int array[SIZE];
- int found;
- int nums;
- int amount = 0;
- cout << " enter the numbers for the array, to stop enter -99";
- for (int amount = 0; amount < 50; amount++)
- {
- cin >> numbers;
- if (numbers = -99) break;
- array[amount] = numbers;
- }
- for (int stop = 0; stop != -99;)
- {
- cout << "Enter a number to search for:" << endl;
- cin >> nums;
- found = searchList(array, amount, nums);
- if (found == -1)
- cout << "The number " << nums
- << " was not found in the list" << endl;
- else
- cout << "The number " << nums <<" is in the " << found + 1
- << " position of the list" << endl;
- }
- return 0;
- }
- int binarySearch(int array[],int numElems,int value) //function heading
- {
- int first = 0; // First element of list
- int last = numElems - 1; // last element of the list
- int middle; // variable containing the current
- // middle value of the list
- while (first <= last)
- {
- middle = first + (last - first) / 2;
- if (array[middle] == value)
- return middle; // if value is in the middle, we are done
- else if (array[middle] > value)
- last = middle - 1; // toss out the second remaining half of
- // the array and search the first
- else
- first = middle + 1; // toss out the first remaining half of
- }
- // the array and search the second
- return -1; // indicates that value is not in the array
- }
- int searchList(int List[], int numElems, int value)
- {
- for (int count = 0; count <= numElems; count++)
- {
- if (List[count] == value)
- // each array entry is checked to see if it contains
- // the desired value.
- return count;
- // if the desired value is found, the array subscript
- // count is returned to indicate the location in the array
- }
- return -1;// if the value is not found, -1 is returned
- }
- void displayArray(int array[], int elems)// function heading
- {
- // Displays array
- for (int count = 0; count < elems; count++)
- cout << array[count] << " ";
- cout << endl;
- }
- void selectionSortArray(int array[], int elems)
- {
- int seek; // array position currently being put in order
- int minCount; // location of smallest value found
- int minValue; // holds the smallest value found
- for (seek = 0; seek < (elems-1);seek++) // outer loop performs the swap
- // and then increments seek
- {
- minCount = seek;
- minValue = array[seek];
- for(int index = seek + 1; index < elems; index++)
- {
- if(array[index] > minValue)
- {
- minValue = array[index];
- minCount = index;
- }
- }
- array[minCount] = array[seek];
- array[seek] = minValue;
- displayArray(array, elems);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement