Homework 3: Count the Digits
Please review the rules on collaboration for homework for this course, as well as the guidelines for turning in work.
This assignment is designed to give you practice with reading input from the keyboard and using arrays. This program will calculate the frequency of each digit between 0 and 9 that is found in a message read from the keyboard, display the frequency for each digit and note which digit appeared most often.
Your program should read in a string of characters (consisting of both letters and digits) from the keyboard. You may assume that the input will consist of at least one character but be no longer than 100 characters. So, assuming x is the number of characters in the message, you can assume that 1 <= x <= 100. Assume that there will not be any spaces or control characters in the message.
Your program should examine each character in the message, and if it is a digit, it should increase the frequency of that digit by one.
See the "Grading" section below for specific criteria for this assignment. As with all homework, the style guidelines apply to this assignment, and points may be deducted for poor style, readability, and testing.
Learning outcomes
Upon completion of this assignment, students will demonstrate understanding of the following learning outcomes:
1. In the context of low-level, systems programming:- Author effectful programs using fundamental imperative operations, e.g., sequenced statements, conditionals, and loops.
- Author small, memory-safe programs that interact with the world (i.e., produce side-effects).
- Design a program by identifying relevant preconditions, postconditions, and invariants of its state.
- Navigate the file system within a CLI.
- View and edit files using a standard Unix text editor (e.g., Emacs or Vim) within a CLI.
- Author a C program involving static types, the pre-processor, mutable variables, functions, loops, and structs.
- Author a C program involving IO and dynamically allocated memory.
The Task
Your program should follow these requirements:
- Read in the message from the keyboard. It will be at least one character and less than 100. You should read until a newline is encountered.
- Once a newline ('\n') is encountered, your program should print the frequency of each digit between 0 and 9.
- Your program should then indicate which digit appeared most frequently. If there is a tie, the program should print out all digits that occured the same, maximum number of times.
- Your program should ignore letters.
- Use an array of integers to track the frequency of each digit in the message
For example:
If I enter the message "a12sd34fg5f4", the program should print out a string of numbers, separated by a space, that indicate the frequency of each digit between 0 and 9 such as:
0 1 1 1 2 1 0 0 0 0
and print a statement such as:
4 appeared most frequently in the input message.
Hints
- I strongly suggest that you process the input string one character at a time, as it is read. You may use scanf() or getchar(). See page 139 in the King book for a way to do this using
scanf. Usinggetcharwould be similar, as you can see on page 140 of King. - Use the functions in
<ctype.h>to determine if a character is a digit or not. Information about this header is here.
Grading
Your testing should cover an appropriate range of "input" value combinations and your program's output should make it easy to assess correctness. I highly recommend for this (and all coding projects) that you make a testing plan before you start to write code. You do not need to plan to test all possible inputs to the program, but you should have a plan that will test major sections of your code and demonstrate your problem solving skills. Include this plan when you submit your code and testing transcript.
Upload your .c file, your testing plan (in PDF form please), and testing transcript to Gradescope. Your .c file should compile on mathlan without error using clang. You should not use any header files beyond those discussed on this page.
This homework is worth 25 points, based on the following criteria:
- [2 points] Gives a message to the user regarding the purpose of the program and expected input
- [3 points] Reads input from keyboard until the newline character is encountered
- [3 points] Correctly identifies digits in the input message
- [2 points] Uses an array of integers to track frequency of each digit in the message
- [1 points] Informs the user of the meaning of the output
- [3 points] Correctly counts the frequency of each digit in the message
- [3 points] Outputs the frequency of each digit from 0 to 9 that appeared in the message
- [3 points] Correctly identifies the most frequently appearing digit(s) inthe message
- [5 points] Testing Plan and Testing Transcript are included. Tests both messages that have a single, most frequent digit and multiple digits that occur with the same frequency.