Due: Monday, October 21, 2024, at 11:59pm
Goals
Synopsis
Set-Up
Tech Requirements
Code Quality
Hints
Assessment
Turn-in instructions
The purpose of this assignment is to gain some experience with C programming. In particular, in this assignment, you will:
gdb
, andThis project should be done independently. If you work with a classmate make sure you are each editing and working on your own set of files. You should not copy and paste code.
The material that we have learned in lecture is not enough to complete this assignment. It is expected that you will investigate the resources and libraries mentioned in this document to learn about how to use them.
In this assignment you will create your own version of the unix command wc
. You will read in a file and report stats including the number of lines and number of words in the file. Your code should behave as follows:
gcc -Wall -std=c11 -o wordcount wordcount.c
wc
on any test file - the results should be the same.wc
, but should match the example response to wordcount
precisely. (See example below.)-l, -w, -c
Example Operation:
$ ./wordcount -l Usage: ./wordcount requires an input file. $ echo $? 1 $ ./wordcount point.c "NON FILE" shorttext 73 247 1574 point.c NON FILE will not open. Skipping 4 13 68 shorttext Total Lines = 77 $ wc point.c "NON FILE" shorttext 73 247 1574 point.c wc: 'NON FILE': No such file or directory 4 13 68 shorttext 77 260 1642 total $ ./wordcount -l point.c 73 $ ./wordcount -c point.c shorttext 1574 68 $ ./wordcount -l -wc shorttext -wc will not open. Skipping 4
Before you get started, ensure that your set-up is up-to-date and appropriate.
cancun
, including gcc
cse374-materials
as an upstream repository. This will
allow you to pull updates of homework materials. The folder for this assignment includes
a test file.
git remote add upstream git@gitlab.cs.washington.edu:mh75/cse374-materials.git
. Do not replace the mh75
with your own user id, but use this command exactly.origin
. Additionally, you have a remote upstream repository that is pointing the the cse374-materials repository. You can verify this with the command git remote -v
.
[mh75@cancun mh75repo]$ git remote -v origin git@gitlab.cs.washington.edu:cse374-24au-students/mh75.git (fetch) origin git@gitlab.cs.washington.edu:cse374-24au-students/mh75.git (push) upstream git@gitlab.cs.washington.edu:mh75/cse374-materials.git (fetch) upstream git@gitlab.cs.washington.edu:mh75/cse374-materials.git (push)
git config pull.rebase false
(Using pull.rebase false
. (You may need to resolve merge conflicts.)git pull upstream main --allow-unrelated-histories --no-edit
push
You now have a git repository, with an upstream repository that allows you to easily pull any starter code for assignments. For the remainder of the course you should work within this repository. You should get into the habit of committing and pushing code frequently, and you should always commit any changes you have made before pulling from the upstream repository.
You should pay attention to the following guidelines for meeting performance expectations.
strncpy
in <string.h>
can be used to copy \0
-terminated strings; you should
not be writing loops to copy such strings one character at a
time.fgets
and strncpy
instead of routines
like gets
and strcpy
. The safe
functions allow specification of maximum buffer or array lengths
and will not overrun adjacent memory if used properly.stderr
and continue processing any remaining files on the command
line.wordcount.c
. You
should arrange your code so that related
functions are grouped together in a logical order in the file.cancun
using gcc
with the -Wall
and -std=c11
options. Since this assignment should
not need to use any unusual or system-dependent code you can
probably develop and test your code on any recent Linux
system or other system that supports a standard C
compiler. However, you should verify your program before the
submission deadline.stderr
and
exit with an exit code of EXIT_FAILURE
(defined in
<stdlib.h>
-- see the description of the
exit()
function).EXIT_SUCCESS
(see <stdlib.h>
).
This is normally done by returning this value as the int
result of the main
function.As with any program you write, your code should be readable and understandable to anyone who knows C. In particular, for full credit your code must observe the following requirements.
main
function
that does everything. However it should not contain tiny functions
that only contain isolated statements or code fragments instead of
dividing the program into coherent pieces. Be sure to
include appropriate function prototype declarations near the
beginning of the file so the actual function definitions can
appear in whatever order is most appropriate for presenting the
code in the remainder of the file in a logical sequence and so that
related functions are grouped together.void
functions that perform an action without
returning a value. Variables of local significance like loop
counters, indices, or pointers should be given simple names
like i
,
or p
, and often do not require further comments.int
s,
pointers, and similar things are cheap; copies of arrays and large
structs are expensive.) Don't read the input by calling a library
function to read each individual character. Read the input a line
at a time (it costs just about the same to call an I/O function to
read an entire line into a char array as it does to read a single
character). Your code should be simple and
clear, not complex containing lots of micro-optimizations that
don't matter.wget
and
chmod +x
to make it
executable if needed)../cpplint.py --clint wordcount.c
to review your code. whereis python
. In some cases you must call python3 explicitely: python3 ./cpplint.py --clint wordcount.c
, or modify the first line of cpplint.py to point to your system's python installation.).emacs
file in your
home directory to ensure that emacs translates leading tabs to
spaces:(setq-default indent-tabs-mode nil)
.
stdout
before
adding another step. HINT: We have a sample program from an earlier lecture that does this.spellcheck
script: You first check for usage
(is the command called correctly?), and then, for each input
file you perform a task. You write similar error messages, and exit
with a failure code under similar circumstances.<stdio.h>
,
<string.h>
, <ctype.h>
and <stdlib.h>
libraries. Use the
cplusplus reference
link on the course home page to look up details about functions
and their parameters; use a good book like
The C Programming Language for background and perspective
about how they are intended to be used.
strlen
tells you how many characters are in a string.-Wall
option. Don't waste time
searching for errors that the compiler or run-time tests could have
caught for you.This assignment is worth 50 points
Your code will be evaluated by an auto-grader that will check specified performance criteria. There will be some additional manual grading that will look at the points brought up in the Code Quality requirements above (specifically looking at commenting and code efficiency).
You will submit this homework to the Gradescope HW3: Wordcount assignment, via Gitlab.
You will first update your Gitlab repository. Ensure that your wordcount.c
file
is located in the hw3
directory, which is at the top level of your repository.
Use git add
, git commit
, and git push
to bring the
repository up-to-date.
Once you locate the Gitlab assignment you will tap the "GitLab" button on the bottom:
Once you submit your code the autograder may take some time to run. You may resubmit your code to address any deductions, but, you may only resubmit 5 times per hour. You should do as much testing as possible on your own platform before resubmitting.