Create a program called arrays.c that does the following: The absolute frequency
ID: 3539697 • Letter: C
Question
Create a program called arrays.c that does the following:
The absolute frequency of a character is the number of times the character appears. For example, in the string "aaab" the absolute frequency of 'a' is 3, and the absolute frequency of 'b' is 1.
The relative frequency of a character is the absolute frequency divided by the total number of characters. For example, in the string "aaab" the relative frequency of 'a' is 3/4 = 0.75, and the relative frequency of 'b' is 1/4 = 0.25. Relative frequency * 100 will give you the result in percentage
Explanation / Answer
#include <stdio.h>
#include <string.h>
int main()
{
char string1[254];
char string2[254];
char str[254];
char string3[100];
int i,k=0,len;
int a[254];
printf(" Program: String Processing ");
printf(" Author: Doug Jones");
printf(" Enter string 1: ");
gets(string1);
printf("Enter string 2: ");
gets(string2);
printf("Enter string 3: ");
gets(string3);
printf(" String 1 is %d bytes long, and String 2 is %d bytes long",strlen(string1)-1,strlen(string2)-1);
for(i=0; i<254; i++)
a[i]=0;
for(i=0; i<strlen(string1)/2; i++)
{
str[k++] = string1[i];
}
for(i=strlen(string2)/2-1; string2[i]!=''; i++)
{
str[k++] = string2[i];
}
printf(" String 3 is: %s ",str);
printf(" FREQUENCY TABLE");
printf(" ---------------");
printf(" Char Count %c of Total",37);
printf(" ---- ----- ----------");
len = strlen(string3)-1;
printf(" ALL %d 100.00%c",len,37);
for(i=0; string3[i]!=''; i++)
{
if(string3[i]!=' ')
a[string3[i]]++;
}
for(i=0; i<254; i++)
{
if(a[i]!=0)
{
printf(" ");
printf(""%c" %d %.2f%c",i,a[i],((float)(a[i])/(float)len)*100,37);
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.