candyapplecorn

elephant marbles

Jun 19th, 2013
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. // This program performs a linear search on a character array
  2. // Place Your Name Here
  3. #include <iostream>
  4. using namespace std;
  5. int searchList( int[], int, int);
  6. // function prototype
  7. const int SIZE = 8;
  8. int main()
  9. {
  10. int word[SIZE] = {3,6,-19,5,5,0,-2,99};
  11. int found;
  12. int ch;
  13. cout << "Enter a number to search for:" << endl;
  14. cin >> ch;
  15. found = searchList(word, SIZE, ch);
  16. if (found == -1)
  17. cout << "The number " << ch << " was not found in the list" << endl;
  18. else
  19. cout << "The number " << ch <<" is in the " << found + 1 << " position of the list" << endl;
  20. return 0;
  21. }
  22. //*******************************************************************
  23. // searchList
  24. //
  25. // task: This searches an array for a particular value
  26. // data in: List of values in an array, the number of
  27. // elements in the array, and the value searched for
  28. // in the array
  29. // data returned: Position in the array of the value or -1 if value
  30. // not found
  31. //
  32. //*******************************************************************
  33. int searchList( int List[], int numElems, int value)
  34. {
  35. for (int count = 0;count < numElems; count++) //haha professor i found a mistake, i replaced <= with <. now everything works as it should.
  36. {
  37. if (List[count] == value)
  38. // each array entry is checked to see if it contains
  39. // the desired value.
  40. return count;
  41. // if the desired value is found, the array subscript
  42. // count is returned to indicate the location in the array
  43. }
  44. return -1;
  45. // if the value is not found, -1 is returned
  46. }
  47.  
  48. //unsuccessful integer search
  49. /*
  50. Enter a number to search for:
  51. 98
  52. The number 98 is in the 9 position of the list
  53.  
  54. Process returned 0 (0x0) execution time : 5.838 s
  55. Press any key to continue.
  56. */
  57. //Successful integer search
  58. /*
  59. Enter a number to search for:
  60. 5
  61. The number 5 is in the 4 position of the list
  62.  
  63. Process returned 0 (0x0) execution time : 0.497 s
  64. Press any key to continue.
  65. */
  66.  
  67.  
  68.  
  69.  
  70.  
  71. // This program performs a linear search on an integer array
  72. // Joseph Burger
  73. #include <iostream>
  74. using namespace std;
  75. int searchList( int[], int, int);
  76. // function prototype
  77. const int SIZE = 8;
  78. int main()
  79. {
  80. int word[SIZE] = {3,6,-19,5,5,0,-2,99};
  81. int found;
  82. int ch;
  83. do
  84. {
  85. cout << "Enter a number to search for:\nEnter 66 to quit" << endl;
  86. cin >> ch;
  87. if(ch==66)
  88. break;
  89. found = searchList(word, SIZE, ch);
  90. if (found == -1)
  91. cout << "The number " << ch << " was not found in the list" << endl;
  92. else
  93. cout << "The number " << ch <<" is in the " << found + 1 << " position of the list" << endl;
  94. }while(1);
  95.  
  96. return 0;
  97. }
  98. //*******************************************************************
  99. // searchList
  100. //
  101. // task: This searches an array for a particular value
  102. // data in: List of values in an array, the number of
  103. // elements in the array, and the value searched for
  104. // in the array
  105. // data returned: Position in the array of the value or -1 if value
  106. // not found
  107. //
  108. //*******************************************************************
  109. int searchList( int List[], int numElems, int value)
  110. {
  111. for (int count = 0;count < numElems; count++) //haha professor i found a mistake, i replaced <= with <. now everything works as it should.
  112. {
  113. if (List[count] == value)
  114. // each array entry is checked to see if it contains
  115. // the desired value.
  116. return count;
  117. // if the desired value is found, the array subscript
  118. // count is returned to indicate the location in the array
  119. }
  120. return -1;
  121. // if the value is not found, -1 is returned
  122. }
  123.  
  124.  
  125. /*
  126. Re-write the program (and save it as LastFirst_lab81b.cpp ) so that the user can continue
  127. to input values that will be searched for, until a sentinel value is entered to end the
  128. program. Should a pre or post test loop be used?
  129. */
  130. /*
  131. 1. It doesn't matter what kind of loop I use. If I use a while loop, i just have to initialize the sentinel so that the
  132. loop executes at least once. I chose a do while loop so I wouldn't have to go to the trouble of initializing the sentinel before
  133. the loop. if you look at my code the loop is made sort of superfluous because when the sentinel is entered, the program just breaks
  134. out of the loop and the next statement it finds is return 0;. I could have easily put return 0; instead of break; but I liked
  135. the idea of breaking out of the loop.'
  136. */
Advertisement
Add Comment
Please, Sign In to add comment