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

Create a one-dimensional array to read 20 alphabetical letters (your program sho

ID: 3751448 • Letter: C

Question

Create a one-dimensional array to read 20 alphabetical letters (your program should be able to detect and print out an error message if a non-alphabetical letter is entered). As each letter is entered, print a message saying ‘duplicate letter’ if the letter is already in the array. Write a function that can sort the array after all 20 letters have been entered. Write another function that print out the most frequent letter and number of times it was entered. Prepare for the case where all 20 letters are different, or all are the same

Explanation / Answer

Since you have not provided the language of your preference, I am providing the code in C++.

CODE

======================

#include <iostream>

using namespace std;

void insertionSort(char *arr, int n)

{

int i, key, j;

for (i = 1; i < n; i++)

{

key = arr[i];

j = i-1;

/* Move elements of arr[0..i-1], that are

greater than key, to one position ahead

of their current position */

while (j >= 0 && arr[j] > key)

{

arr[j+1] = arr[j];

j = j-1;

}

arr[j+1] = key;

}

}

void mostFrequent(char arr[], int n)

{

// Sort the array

insertionSort(arr, n);

// find the max frequency using linear traversal

int max_count = 1, res = arr[0], curr_count = 1;

for (int i = 1; i < n; i++) {

if (arr[i] == arr[i - 1])

curr_count++;

else {

if (curr_count > max_count) {

max_count = curr_count;

res = arr[i - 1];

}

curr_count = 1;

}

}

// If last element is most frequent

if (curr_count > max_count)

{

max_count = curr_count;

res = arr[n - 1];

}

cout << "The most frequent character is: " << res << ", count = " << max_count << endl;

}

bool exists(char arr[], int n, char ch) {

for (int i=0; i<n; i++) {

if (arr[i] == ch)

return true;

}

return false;

}

int main() {

int i = 0;

int n = 20;

char arr[n];

while (i < n) {

char ch;

cout << "Enter a character : ";

cin >> ch;

if(exists(arr, n, ch) == true) {

cout << "Duplicate Letter!!" << endl;

continue;

}

arr[i++] = ch;

}

cout << "The array is : " << endl;

for(int i=0; i<n; i++) {

cout << arr[i] << " ";

}

insertionSort(arr, n);

cout << " Th array after sorting : " << endl;

for(int i=0; i<n; i++) {

cout << arr[i] << " ";

}

mostFrequent(arr, n);

}

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