Question

In: Computer Science

// This program performs a linear search on a character array // Place Your Name Here...

// This program performs a linear search on a character array

// Place Your Name Here

#include<iostream>

using namespace std;

int searchList( char[], int, char); // function prototype

const int SIZE = 8;

int main()

{

char word[SIZE] = "Harpoon";

int found;

char ch;

cout << "Enter a letter to search for:" << endl;

cin >> ch;

found = searchList(word, SIZE, ch);

if (found == -1)

cout << "The letter " << ch

     << " was not found in the list" << endl;

else

cout << "The letter " << ch <<" is in the " << found + 1

  << " position of the list" << endl;

return 0;

}

//*******************************************************************

//   searchList

//

// task:

// data in:

// data returned:

//

//*******************************************************************

int searchList( char List[], int numElems, char value)

{

for (int count = 0;count <= numElems; count++)  

{//WRITE STATEMENT TO CHECK FOR VALUE IN EACH ARRAY ELEMENT

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

}

Solutions

Expert Solution

// This program performs a linear search on a character array

// Place Your Name Here

#include<iostream>

using namespace std;

int searchList( char[], int, char); // function prototype

const int SIZE = 8;

int main()

{

char word[SIZE] = "Harpoon";

int found;

char ch;

cout << "Enter a letter to search for:" << endl;

cin >> ch;

found = searchList(word, SIZE, ch);

if (found == -1)

cout << "The letter " << ch

     << " was not found in the list" << endl;

else

cout << "The letter " << ch <<" is in the " << found + 1

  << " position of the list" << endl;

return 0;

}

//*******************************************************************

//   searchList

//

// task:

// data in:

// data returned:

//

//*******************************************************************

int searchList( char List[], int numElems, char value)

{

for (int count = 0;count <= numElems; count++)  

{//WRITE STATEMENT TO CHECK FOR VALUE IN EACH ARRAY ELEMENT

    if(List[count]==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

}


Related Solutions

// This program ask the user to enter a character. It then performs a // linear...
// This program ask the user to enter a character. It then performs a // linear search on a character array and display the number of times // that the character appears on the array. If the character is not in the // array, then it will display a message saying that is was not found. // Add the necessary code for the program to work. // NOTE: // You don't have to edit anything in the main(), just in...
// This program demonstrates a Binary Search //PLACE YOUR NAME HERE #include<iostream> using namespace std; int...
// This program demonstrates a Binary Search //PLACE YOUR NAME HERE #include<iostream> using namespace std; int binarySearch(int [], int, int);  // function prototype const int SIZE = 16; int main() { int found, value; int array[] = {34,19,19,18,17,13,12,12,12,11,9,5,3,2,2,0}; // array to be searched cout << "Enter an integer to search for:" << endl; cin >> value; found = binarySearch(array, SIZE, value); //function call to perform the binary search   //on array looking for an occurrence of value if (found == -1) cout...
Part 1: Create a character array and save your first and last name in it
PROGRAMMING IN C:Part 1:Create a character array and save your first and last name in itNote: You can assign the name directly or you can use the scanf function.Display your name on the screen.Display the address (memory location) in hexadecimal notation of the array. (hint: use %p)Use a for loop to display each letter of your name on a separate line.Part 2:Create a one dimensional array and initialize it with 10 integers of your choice.Create a function and pass the...
// This program demonstrates a Binary Search, which search for a value // in an array,...
// This program demonstrates a Binary Search, which search for a value // in an array, assuming that the array is sorted in descending order. // You have to modify the function binarySearch() to search for a value // in an array that is sorted in ascending order. // NOTES: // Uncomment line 34 and comment line 32. You don't have to edit anything // else in the main(), just in the binarySearch() function. // EXAMPLES (using the array sorted...
Please name your driver program XXX_P03 where XXX are your initials. Please name your array utilities...
Please name your driver program XXX_P03 where XXX are your initials. Please name your array utilities class ArrayUtilities. Have only static methods in your ArrayUtilities class. Make sure your program will work for any array size. Write a driver program with a double array that is initialized with the following test data. You may hard code this data into your array. Echo the input in a neat table formatted exactly as below (5 elements to a line and aligned). Then...
Write a program to show the difference between linear search and binary search. Show the input...
Write a program to show the difference between linear search and binary search. Show the input test data for your program and the output produced by your program which clearly show that binary search is faster than linear search
1. Write a Python program that performs the following: 2. Defines an array of integers from...
1. Write a Python program that performs the following: 2. Defines an array of integers from 1 to 10. The numbers should be filled automatically without the need for user inputs 3. Find the sum of the numbers that are divisible by 3 (i.e., when a number is divided by 3, the remainder is zero) 4. Swap the positions of the maximum and minimum elements in the array. First, you need to find the maximum element as shown in the...
Write a program that allows the user to search the array to find the number entered
Write a program that allows the user to search the array to find the number entered
***IN C++*** Create student structure with the following fields:  Name (cstring or null-terminated character array)...
***IN C++*** Create student structure with the following fields:  Name (cstring or null-terminated character array)  Student ID (int – unique random value between 1000 and 9999)  grade (char – Values A thru F)  birthday (myDate – random value: range 1/1/2000 to 12/31/2005)  Home Town (string) Create an array of pointers to students of size 10. Example: Student *stuPtr[10]; Write a function that populates the array with 10 students. Example: populate(stuPtr); Write a display function that...
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace...
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace std; // Binary search algorith // f is the first , l is the last , t is the target int binarySearch(int stgrade[], int f, int l, int t) { while (f <= l) { int m = f + (l - l) / 2; // Check if x is present at mid if (stgrade[m] == t) return m; // If x greater, ignore...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT