Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This program performs a linear search on a character array
- // Place Your Name Here
- #include <iostream>
- using namespace std;
- int searchList( int[], int, int);
- // function prototype
- const int SIZE = 8;
- int main()
- {
- int word[SIZE] = {3,6,-19,5,5,0,-2,99};
- int found;
- int ch;
- cout << "Enter a number to search for:" << endl;
- cin >> ch;
- found = searchList(word, SIZE, ch);
- if (found == -1)
- cout << "The number " << ch << " was not found in the list" << endl;
- else
- cout << "The number " << ch <<" is in the " << found + 1 << " position of the list" << endl;
- return 0;
- }
- //*******************************************************************
- // searchList
- //
- // task: This searches an array for a particular value
- // data in: List of values in an array, the number of
- // elements in the array, and the value searched for
- // in the array
- // data returned: Position in the array of the value or -1 if value
- // not found
- //
- //*******************************************************************
- int searchList( int List[], int numElems, int value)
- {
- for (int count = 0;count < numElems; count++) //haha professor i found a mistake, i replaced <= with <. now everything works as it should.
- {
- 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
- }
- //unsuccessful integer search
- /*
- Enter a number to search for:
- 98
- The number 98 is in the 9 position of the list
- Process returned 0 (0x0) execution time : 5.838 s
- Press any key to continue.
- */
- //Successful integer search
- /*
- Enter a number to search for:
- 5
- The number 5 is in the 4 position of the list
- Process returned 0 (0x0) execution time : 0.497 s
- Press any key to continue.
- */
- // This program performs a linear search on an integer array
- // Joseph Burger
- #include <iostream>
- using namespace std;
- int searchList( int[], int, int);
- // function prototype
- const int SIZE = 8;
- int main()
- {
- int word[SIZE] = {3,6,-19,5,5,0,-2,99};
- int found;
- int ch;
- do
- {
- cout << "Enter a number to search for:\nEnter 66 to quit" << endl;
- cin >> ch;
- if(ch==66)
- break;
- found = searchList(word, SIZE, ch);
- if (found == -1)
- cout << "The number " << ch << " was not found in the list" << endl;
- else
- cout << "The number " << ch <<" is in the " << found + 1 << " position of the list" << endl;
- }while(1);
- return 0;
- }
- //*******************************************************************
- // searchList
- //
- // task: This searches an array for a particular value
- // data in: List of values in an array, the number of
- // elements in the array, and the value searched for
- // in the array
- // data returned: Position in the array of the value or -1 if value
- // not found
- //
- //*******************************************************************
- int searchList( int List[], int numElems, int value)
- {
- for (int count = 0;count < numElems; count++) //haha professor i found a mistake, i replaced <= with <. now everything works as it should.
- {
- 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
- }
- /*
- Re-write the program (and save it as LastFirst_lab81b.cpp ) so that the user can continue
- to input values that will be searched for, until a sentinel value is entered to end the
- program. Should a pre or post test loop be used?
- */
- /*
- 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
- 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
- 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
- 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
- the idea of breaking out of the loop.'
- */
Advertisement
Add Comment
Please, Sign In to add comment