Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int searchList(int[], int, int); // function prototype
  4. int binarySearch(int [], int, int);
  5. void selectionSortArray(int [], int);
  6. void displayArray(int[], int);
  7. const int SIZE = 50;
  8. int main()
  9. {
  10. int numbers;
  11. int array[SIZE];
  12. int found;
  13. int nums;
  14. int amount = 0;
  15. cout << " enter the numbers for the array, to stop enter -99";
  16. for (int amount = 0; amount < 50; amount++)
  17. {
  18. cin >> numbers;
  19. if (numbers = -99) break;
  20. array[amount] = numbers;
  21. }
  22. for (int stop = 0; stop != -99;)
  23. {
  24. cout << "Enter a number to search for:" << endl;
  25. cin >> nums;
  26. found = searchList(array, amount, nums);
  27. if (found == -1)
  28. cout << "The number " << nums
  29. << " was not found in the list" << endl;
  30. else
  31. cout << "The number " << nums <<" is in the " << found + 1
  32. << " position of the list" << endl;
  33. }
  34. return 0;
  35. }
  36. int binarySearch(int array[],int numElems,int value) //function heading
  37. {
  38. int first = 0; // First element of list
  39. int last = numElems - 1; // last element of the list
  40. int middle; // variable containing the current
  41. // middle value of the list
  42. while (first <= last)
  43. {
  44. middle = first + (last - first) / 2;
  45. if (array[middle] == value)
  46. return middle; // if value is in the middle, we are done
  47. else if (array[middle] > value)
  48. last = middle - 1; // toss out the second remaining half of
  49. // the array and search the first
  50. else
  51. first = middle + 1; // toss out the first remaining half of
  52. }
  53. // the array and search the second
  54. return -1; // indicates that value is not in the array
  55. }
  56. int searchList(int List[], int numElems, int value)
  57. {
  58. for (int count = 0; count <= numElems; count++)
  59. {
  60. if (List[count] == value)
  61. // each array entry is checked to see if it contains
  62. // the desired value.
  63. return count;
  64. // if the desired value is found, the array subscript
  65. // count is returned to indicate the location in the array
  66. }
  67. return -1;// if the value is not found, -1 is returned
  68. }
  69. void displayArray(int array[], int elems)// function heading
  70. {
  71.  
  72. // Displays array
  73. for (int count = 0; count < elems; count++)
  74. cout << array[count] << " ";
  75. cout << endl;
  76. }
  77. void selectionSortArray(int array[], int elems)
  78. {
  79. int seek; // array position currently being put in order
  80. int minCount; // location of smallest value found
  81. int minValue; // holds the smallest value found
  82. for (seek = 0; seek < (elems-1);seek++) // outer loop performs the swap
  83. // and then increments seek
  84. {
  85. minCount = seek;
  86. minValue = array[seek];
  87. for(int index = seek + 1; index < elems; index++)
  88. {
  89. if(array[index] > minValue)
  90. {
  91. minValue = array[index];
  92. minCount = index;
  93. }
  94. }
  95. array[minCount] = array[seek];
  96. array[seek] = minValue;
  97. displayArray(array, elems);
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement