Write a complete C program that should be able to read ten alphabets between A, B and C from users. The program should then display a list of all entered alphabets and the total number of each alphabet. You are required to write the program by applying the modular programming concept as have been taught in the class. A list (an array) to store 10 characters should be created in the main function and as part of the program, you are required to:
a) Define a function named readAlpha() to read all the 10 alphabets from the user.
b) Define a function named listAlpha() to display all the entered alphabets as a list.
c) Define a function named findTotal() to count and display the total number for each alphabet entered. Also determine which alphabet has the highest number of hit then return the result to the main function and print the result from the main function.
#include <stdio.h>
#include <string.h>
void readAlpha(char alphas[]);
void listAlpha(char alphas[]);
char findTotal(char alphas[]);
int main(){
// A list (an array) to store 10 characters should be created in the main function and as part of the program
char alphas[10];
readAlpha(alphas);
listAlpha(alphas);
printf("\nAlphabet with the highest number of hit is %c", findTotal(alphas));
getchar();
getchar();
return 0;
}
//A function named readAlpha() to read all the 10 alphabets from the user.
void readAlpha(char alphas[]){
int i;
//read ten alphabets between A, B and C from users.
for(i=0;i<10;i++){
alphas[i]=' ';
while(alphas[i]!='A' && alphas[i]!='B' && alphas[i]!='C'){
printf("Enter an alphabet (A,B or C) %d: ",(i+1));
scanf("%c",&alphas[i]);
fflush(stdin);
}
}
}
//A function named listAlpha() to display all the entered alphabets as a list.
void listAlpha(char alphas[]){
int i;
printf("\nAll the entered alphabets: ");
for(i=0;i<10;i++){
printf("%c ",alphas[i]);
}
}
//A function named findTotal() to count and display the total number for each alphabet entered.
char findTotal(char alphas[]){
int i;
int totalAlphabets[3];
int highestNumberHit;
for(i=0;i<3;i++){
totalAlphabets[i]=0;
}
for(i=0;i<10;i++){
if(alphas[i]=='A'){
totalAlphabets[0]++;
}
if(alphas[i]=='B'){
totalAlphabets[1]++;
}
if(alphas[i]=='C'){
totalAlphabets[2]++;
}
}
printf("\n\nTotal alphabet A: %d\n",totalAlphabets[0]);
printf("Total alphabet B: %d\n",totalAlphabets[1]);
printf("Total alphabet C: %d\n",totalAlphabets[2]);
//Also determine which alphabet has the highest number of hit then return the result
//to the main function and print the result from the main function.
highestNumberHit=totalAlphabets[0];
for(i=1;i<3;i++){
if(totalAlphabets[i]>highestNumberHit){
highestNumberHit=totalAlphabets[i];
}
}
if(totalAlphabets[0]==highestNumberHit){
return 'A';
}
if(totalAlphabets[1]==highestNumberHit){
return 'B';
}
return 'C';
}
Comments
Leave a comment