Price: $20 USD

Question description

Problem 2. This program provides practice with global and static variables. In real-life program- ming you may choose to define these variables differently.

Write a program statistics.c that takes as command-line arguments a sequence of words, and then asks the user for the name of an output file. The program prints in the file the lowercase form of each word, its length, and the cumulative length so far. After doing that for all words, the program prints each letter of the alphabet that was included in these words followed by the number of times the letter appeared. You may assume that the user will type only letters (no punctuations or numbers).

( ̃)$ a.out Peter Piper picked a peck 

Output file: output1

( ̃)$ more output1

peter: length=5 total=5

piper: length=5 total=10
picked: length=6 total=16
a: length=1 total=17
peck: length=4 total=21
Letters: a:1 c:2 d:1 e:5 i:2 k:2 p:5 r:2 t:1

 ( ̃)$ a.out aBrAcAdAbRa

Output file: output2( ̃)$ 

more output2

abracadabra: length=11 total=11 

Letters: a:5 b:2 c:1 d:1 r:2

 ( ̃)$ a.outOutput file: output3

( ̃)$ more output3


( ̃)$

The program should use the following variables:

  • int letterFrequencies[26] – a global array that tracks the number of times each letter has appeared so far. It will be used by the functions wordStats and printFrequencies. This variable is already defined for you in myFunctions.c

  • static int cumulativeLength–astaticvariabledefinedinsidethefunctionwordStats that tracks the cumulative length of words so far.

    The program should include the following functions. Of these functions, main should be defined in statistics.c and all other functions should be defined in myFunctions.c

    • void lowercaseString(char string[])—Convertsallupper-caselettersinstring to lower case. You can use the same-named function you wrote for the previous homework.

    • void wordStats(FILE *fp, char string[])

      – Prints everything below to the file pointed to by fp. – Prints string.


  • –  Printsthelengthofstring.YoucandeterminethelengthbyusingthefunctionstringLen you wrote for the last homework or the function strlen defined in string.h.

  • –  Updates and prints the static variable cumulativeLength (see above) that tracks the cumulative word lengths so far.

  • –  UpdatesaglobalarrayletterFrequencies(seeabove)thattracksthenumberoftimes each letter has appeared among the words processed so far.

    • void printFrequencies(FILE *fp)

– –

• int –

Prints everything below to the output file pointed to by fp.
Checks the global array letterFrequencies, and for each letter from a to z that ap-

peared at least once, prints the number of times it appeared.

main(int argc, char *argv[])

Asks the user for the name of an output file, and opens the file.

For each word in the command line, calls lowercaseString to convert uppercase letter to lower, and then wordStats to print the word and cumulative lengths in the file, and to update the number of times each letter appears.

After all words are processed, calls printFrequencies to print the number of time each seen letter has appeared. 

Tutor Answer

(Top Tutor) Daniel C.
School: UC Berkeley
Studypool has helped 1,244,100 students
Ask your homework questions. Receive quality answers!

Type your question here (or upload an image)

1826 tutors are online

Brown University

1271 Tutors

California Institute of Technology

2131 Tutors

Carnegie Mellon University

982 Tutors

Columbia University

1256 Tutors

Dartmouth University

2113 Tutors

Emory University

2279 Tutors

Harvard University

599 Tutors

Massachusetts Institute of Technology

2319 Tutors

New York University

1645 Tutors

Notre Dam University

1911 Tutors

Oklahoma University

2122 Tutors

Pennsylvania State University

932 Tutors

Princeton University

1211 Tutors

Stanford University

983 Tutors

University of California

1282 Tutors

Oxford University

123 Tutors

Yale University

2325 Tutors