Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this project, you will write a C++ program with the following functions: 3.

ID: 3721703 • Letter: F

Question

 For this project, you will write a C++ program with the following functions:
  3. Write a function with the following description.  /* * This function inspects the characters in the given array charArray and  * returns the character that has the highest number of occurrences. *  * for e.g. for the input array {'e','y','e'}, the function returns 'e' * * for e.g. for the input array {'m','i','s','s','i','s','s','i','p','i'},  *    the function returns 's' * * for input array {'c','a','r'}, the function is allowed to return any  *    arbitrary character within the array (e.g. 'c') * * You can assume that charArray[] contains only characters a-z and/or A-Z * */ char getMostFrequentChar(char charArray[], int arraySize);   4. Write another version of the above function that is able to ignore non-alphabetic characters from the array, but otherwise still functions the same way.  /* * for the input array {'e', '1', '@', 'y','e', '1', 'Y'}, the function returns 'e' * (y and Y  are both alpha, but considered separately) */ char getMostFrequentAlphaChar(char array[], int arraySize);  Your main function should provide evidence of adequate testing performed for each of your functions.

Explanation / Answer

Please find my working code.

#include<iostream>
#include <cstring>
#define ASCII_SIZE 256
using namespace std;

char getMostFrequentChar(char charArray[], int arraySize)
{
    // Create array to keep the count of individual
    // characters and initialize the array as 0
    int count[ASCII_SIZE] = {0};

    // Construct character count array from the input
    // charArraying.
    for (int i=0; i<arraySize; i++)
        count[charArray[i]]++;

    int max = -1; // Initialize max count
    char result;   // Initialize result

    // Traversing through the charArraying and maintaining
    // the count of each character
    for (int i = 0; i < arraySize; i++) {
        if (max < count[charArray[i]]) {
            max = count[charArray[i]];
            result = charArray[i];
        }
    }

    return result;
}

char getMostFrequentAlphaChar(char charArray[], int arraySize)
{
    // Create array to keep the count of individual
    // characters and initialize the array as 0
    int count[ASCII_SIZE] = {0};

    // Construct character count array from the input
    // charArraying.
    for (int i=0; i<arraySize; i++){
        if(isalpha(charArray[i])) // only consedring alpha characters
            count[charArray[i]]++;
    }

    int max = -1; // Initialize max count
    char result;   // Initialize result

    // Traversing through the charArraying and maintaining
    // the count of each character
    for (int i = 0; i < arraySize; i++) {
        if (max < count[charArray[i]]) {
            max = count[charArray[i]];
            result = charArray[i];
        }
    }

    return result;
}

// Driver program to test the above function
int main()
{
    char charArray[] = "sample charArraying";

    int len = strlen(charArray);

    cout << "Max occurring character is "
         << getMostFrequentChar(charArray, len)<<endl;
    cout << "Max occurring alpha character is "
         << getMostFrequentAlphaChar(charArray, len)<<endl;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote