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
The code is in C programming language pls convert it into python. Thanks. Program --> #include...
The code is in C programming language pls convert it into python. Thanks. Program --> #include <stdio.h> #include <stdlib.h> void main() { //declare variables FILE *fileptr; char filename[15]; char charRead; char filedata[200],searchString[50]; int i=0,j=0,countNoOfWord=0,count=0; //enter the filename to be opened printf("Enter the filename to be opened \n"); scanf("%s", filename); /* open the file for reading */ fileptr = fopen(filename, "r"); //check file exit if (fileptr == NULL) { printf("Cannot open file \n"); exit(0); } charRead = fgetc(fileptr); //read the string...
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 PROGRAMMING: HOW TO INSERT ERROR BELOW CODE? QUESTION: This program reads integers from standard input....
/*C PROGRAMMING: HOW TO INSERT ERROR BELOW CODE? QUESTION: This program reads integers from standard input. The first integer indicates the number of values that will follow. Read that many values, and return their sum, ignoring any additional values that may follow. However, if there are fewer integers than specified in the input, print "Error" and terminate. Hint: int n, success; ... success = scanf("%d", &n); // FIRST INTEGER INPUT reads an integer from stdin, returning 1 if the integer...
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()...
USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is...
USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is one of the simplest encryption techniques in the field of cryptography. It is a cipher in which each letter in a plain text message is replaced by a letter some fixed number of positions up the alphabet (i.e., by right shifting the alphabetic characters in the plain text message). For example, with a right shift of 2, ’A’ is replaced by ’C’, ’B’ is...
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...
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...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT