Question

In: Computer Science

// 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 the searchArray() function.

// EXAMPLES:

// Input: Enter a letter to search for: h

// Output: The are 2 number of 'h' letters in the list

// Input: Enter a letter to search for: n

// Output: The are 2 number of 'n' letters in the list

// Input: Enter a letter to search for: z

// Output: The letter z was not found in the list

#include<iostream>

using namespace std;

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

const int SIZE = 10;

int main()

{

              //char charArray[SIZE] = {'h', 'h', 'a', 'r', 'p', 'o', 'o', 'n', 'n', '\0'};

              char charArray[SIZE] = "hharpoonn";

              int found;

              char ch;

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

              cin >> ch;

              found = searchArray(charArray, SIZE, ch);

              if (found == 0)

                             cout << "The letter " << ch

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

              else

                             cout << "The are " << found << " number of '" << ch

                                           <<"' letters in the array" << endl;

              return 0;

}

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

//                      searchArray

//

// task:       This searches an array for a particular value

// data in:       Array of values, the number of

//                elements in the array, and the value searched for

//                in the array

// data returned: Number of times the value appears in the list or 0

//                                                      if the value is not found

//

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

int searchArray(char array[], int numElems, char value)

{

              // Counter, contains the number of times the character appears on the array.

              int count = 0;

              // Add code to search the array and count the number of times the

              // character appears in it.

              // HINT: for-loop similar to the one in linearSearch.cpp

              // Complete the return statement. If the value is found, returns the

              // number of characters founded. If the value is not found, 0 is returned.

              return _________;

}

Solutions

Expert Solution

// 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 the searchArray() function.
// EXAMPLES:
// Input: Enter a letter to search for: h
// Output: The are 2 number of 'h' letters in the list
// Input: Enter a letter to search for: n
// Output: The are 2 number of 'n' letters in the list
// Input: Enter a letter to search for: z
// Output: The letter z was not found in the list
#include<iostream>

using namespace std;

int searchArray(char[], int, char); // function prototype
const int SIZE = 10;

int main() {
    //char charArray[SIZE] = {'h', 'h', 'a', 'r', 'p', 'o', 'o', 'n', 'n', '\0'};
    char charArray[SIZE] = "hharpoonn";
    int found;
    char ch;
    cout << "Enter a letter to search for: ";
    cin >> ch;
    found = searchArray(charArray, SIZE, ch);
    if (found == 0)
        cout << "The letter " << ch
             << " was not found in the array" << endl;
    else
        cout << "The are " << found << " number of '" << ch
             << "' letters in the array" << endl;
    return 0;
}

//*******************************************************************
//                      searchArray
//
// task:       This searches an array for a particular value
// data in:       Array of values, the number of
//                elements in the array, and the value searched for
//                in the array
// data returned: Number of times the value appears in the list or 0
//                                                      if the value is not found
//
//*******************************************************************
int searchArray(char array[], int numElems, char value) {
    // Counter, contains the number of times the character appears on the array.
    int count = 0;
    // Add code to search the array and count the number of times the
    // character appears in it.
    for (int i = 0; i < numElems; ++i) {
        if (array[i] == value)
            ++count;
    }
    // Complete the return statement. If the value is found, returns the
    // number of characters founded. If the value is not found, 0 is returned.
    return count;
}

Related Solutions

1. Write a program that will ask the user to enter a character and then classify...
1. Write a program that will ask the user to enter a character and then classify the character as one of the following using only IF-ELSE and logical operators. (50 points - 10pts for syntax, 10pts for commenting and 30pts for successful execution) • Integer • Lower Case Vowel • Upper Case Vowel • Lower Case Consonant • Upper Case Consonant • Special Character
Write a program that will ask the user to enter the amount of a purchase. The...
Write a program that will ask the user to enter the amount of a purchase. The program should then compute the state and county sales tax. Assume the state sales tax is 5 percent and the county sales tax is 2.5 percent. The program should display the amount of the purchase, the state sales tax, the county sales tax, the total sales tax, and the total of the sale (which is the sum of the amount of purchase plus the...
In C: Write a complete program that performs the following task: Ask the user for the...
In C: Write a complete program that performs the following task: Ask the user for the number of sequences to display. For each sequence, Ask the user for a starting value Print out the value and double it (multiply by 2). Continue printing and doubling (all on the same line, separated by one space each) as long as the current number is less than 1000, or until 8 numbers have been printed on the line. You may assume that the...
// 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...
Design and implement a C++ program that performs the following steps:Ask the user to enter a...
Design and implement a C++ program that performs the following steps:Ask the user to enter a positive integer number N; Your program may need to prompt the user to enter many times until it reads in a positive number;Let user to enter N (obtained in the previous step) floating point numbers, and count how many positive ones there are in the sequence and sum up these positive numbers; (Hint: negative numbers or 0 are ignored).Display result.You can and should use...
Write a program that does the following. It will ask the user to enter an integer...
Write a program that does the following. It will ask the user to enter an integer larger than 1, and the if entered integer is not larger than 1, it keeps prompting the user. After the user enters a valid integer, the program prints all the prime factors of the integer (including the repeated factors). For example, if the entered integer is 24, the program prints: 2 2 2 3 Run your program with the test cases where the entered...
write a program i java that ask the user to enter salary, user ID and username...
write a program i java that ask the user to enter salary, user ID and username and out put them
In your python program, ask the user to enter the annual income of an employee and...
In your python program, ask the user to enter the annual income of an employee and the years of experience. Pass these data to a function. The function decides if an employee does qualify for a loan or does not, then it prints a message based on the following rules: If the income is equal or greater than $40000, the function checks if the employee’s years of experience is greater than 4, if so the employee gets $6000 loan; otherwise,...
Write a MIPS program that will ask the user to enter two numbers at the console...
Write a MIPS program that will ask the user to enter two numbers at the console and pass the values to a function that does multiplication
Write the following program in Java. Ask the user to enter the number of rows and...
Write the following program in Java. Ask the user to enter the number of rows and columns for a 2-D array. Create the array and fill it with random integers using (int)(Math.random() * 20) (or whatever number you choose). Then, provide the user with several menu choices. 1. See the entire array. 2. See a specific row. 3. See a specific column. 4. See a specific value. 5. Calculate the average of each row. 6. Calculate the total of each...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT