Question

C Programming I am trying to also print the frequency and the occurrence of an input...

C Programming

I am trying to also print the frequency and the occurrence of an input text file. I got the occurrence to but cant get the frequency.

Formula for frequency is "Occurrence / total input count", this is the percentage of occurrence.

Can someone please help me to get the frequency to work.

Code:

int freq[26] = {0};

fgets(input1, 10000, (FILE*)MyFile);

for(i=0; i< strlen(input); i++) {

freq[input[i]-'a']++;

count++;

}

printf("Text count = %d", count);

printf("\n");

printf("Frequency of plain text\n");

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

char temp = 'a' + i;

printf("%c : %d : %f\n",temp,freq[i],(float)(freq[i]/count));

Results:

Text count = 868
Frequency of plain text
a : 64 : 0.000000
b : 11 : 0.000000
c : 37 : 0.000000
d : 31 : 0.000000
e : 109 : 0.000000
f : 15 : 0.000000
g : 13 : 0.000000
h : 30 : 0.000000
i : 68 : 0.000000
j : 2 : 0.000000
k : 6 : 0.000000
l : 37 : 0.000000
m : 18 : 0.000000
n : 61 : 0.000000
o : 56 : 0.000000
p : 21 : 0.000000
q : 3 : 0.000000
r : 54 : 0.000000
s : 64 : 0.000000
t : 77 : 0.000000
u : 44 : 0.000000
v : 12 : 0.000000
w : 11 : 0.000000
x : 1 : 0.000000
y : 22 : 0.000000
z : 1 : 0.000000

Homework Answers

Answer #1
  • Below is the detailed implementation of the above problem in C with code and output shown.
  • For better understanding please read the comments mentioned in the code.
  • The mistake in your code in printing the frequency is that the division is not resulting in floating number as the denominator have to be typecasted to float to make the integer division resulting in floating point number, you can see the code below for reference.
  • You can print the occurence and frequency as shown in the code and also you can read ultiple lines from your text file from the code below.
  • Note: keep the input file(input.txt) and the c file(.c) in the same directory so that the program can read the file.
  • CODE:

//include headers
#include<stdio.h>
#include<string.h>

//driver function
int main(){
//file pointer
FILE *MyFile;
//create frequency array
int freq[26] = {0};
//open file in reading mode
MyFile = fopen("input.txt" , "r");
//create a character array of size 10000
char input[10000];
//count stores total input count of letters
int count=0;
//read till EOF(end-of-file)
while(!feof(MyFile)){
//input text
fgets(input, 10000, (FILE*)MyFile);
//iterate through string
for(int i=0; i< strlen(input); i++){
//increment count of alphabets read
if(input[i]<='z' && input[i]>='a')
freq[input[i]-'a']++;
//increment total count
count++;
}
}
//print total text count
printf("Text count = %d", count);
//newline
printf("\n");
//heading
printf("Frequency of plain text\n");
//for all characters from a to z
for(int i=0;i<26;i++){
//current character
char temp = 'a' + i;
//print it's occurrence and frequency
printf("%c : %d : %f\n",temp,freq[i],(freq[i]/(float)count));
}
return 0;
}

  • INPUT/OUTPUT:

input file(input.txt)

abcdbdbdbdbdhasuhfsakdjajbfkdjgkjdngsithosietnsnskbsgbnsklgbnskbnslibnsbn
dnfbdsskjdfbsjbskjfbsjk

output

Text count = 97
Frequency of plain text
a : 4 : 0.041237
b : 16 : 0.164948
c : 1 : 0.010309
d : 11 : 0.113402
e : 1 : 0.010309
f : 5 : 0.051546
g : 4 : 0.041237
h : 3 : 0.030928
i : 3 : 0.030928
j : 8 : 0.082474
k : 9 : 0.092784
l : 2 : 0.020619
m : 0 : 0.000000
n : 9 : 0.092784
o : 1 : 0.010309
p : 0 : 0.000000
q : 0 : 0.000000
r : 0 : 0.000000
s : 16 : 0.164948
t : 2 : 0.020619
u : 1 : 0.010309
v : 0 : 0.000000
w : 0 : 0.000000
x : 0 : 0.000000
y : 0 : 0.000000
z : 0 : 0.000000

  • Below are the screenshot attached for the code and input/output for better clarity and understanding.

CODE

INPUT/OUTPUT

input.txt

output

So if you still have any doubt regarding this solution please feel free to ask it in the comment section below and if it is helpful then please upvote this solution, THANK YOU.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C Programming I have this function to i want to return the cipher text, but its...
C Programming I have this function to i want to return the cipher text, but its not working, can anyone try to see what i'm doing wrong. I need it to return the cipher text. char* vigenereCipher(char *plainText, char *k) { int i; char cipher; int cipherValue; int len = strlen(k); char *cipherText = (char *)malloc(sizeof(plainText) * sizeof(char)); //Loop through the length of the plain text string for (i = 0; i < strlen(plainText); i++) { //if the character is...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...
Please answer the following C question: There is a documented prototype for a function called get_a_line...
Please answer the following C question: There is a documented prototype for a function called get_a_line in the code below. Write a definition for get_a_line—the only function called from that definition should be fgetc. #include <stdio.h> #include <string.h> #define BUFFER_ARRAY_SIZE 10 int get_a_line(char *s, int size, FILE *stream); // Does what fgets does, using repeated calls to fgetc, but // provides a more useful return value than fgets does. // // REQUIRES // size > 1. // s points to...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
I am trying to write a program in C language but keep running into errors. Any...
I am trying to write a program in C language but keep running into errors. Any help would be awesome. here is my code I have so far. #include <stdio.h> #include <conio.h> #include <string.h> int main(){    int lenght, i = 0, state = 0;    char line[100];    printf("enter the string for checking of comment line: \n");    gets(line);    while(i < strline(line)){        switch(state){            case 0: if (line[i] == '/'){               ...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an executable with gcc -Wall -DUNIT_TESTS=1 array-utils5A.c The definitions for is_reverse_sorted and all_different are both defective. Rewrite the definitions so that they are correct. The definition for is_alternating is missing. Write a correct definition for that function, and add unit tests for it, using the unit tests for is_reverse_sorted and all_different as models. Please explain the logic errors present in in the definition of is_reverse_sorted...
can someone edit my c++ code where it will output to a file. I am currently...
can someone edit my c++ code where it will output to a file. I am currently using xcode. #include <iostream> #include <cctype> #include <cstring> #include <fstream> using namespace std; bool inputNum(int [],int&,istream&); void multiply(int[],int,int[],int,int[],int&); void print(int[],int,int,int); int main() {ifstream input; int num1[35],num2[35],len1,len2,num3[60],len3=10,i; input.open("multiplyV2.txt"); //open file if(input.fail()) //is it ok? { cout<<"file did not open please check it\n"; system("pause"); return 1; }    while(inputNum(num1,len1,input)) {inputNum(num2,len2,input); multiply(num1,len1,num2,len2,num3,len3); print(num1,len1,len3,1); print(num2,len2,len3,2); for(i=0;i<len3;i++) cout<<"-"; cout<<endl; print(num3,len3,len3,1); //cout<<len1<<" "<<len2<<" "<<len3<<endl; cout<<endl;    } system("pause"); } void...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...