Question

* This program illustrates how to use a sequential search to find the position of the...

* This program illustrates how to use a sequential search to find the position of the first apparance of a number in an array

TODO#6: change the name to your name and date to the current date * * Created by John Doe, April 17 2019 */ #include using namespace std; //global constant const int ARRAY_SIZE = 10; //TODO#5: provide the function prototype for the function sequentialSearch int main() { //TODO#1: declare an integer array named intList with size of ARRAY_SIZE, no initialization int number; //hold the number to be searched in the array int position = -1; //hold the position where the number is in the array cout << "Please enter " << ARRAY_SIZE << " integers." << endl; //TODO#2: request the values of the array intList from user //hint: use a for loop, request for one element in each iteration cout << "\nPlease enter the number to be searched: "; cin >> number; cout << endl; //TODO#3: call function sequentialSearch to return the position where the number is in the array if (position != -1) cout << number << " is found at index " << position << endl << endl; else cout << number << " is not in the list." << endl << endl; return 0; } //end main function //function definition for sequential search //search the array from the first element sequentially till //find the item, and return the position, or //could not find the item, so return -1 int sequentialSearch(const int list[], int listLength, int searchItem) { int loc = 0; //the first position to be checked in the array is 0 bool found = false; //initially the number has not been found while (loc < listLength && !found) { //TODO#4: access each element of the array list[] (index is loc) //compare it with the given number (searchItem) //if match, found it, set found to true //if not match, increment loc and go to the next iteration } if (found) return loc; //found the number in the array, return the position else return -1; //the number is not in the array } //end function sequentialSearch

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

using namespace std;

const int ARRAY_SIZE = 10;

int sequentialSearch(const int[], int, int);

int main()

{

int list[ARRAY_SIZE];

int position = -1;

int number;

cout << "Please enter " << ARRAY_SIZE << " integers: " << endl;

for(int i = 0; i < ARRAY_SIZE; i++)

{

cin >> list[i];

}

cout << endl << "Please enter the number to be searched: ";

cin >> number;

position = sequentialSearch(list, ARRAY_SIZE, number) + 1;

if(position != -1)

{

cout << endl << number << " is found at index " << position << endl;

}

else

cout << endl << number << " is not in the list." << endl;

return 0;

}

int sequentialSearch(const int list[], int listLength, int searchItem)

{

bool found = false;

int loc = 0;

while (loc < listLength && !found)

{

if(list[loc] == searchItem)

{

found = true;

break;

}

else

loc++;

}

if(found)

return loc;

else

return -1;

}

*********************************************************************** SCREENSHOT ****************************************************

Add a comment
Know the answer?
Add Answer to:
* This program illustrates how to use a sequential search to find the position of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • /* * Program5 for Arrays * * This program illustrates how to use a sequential search...

    /* * Program5 for Arrays * * This program illustrates how to use a sequential search to * find the position of the first apparance of a number in an array * * TODO#6: change the name to your name and date to the current date * * Created by Li Ma, April 17 2019 */ #include <iostream> using namespace std; //global constant const int ARRAY_SIZE = 10; //TODO#5: provide the function prototype for the function sequentialSearch int main() {...

  • Hello, I am working on a C++ pick 5 lottery game that gives you the option...

    Hello, I am working on a C++ pick 5 lottery game that gives you the option to play over and over. I have everything working right except that every time the game runs it generates the same winning numbers. I know this is an srand or rand problem, (at least I think it is), but I can't figure out what my mistake is. I've tried moving srand out of the loop, but I'm still getting the same random numbers every...

  • C++ Time the sequential search and the binary search methods several times each for randomly generated...

    C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...

  • Write a C++ program that contains 2 functions. LastLargestIndex, that takes as paraneters an int array...

    Write a C++ program that contains 2 functions. LastLargestIndex, that takes as paraneters an int array and // its size and returns the index of the first occurrence of the largest element II in the array. Also, write a function to display the array #include ·peh.h" #include <iostream> using namespace std const int ARRAY_SIZE = 15; int main int list[ARRAY SIZE56, 34, 67, 54, 23, 87, 66, 92. 15, 32, 5, 54, 88, 92, 30 cout < List elements: "...

  • Follow the TODOs and complete the insertItem(), searchItem() and printTable() functions in hash.cpp. The hash function...

    Follow the TODOs and complete the insertItem(), searchItem() and printTable() functions in hash.cpp. The hash function has already been implemented for you. // hash.CPP program to implement hashing with chaining #include<iostream> #include "hash.hpp" using namespace std; node* HashTable::createNode(int key, node* next) { node* nw = new node; nw->key = key; nw->next = next; return nw; } HashTable::HashTable(int bsize) { this->tableSize= bsize; table = new node*[tableSize]; for(int i=0;i<bsize;i++) table[i] = nullptr; } //function to calculate hash function unsigned int HashTable::hashFunction(int key)...

  • This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and...

    This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and unorderedArrayList templates that are attached. Create a header file for your unorderedSet template and add it to the project. An implementation file will not be needed since the the new class will be a template. Override the definitions of insertAt, insertEnd, and replaceAt in the unorderedSet template definition. Implement the template member functions so that all they do is verify that the...

  • Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch...

    Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch algorithms as follows: Suppose list is an array of 2500 elements. 1. Use a random number generator to fill list; 2. Use a sorting algorithm to sort list; 3. Search list for some items as follows: a) Use the binary search algorithm to search list (please work on SearchSortAlgorithms.java and modify the algorithm to count the number of comparisons) b) Use the sequential search...

  • Modify PartTwo.cpp so that it is uses a vector, named vect, instead of an array. PartTwo.cpp...

    Modify PartTwo.cpp so that it is uses a vector, named vect, instead of an array. PartTwo.cpp Is posted here: // This program reads data from a file into an array. Then, it // asks the user for a number. It then compares the user number to // each element in the array, displays the array element if it is larger. // After looking at entire array, it displays the number of elements in the array larger // than the user...

  • The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function...

    The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function so that it is written as a value returning function, returning the required item. If the location of the item to be returned is out of range, use the assert function to terminate the program. note: please give all code in c++ below is the class and implementation(a test program would be helpful in determining how to use it): class arrayListType { public:    ...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT