This assignment consists of two written questions and two
programming questions.
For the programming assignments, please use the gnu (Free Software
Foundation) C compiler, gcc
. To help with debugging, use
the gnu debugger, gdb
.
void foo (int parameter_array[]) { int local_array[10]; ...Why the difference?
struct integer { int val; }; static void bar(struct integer i) { i.val = 3; } main() { struct integer n; n.val = 2; bar(n); printf("%d\n", n.val); }What does this similar program print in Java?
import java.io.*; class integer { public int val = 0; } public class num { static void bar(integer i) { i.val = 3; } public static void main(String[] args) { integer n = new integer(); n.val = 2; bar(n); System.out.println(n.val); } }Why the difference? How could you modify the C program to make it behave like the Java one?
#include <stdio.h> void get_line (char *s) { /* your code here */ } void put_line (char *s) { /* your code here */ } main () { char s[100]; while (1) { get_line(s); if (!s[0]) break; put_line(s); } }You should modify only the places where
/* your code here
*/
appears.
Your code for get_line
and put_line
should
duplicate the functionality of the standard gets
and
puts
library routines, except that (1) you need not return a
useful value, and (2) get_line
should not strip the
newline characters off the end of its input lines, and
put_line
should not add a newline to its output lines.
(This behavior with respect to newlines is the same as displayed by the
file-specific fgets
and fputs
routines.)
Within get_line
and put_line
you should use the getchar
and putchar
library
routines. You are not permitted (for this one assignment) to use
gets
, puts
, scanf
, or
printf
.
Your code must reside in a file named parrot.c
in a
directory named parrot
, which must be an immediate
subdirectory of the directory in which you run the
turnin
script.
Finally, in your README.txt
or README.pdf
file, you must answer the following questions: What may happen if you
feed your parrot
program input containing a line longer
than 100 characters? What might you do about this problem?
tail
when
run with the -r
command-line switch.)
You should store your strings internally in a linked list.
You may place a (large) fixed limit on the length of an input line, but
you should try not to use that much space for every line: once
read into memory, a line should be copied to dynamically allocated space
of the appropriate size.
You code should handle an arbitrary number of input lines.
You may use any of the functions in the <string.h> library package,
with the exception of strdup
;
type "man 3 string
" to learn more.
Prior to terminating, your program must deallocate (free
)
any dynamically allocated (malloc
ed) space. (Your program
will appear to run correctly even if you don't, but your code would then
"leak storage" if used as part of a larger system. The TAs will be
checking for proper memory cleanup.
Your code must reside in a directory named reverselines
,
which must be an immediate subdirectory of the directory in which you
run the turnin
script.
Answers to the written questions and documentation for the programming
questions must appear in your README.txt
or
README.pdf
file. This file
must be located in the directory in which you run the
turnin
script.
The only other things in this directory should be the two subdirectories
parrot
and reverselines
.
NO EXTENSIONS.
There is no "trivia" assignment for this project. Watch the newsgroup for possible updates.