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

Analyse the input/output and write a hand example to prepare for the solutions t

ID: 3773689 • Letter: A

Question

Analyse the input/output and write a hand example to prepare for the solutions that solve the following problem. Then, write a complete C program. Create a one-dimension 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 inputted). As each letter is entered, print it only if it has been entered before. 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 time it was entered. Prepare for the case where all 20 letters are different, or all are the same. Use the smallest possible variables and minimum array size to solve the problem.

Explanation / Answer

#include <stdio.h>
#include <conio.h>
#include <string.h>

bool checkDuplicate(char arr[],int n, char ch)
{
   bool duplicates = false;
   int i=0;
   for(i = 0; i < n-1; i++)
   {
       if (arr[i] == ch)
       {
           duplicates = true;
           break;
       }
   }
   return duplicates;
}
void reorder(int n,char x[])
{
   int i,j;

   char t;
   for(i=0;i<n-1;i++)
   {
       for(j=i+1;j<n;j++)
       {
           if((x[i]>x[j]))
           {
               t=x[j];
               x[j]=x[i];
               x[i]=t;
           }
       }  
   }
   return;
}

void mostFrequentLetter(char arr[])
{
   int count = 1, tempCount;
   char frequentLetter = arr[0];
   char tempChar ;
   for (int i = 0; i < 19; i++)
   {
       tempChar = arr[i];
       tempCount = 0;
       for (int j = 1; j <20; j++)
       {
           if (tempChar ==arr[j])
               tempCount++;
       }
       if (tempCount > count)
       {
           frequentLetter = tempChar;
           count = tempCount;
       }
   }

   printf(" %c is most frequent letter- %d", frequentLetter,tempCount);
}


int main()
{
   char arr[20],ch;
   bool Duplicateflag=false;
   int i=0;
   //Read values until 20 char
   while (((ch = getchar()) != EOF) && i<20)
   {
       if(ch!=' ')
       {
           if(!( (ch>='a'&& ch<='z') || (ch>='A' && ch<='Z')))
           {
               printf(" %c is an non-alphabet.",ch);
           }
           else
           {
               arr[i]=ch;
               i++;
               bool duplicate= checkDuplicate(arr,i,ch);
               if(duplicate)
               {
                   printf(" Alert-Duplicate value %c is entered. ",ch);
                   Duplicateflag=true;
               }
           }
       }
   }
   reorder(20, arr);
   printf(" Alphabets in order is:");
   printf(" *************** ");
   for(i=0;i<20;i++)
   {
       printf("%d - %c ",i+1,arr[i]);
   }
   printf(" *************** ");

   mostFrequentLetter(arr);
   if(Duplicateflag==false)
   {
       printf(" Entered different characters in the array.");
   }
   else
   {
       printf(" Entered some non different characters in the array.");
   }
   getch();

}

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