|
CS 2213/2211 Advanced Programming Spring 2005 Recitation 7: Arrays of Strings Week 7: Mar 1-3 Due (on time): 2005-03-08 23:59:59 Due (late): 2005-03-13 23:59:59 |
Recitation 7 should be submitted
following directions at: submissions with deadlines
|
int main(int argc, char *argv[]) { /* ... */ } OR int main(int argc, char **argv) { /* ... */ }
Of course you can use other variable names besides argc and argv.
Inside the main function, the variable argc gives the number of command line arguments, that is, the number of elements in the array of strings, not including the NULL at the end. argv gives the command line arguments themselves as an array of strings. (Note that Java doesn't have the first parameter because Java arrays know how big they are. Java can't use the NULL at the end, because Java doesn't allow pointer arithmetic.)
% cargs one two three four five (a TAB before "five") % cargs -l long -eNOERRORS -a % cargs firstarg < infile.txt % cargs firstarg < infile.txt otherarg
In the last two test cases, you will have to create a dummy file named infile.txt for this to work.
% cargs one two three four five (a TAB before "five") First print of argv "cargs","one","two","three","four","five" Second print of argv "cargs","one","two","three","four","five" Third print of argv "cargs","one","two","three","four","five" Fourth print of argv "cargs","one","two","three","four","five" Fifth print of argv "cargs","one","two","three","four","five" Sixth print of argv "cargs","one","two","three","four","five" Seventh print of argv "cargs","one","two","three","four","five"
I have provided three web pages showing and describing self-reproducing programs:
You should first reformat the program for clarity, and then show piece-by-piece how the format string is used and what is printed out. You should also answer the question at the end of web page number 3 above.
Character | Representation in a string |
---|---|
newline | \n |
" | \" |
\ | \\ |
tab | \t |
The web page at link 4 tries to help by giving strings in red and escape sequences (representing one character) in yellow. (Escape sequences can only occur inside a string (inside double quotes) or as a single character (inside single quotes).)
You should deconstruct this C program, explaining how it works, in particular, explaining how it is able to reproduce itself. You should use the bold line numbers at the left (not part of the source) to refer to parts of the program. As a few hints, notice that the program starts out with the declaration and initialization of an array of strings. The most confusing part of this code for me was that I didn't notice that the two cases together at line 28 (starting with case '\\': case '\"': putchar('\\');) did not have a break at the end, so in case a \ or a " is encountered, the program will print a \ followed by a \ or a ", which uses the default case. Finally, notice that the code prints the array of strings in two different ways.
Contents of email submission
for Recitation 7: Last Name, First Name; Course Number; Recitation Number. a. Give your program cargs.c along with runs for the given test data (four runs), as described in part A above. b. Give your deconstruction of the simple self-reproducing program as described in part B above. Also try to answer the question at the end. c. Give your deconstruction of the more-complicated self-reproducing program as described in part C above.
|