Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What are the description of approach or technique used in this? #include <stdio.

ID: 3874645 • Letter: W

Question

What are the description of approach or technique used in this?

#include <stdio.h>

long charCount=0;

long digitCount=0;

long lineCount=0;

long wordCount=0;

long digitFreq[10];

int c=0;

int outOfWord=0;

int state=0;

int inWord=1;

void printStats();

void totalWords();

void totalLines();

void digitFrequency();

int main(int argc,char **argv)

{

int i;

for(i=0;i<10;i++)

{

digitFreq[i]=0;

}

state=outOfWord;

c=getchar();

while((c !=EOF))

{

charCount++;

digitFrequency(c);

totalLines(c);

totalWords(c);

c=getchar();

}

printStats();

return 1;

}

void totalLines(int c)

{

if(c == ' ')

{

lineCount++;

}

}

void digitFrequency(int c)

{

if(c >='0'&& c<='9')

{

digitCount++;

digitFreq[c-'0']++;

}

}

void totalWords(int c)

{

if(c == ' ' || c == ' ' || c == ' ')

{

state=outOfWord;

}

else if(state == outOfWord)

{

state = inWord;

wordCount++;

}

}

void printStats()

{

printf("words: %ld chars: %ld lines: %ld ",wordCount,charCount,lineCount);

int i;

for(i=0;i<10;i++)

{

printf("digit %d: %ld ",i,digitFreq[i]);

}

}

Explanation / Answer

The porgram is about counting the number of characters,no of digits,no of lines,no of words and frquency of digits.

charCount is used to store no of characters

digitCount is used to store no of digits

lineCount is used to store no of lines

word count is used to store no of words

array digitFreq of size 10 is used to frquency of digits in the file.

OutOfWord and inWord is used to indicate that reading of one word completed.

Initially we didnt know howmany digits are there and what are their frequencies ofdigits so initializing that array with zero in all indexes

after that we are reading character by character form the file into c using getchar function

we will continue reading till the end of file using while loop.

Inside the while loop we have code to count statistics

Every thing we read is a character thats why first statement in while is incrementing teh character count

second statement we are calling digitFrequency(c)

in this function we are cheking whether teh character we red was in range o and 9 if it is then we are incrementing value in that particular index in that array

Third statement is calling function totalLines(c) to check whether the character we read was end of line or not.

in this function we are checking whether c is " " or not which indicates teh end of line if yes incrementing lineCount

fourth statement we are calling totalWords(c) to check whether we have completed reading a word or

not

a word will end if the character we read was a space or tab( ) or (end of line) if it is then incrementing word count.

and fifth statement id again reding chharacter from file and again loop repesta till end of file.

The technique here used is simple one. We are counting the statistics of a file by using simple basic things.

Example every thing in file is a character so incrementing charCount for every thing we read from file

and a line ends with so if the character we read is then it indicates that we have finished reading a line so incrementing line count

a word will end if the character we read was a space or tab( ) or (end of line) if it is then incrementing word count.

and final thing is counting the digits. Any number can be formed by numbers between 0 t0 9 . This the thing we are using in this code, if the character read from file is

in range 0 to 9 and we are incrementing that digit frequency and storing that in array

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote