This page contains some examples and problems for the Systems Programming course.
% wc -w
Test this function by writing a program that reads a string of up
to 1000 characters from the standard input, and outputs YES
or
NO
according to the result of the twotimes
function applied to the input string.
For example, the output of twotimes should
be true when called with a
parameter mammamiamammamia
and false when
called with a parameter abracadabra
.
For example, the output of cfreq when this file is passed as its input stream should be this.
int pwords(const char * begin, const char * end);
that takes an input text and returns the number of palindrome words
in the input text. A word is a contiguous sequence of
alphabetic characters as defined by the isalpha library
function.
Test the pword function by writing a program that reads the text from the standard input.
More interesting variant: Implement a program that takes one or more strings as a command-line parameters, reads the standard input, and returns 0 if all the given strings are found in the input stream.
WITH_ARRAY
can be passed to
the compiler to select the array version. Also, the numeric
pre-processor variable MAX_LINE_SIZE
defines the
maximum accepted line size (size of a single line). In addition,
the numeric pre-processor variable MAX_INPUT_SIZE
defines the maximum number of lines accepted by the array
implementation.
More interesting variant: If you have not done so already, separate the previous implementation into three modules:
main
function,
reads the input file, and uses one of the two container data
structures to store and then print the lines.
The input database is a text file with one record per line, and each field in each line separated by commas. For example:
16/12/1969,Mario Rossi,Alberto Rossi,Diana BianchiFor each selected person (specified on the command line) the program must print the name of all the person's ancestors, one for each line, indicating also their relation with the person. The relations are "mother", "father", "grandmother", "grandfather", "great-grandmother", "great-grandfather", "great-great-grandmother", and so on.
'F'
for fall or 'S'
for spring.
otp
to encrypt files using a one-time pad. A
one-time pad is an extremely simple and also very secure encryption
scheme that works as follows: each byte xi of the
cleartext file (input) produces a
byte yi=xi⊕ ki in the
ciphertext file (output) using one byte ki of the
key, where ⊕ is the xor operator.
A one-time pad is such that a key byte is used only once.
This means that you need large keys, and that you need to remember
which bytes you used of those keys. So, your encryption program must
use a key file f, plus an auxiliary file
named f.counter
that stores the number of bytes
already used in f.
The otp
program must accept the following optional
command-line options:
in=
input-file defines the input cleartext file.
By default, the program must read the cleartext file from standard
input stdin
.
out=
output-file defines the output ciphertext
file. By default, the program must write the cleartext file from
standard output stdout
.
key=
key-file defines the output ciphertext
file. By default, the program must use a key file
called key
. This also determines the name of the
counter file to associate with a particular key file. The name of
the counter file is then
key-file.counter
. If a counter file does not yet
exist, the counter is assumed to be 0.
For example, imagine that you and your friend share a large secret key
(say 10MB) that you store in a file called secretkey
. This is
the first time you use this key file, and you start by encrypting a
message with the following command:
./otp in=private_message1.txt out=encrypted_message1 key=secretkey
This creates a file called encrypted_message1
and also a file
called secretkey.couter
that contains the number n of
characters used from the beginning of the key file, which is also
exactly the number of characters in private_message1.txt
and in
encrypted_message1
. The number n is stored in
the counter file in decimal characters.
Then you use again the same key file to encrypt another message:
./otp in=message2.txt out=message2 key=secretkey
This second command reads the number from the counter file, and then starts encrypting the second message with the (n+1)-th character in the key file, and then updates the counter file.
Use this test script to test your implementation. Notice that this test uses two plaintext messages (message1.txt and message2.txt) and two corresponding ciphertext messages (message1.enc and message2.enc) encrypted with this secret key file.
Hint: in your implementation, you might want to use the
following functions from the standard library: fopen
,
fread
, fwrite
, fseek
, and of course
fprintf
, etc. Notice that you can open the key counter
file in read/write mode ("r+"
), and after reading the
current counter you can use rewind
to go back to the
beginning of that file to write the new value of the counter.
particles
that produces an animation of a number
of particles subjected to a constant force field. The animation is
based on a simulation of n ball-shaped particles moving and
bouncing around in a 2D space defined by the animation window.
Particles bounce off walls, but do do not interact with each other.
Examples:
The particles
program must accept the following
optional command-line options:
n=
integer sets the number of particles in the
simulation.
fx=
number horizontal component of the force field.
fy=
number vertical component of the force field.
trace=
number shading factor for the trace of
a particle (see examples above). This is the factor by which a
frame of the animation is dimmed at each successive frame. In other
words, the trace factor determines the exponential decay of the
brightness in tracing a particle, thus a value of 1.0 results in no
traces at all, while a value of 0.0 results in infinite traces.
radius=
number radius of the particles in
pixels.
delta=
time-in-seconds inter-frame interval.
Implement the particle program using your GPU to aminate the particles, and also to render their traces on a 2D space (window).