{"id":3772,"date":"2018-02-20T23:50:30","date_gmt":"2018-02-20T23:50:30","guid":{"rendered":"http:\/\/essaysparlor.com\/non-printable-characters\/"},"modified":"2017-02-20T18:38:58","modified_gmt":"2017-02-20T18:38:58","slug":"non-printable-characters","status":"publish","type":"post","link":"https:\/\/www.benedictsol.com\/blogs\/non-printable-characters\/","title":{"rendered":"Non-printable characters"},"content":{"rendered":"<p>\nCOMP 2103X1 Assignment 2<br \/>\nGeneral information about assignments (important!):<br \/>\nhttp:\/\/cs.acadiau.ca\/~jdiamond\/comp2103\/assignments\/General-info.html<br \/>\nInformation on passing in assignments:<br \/>\nhttp:\/\/cs.acadiau.ca\/~jdiamond\/comp2103\/assignments\/Pass-in-info.html<br \/>\nInformation on coding style:<br \/>\nhttp:\/\/cs.acadiau.ca\/~jdiamond\/comp2103\/assignments\/C-coding-style-notes<br \/>\n[1] A filter program is a program which reads its input from ?standard input? (?stdin?) and writes<br \/>\nits output to ?standard output? (?stdout?). Filter programs are useful because they make it easy<br \/>\nto combine the functions they provide to solve more complex problems using the standard shell<br \/>\nfacilities. Filter programs are also nice to write, because the programmer doesn?t have to worry<br \/>\nabout writing code to open and close files, nor does the programmer have to worry about dealing<br \/>\nwith related error conditions. In some respects, filter programs are truly ?win-win?.<br \/>\nWrite a filter program which uses getchar() to read in characters from stdin, continuing until<br \/>\nend of file (read the man page and\/or textbook to see the details on getchar(), or, heaven forbid,<br \/>\nreview the class slides). Your program must count the number of occurrences of each character in<br \/>\nthe input. After having read all of the input, it outputs a table similar to the one below which, for<br \/>\neach character seen at least once, lists the total number of times that character was seen as well as<br \/>\nits relative frequency (expressed as a percentage). Note that the characters n, r, t, \u0000, a, b,<br \/>\nf, and v (see man ascii) must be displayed with the appropriate ?escape sequence?. Ordinary<br \/>\nprintable characters must be output as themselves. Non-printable characters (see man isprint)<br \/>\nmust be printed with their three-digit octal code (see man printf).<br \/>\nYou can get input into a filter program (a2p1 in this case) in three ways:<br \/>\n(a) ?pipe? data from another program into it, like<br \/>\n$ echo blah blah | a2p1<br \/>\n(b) ?redirect? the contents of a file into the program, like<br \/>\n$ a2p1 < some-file<br \/>\n(c) type at the keyboard, and (eventually) type ^D (control-d) at the beginning of a line to<br \/>\nsignify end of file.<br \/>\nYour output must look like the following, for this sample case:<br \/>\n$ echo ^Aboo | a2p1<br \/>\nChar Count Frequency<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n001 1 20.00%<br \/>\nn 1 20.00%<br \/>\nb 1 20.00%<br \/>\no 2 40.00%<br \/>\nNote: in the above examples, and from now on in this course?s assignments, text in red is text that<br \/>\nthe human types, and a ?$&#8221; at the beginning of a line like that represents the shell prompt.<br \/>\n1<br \/>\nNote that I entered a ^A (control-a, not the circumflex character followed by the capital A) by<br \/>\ntyping ^V^A. The ^V tells your shell that you want it to interpret the next character literally, rather<br \/>\nthan to use any special meaning (during command line entry) that the next character might normally<br \/>\nhave. (Question: what does your shell do, when typing in a command line, if you type ^A without<br \/>\nfirst entering ^V? Try it and see if you can figure it out. You might find it useful to know this, and<br \/>\nto know what ^E and ^W do as well.)<br \/>\nYou should run your program on a few different inputs to demonstrate to the marker that you have<br \/>\nthought about (and programmed for!) the different cases that could occur. If you redirect input<br \/>\nfrom a data file, rather than using ?cat? to display the contents of your file when creating your<br \/>\ntranscript file, use a command like $ od -bc test-data-1 .<br \/>\n[2] The printf() function in C is very powerful and convenient, but it takes some getting used to.<br \/>\nThis question will give you experience with this function.<br \/>\nWhen you are testing functions like printf() which produce formatted output, sometimes you<br \/>\nwant to make sure that the spaces in the output are the ones you expect. To make it obvious where<br \/>\nall the white space came from, it is often convenient to enclose a format specification inside a pair<br \/>\nof characters that are not otherwise used in that output. For example, if you use something like<br \/>\nprintf(&#8220;|%f|n&#8221;, x)<br \/>\nthen you will know whether the %f format specification produced any spaces before the newline.<br \/>\nThe constant M_PI, an approximation to the value of \u0019, is defined in the system include file math.h.<br \/>\nWrite a program which prints out the value of M_PI using each of the following specifications, one<br \/>\nspecification per line of output:<br \/>\n(a) fixed point notation, field width of 9, 5 digits of precision, left justified<br \/>\n(b) fixed point notation, field width of 9, 5 digits of precision, right justified<br \/>\n(c) fixed point notation, field width of 9, no precision specification, right justified<br \/>\n(d) fixed point notation, field width unspecified, 5 digits of precision, right justified<br \/>\n(e) scientific notation, field width 9, 5 digits of precision, right justified<br \/>\n(f) scientific notation, field width 9, 5 digits of precision, left justified<br \/>\n(g) scientific notation, field width 14, 5 digits of precision, right justified<br \/>\n(h) scientific notation, field width 14, 5 digits of precision, left justified<br \/>\nAs an example of what your output might look like, here is one sample line of output:<br \/>\n|3.14159 | is field 9, precision 5, left justified<br \/>\nExamine your output and try to understand what the printf() function is doing, especially any<br \/>\noutputs that you find surprising.<br \/>\n2<br \/>\n[3] These days, many people are very concerned with the protection of private information. This<br \/>\nprogram will do a very rudimentary form of encryption. (Don?t use this for anything you want to<br \/>\nkeep secret!)<br \/>\nWrite a filter program to encrypt standard input as follows:<br \/>\n(i) upper case letters between ?A? and ?M? are replaced with the lower case letter 13 positions<br \/>\nfurther along in the alphabet (e.g., ?B? is replaced with ?o?);<br \/>\n(ii) upper case letters between ?N? and ?Z? are replaced with the lower case letter 13 positions<br \/>\nearlier in the alphabet (e.g., ?Z? is replaced with ?m?);<br \/>\n(iii) lower case letters between ?a? and ?m? are replaced with the upper case letter 13 positions<br \/>\nfurther along in the alphabet (e.g., ?d? is replaced with ?Q?);<br \/>\n(iv) lower case letters between ?n? and ?z? are replaced with the upper case letter 13 positions<br \/>\nearlier in the alphabet (e.g., ?y? is replaced with ?L?);<br \/>\n(v) the four punctuation characters ?.?, ?,?, ?!? and ??? are replaced with, respectively, ?!?, ???, ?.?<br \/>\nand ?,?.<br \/>\nOf course, an encryption program is no use without a corresponding decryption program. A bit of<br \/>\nthought (or experimentation) should show that this program will decrypt its own output.<br \/>\nTo test your program, create a few text files; some small, some big. Then run some tests which will<br \/>\nconvince the marker of the following things:<br \/>\n(i) the output of the program looks different than the input; and<br \/>\n(ii) running the output of the program through the program a second time recovers the original<br \/>\ndata.<br \/>\nYou can make use of the Unix utilities diff and\/or cmp, as well as cat, to help convince the<br \/>\nmarker that your program works. Here is a sample run with a small number of tests. Note that you<br \/>\ndon?t need to cat big files into the script file, but you can use ls -l or wc to show the marker<br \/>\nthat the files were big.<br \/>\n$ <cat a2p3.c, show gcc step, ...><br \/>\n$<br \/>\n$ cat test1.dat<br \/>\nThis is a short file.<br \/>\nIs it long enough, or should it be longer?<br \/>\n$<br \/>\n$ a3p1 < test1.dat<br \/>\ngUVF VF N FUBEG SVYR!<br \/>\nvF VG YBAT RABHTU? BE FUBHYQ VG OR YBATRE,<br \/>\n$<br \/>\n$ a3p1 < test1.dat | a3p1<br \/>\nThis is a short file.<br \/>\nIs it long enough, or should it be longer?<br \/>\n$<br \/>\n$ wc test2.dat<br \/>\n1241 3394 44616 test2.dat<br \/>\n$ a3p1 < test2.dat > test2.dat.crypt<br \/>\n3<br \/>\n$<br \/>\n$ wc test2.dat.crypt<br \/>\n1241 3394 44616 test2.dat.crypt<br \/>\n$<br \/>\n$ cmp test2.dat test2.dat.crypt<br \/>\ntest2.dat test2.dat.crypt differ: byte 3, line 1<br \/>\n$<br \/>\n$ a3p1 < test2.dat.crypt > test2.dat.crypt.decrypt<br \/>\n$<br \/>\n$ cmp test2.dat test2.dat.crypt.decrypt<br \/>\n$<br \/>\nNotice that, as is common for Unix programs, cmp says nothing in the ?success? case, which for<br \/>\ncmp is when the two files are identical.<br \/>\nThe third (red) command entered above is interesting. It shows how the Unix shell (command<br \/>\ninterpreter) allows you to send the output of one program (the first a2p3) into the input to another<br \/>\nprogram (in this case the second a2p3). This is an extremely powerful feature of the shell,<br \/>\nespecially since your program does not have to do anything special to make this happen; as far as<br \/>\nyour program is concerned, it is reading from standard input and writing to standard output. The<br \/>\nfact that those may be the keyboard, screen, a file or another program generally don?t matter to<br \/>\nyour program. (In fact, the occasional program does care, but that is an uncommon circumstance.)<br \/>\nIn the partial script above I showed a test with a short file, and a long file. I showed that (some)<br \/>\nletters got encrypted as they should, and that punctuation is properly encrypted. I also showed that<br \/>\nmy program works with a fairly long file, but you might want to try a bigger file yet. And you<br \/>\nmight consider using the head program to output just the first few lines of a very long file to your<br \/>\nscript file.<br \/>\nShould anything else be tested? Are there any boundary cases here?<br \/>\nDid you use functions in any of these questions? Should you have? Did you document them<br \/>\ncorrectly?<br \/>\nDoes you program ?blow up? on unexpected input, or does it deal with bad input in a ?graceful?<br \/>\nway?<br \/>\nHow does your program deal with boundary conditions, if there are any?<br \/>\nDid you remember to put all required comments in? Does your program call out for any other<br \/>\ncomments in the body of the code?<\/p>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>COMP 2103X1 Assignment 2 General information about assignments (important!): http:\/\/cs.acadiau.ca\/~jdiamond\/comp2103\/assignments\/General-info.html Information on passing in assignments: http:\/\/cs.acadiau.ca\/~jdiamond\/comp2103\/assignments\/Pass-in-info.html Information on coding style: http:\/\/cs.acadiau.ca\/~jdiamond\/comp2103\/assignments\/C-coding-style-notes [1] A filter program is a program which reads its input from ?standard input? (?stdin?) and writes its output <a href=\"https:\/\/www.benedictsol.com\/blogs\/non-printable-characters\/\" class=\"read-more\">Read More &#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-3772","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/posts\/3772","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/comments?post=3772"}],"version-history":[{"count":0,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/posts\/3772\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/media?parent=3772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/categories?post=3772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/tags?post=3772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}