Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am writing a program in c that prints out the most frequent letter and the most frequent digit from input. The program that I

I am writing a program in c that prints out the most frequent letter and the most frequent digit from input. The program that I have written below is working fine, I am having trouble with the output. The output should print out the most frequent digit and most frequent letter when they are entered. I am having trouble with the conditional statement for the output to show most frequent character only when letters are entered from input and not show the digits print statement. Also vice versa when digits are only entered I want the output to only show the most frequent digit and not the most frequent letter print statement. Basically I'm having trouble about what if, else condition I should use.

This is my code below:

#include #include

struct cf { char ch; /* ch is a character */ int fr; /* a number */ };

int process_data(char lc, struct cf cc[], int s); void ss(struct cf cc[], int s);

int main() { int sl = 0, sd = 0; struct cf letters[200]; struct cf digits[10]; char lc; int i, l;

/* Loop until end of file */ while ((lc = getchar()) != EOF) { if(isalpha(lc)){ l = process_data(lc, letters, sl); if (l >= 0) { letters[l].ch = lc; letters[l].fr++; /* Increment fr */ } else{ letters[sl].ch = lc; letters[sl].fr = l; sl++; }

}

else if(isdigit(lc)){ l = process_data(lc, digits, sd); if(l >= 0){ digits[l].ch = lc; digits[l].fr++; /* increment fr */ } else{ digits[sd].ch = lc; digits[sd].fr =l; sd++; }

} }

/* Call ss function */ ss(letters, sl); ss(digits, sd);

/* Print answer */ printf("Most frequent letter: %c ", letters[0].ch); printf("Most frequent digit: %c ", digits[0].ch);

return 0; }

int process_data(char lc, struct cf cc[], int s) { int i; for (i = 0; i < s; i++) if (cc[i].ch == lc) /* Compare cc[i].ch to lc */ return i; return -1; }

void ss(struct cf cc[], int s) { int f, i, x; struct cf X;

for (f = 0; f < s - 1; f++) { /* Get next x */ x = f; for (i = f + 1; i < s; i++) if (cc[i].fr > cc[x].fr) x = i; /* set x to i y = i - 1; */

/* Swap values */ X = cc[f]; cc[f] = cc[x]; cc[x] = X; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And SQL For DB2

Authors: James Cooper

1st Edition

1583473572, 978-1583473573

More Books

Students also viewed these Databases questions

Question

=+how might their legitimacy be improved?

Answered: 1 week ago

Question

How many multiples of 4 are there between 10 and 250?

Answered: 1 week ago