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 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...
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...
***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...
Hello, This is an Array Search Program. Details will be provided below and please don't hesitate...
Hello, This is an Array Search Program. Details will be provided below and please don't hesitate to contact me if you need anything. 1- Brief Introduction: Swapping Values Swapping consecutive values in an array: ... Before After |---| |---| arr[2] | 8 | | 1 | |---| |---| arr[3] | 1 | | 8 | |---| |---| ... # Define an array, print the array before the swap arr = [4, 0, 8, 1, 7] print(arr) # Output: [4, 0,...
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...
Write a C program that performs the following: Asks the user to enter his full name...
Write a C program that performs the following: Asks the user to enter his full name as one entry. Asks the user to enter his older brothers’ names each at a time (the user should be instructed by the program to enter NULL if he does not have any older brother). Asks the user to enter his younger brothers’ names each at a time (the user should be instructed by the program to enter NULL if he does not have...
Binary Search. Write a MIPS assembly program to perform a binary search on A[10], which is an array of 10 positive integers.
Binary Search. Write a MIPS assembly program to perform a binary search on A[10], which is an array of 10 positive integers. Your program should have a main routine that does the following:(a) Prompt the user to enter all the 10 integers in the array.(b) Prompt the user to enter the number to be searched.(c) Reads the integer values and makes sure it is a positive integer.(d) Prints the index of the integer. If the input is not available in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT