COMP2303 / COMP7306 Exercises
Exercises will be provided relating to the lecture material and C programming. We won't provide solutions to these exercises, but are willing to post correct student supplied solutions. If you want your solution to be considered, email it to comp2303@itee.uq.edu.au (as an attachment) with the word SOLUTION in the subject line and clearly specify the week and exercise number which this is a potential solution for. The submitter's identity (name) WILL be published unless you request anonymity. Either way, you MUST send the email from your student email account OR include your student number in the email. Multiple solutions will be posted only if sufficiently different from the first solution posted. Don't expect a reply to your email - we won't mark your work. If you have questions or queries about posted solutions or other possible solutions, you should post them to the newsgroup for discussion or raise them at a prac or consultation session.
Week 2-3
These exercises are intended to get you familiar with the relationship between integers and floating point numbers, printf formatting, basic loops, character comparisons, array iteration. It is assumed that you have worked through the C programming tutorials and can use the functions covered there, e.g. scanf() and printf(). You may need to consult the documentation for these functions.
- Write a C program which prompts the user for two decimal numbers
(assumed to be in the range 0 to 100 inclusive) and prints out both the
arithmetic mean and geometric mean of the two numbers to 2 decimal places.
- Write a C program that prints a "nicely formatted" table with row and
column headings where each cell shows the geometric mean (rounded to the
nearest integer) of the row and column values. Row values should range from
25 to 100 in steps of 5. Column values should range from 25 to 100 in steps
of 5. Hint: See the multiplication table in C Tutorial 2.
- Write a C program that reads a string (of up to 80 characters) from
standard input and then prints out a report that specifies
(a) the number of uppercase characters in the string
(b) the number of lowercase characters in the string
(c) the number of digits in the string
Try writing both an "array index" and "pointer" version of this program.
These exercises are intended to get you familiar with more complex C concepts including structures, some system calls, function pointers.
- Write a C program which prints the current local time in the format:
hh:mm:ss to standard output. Hint: look at the manual pages for time(2) and
localtime(3c) (i.e. run "man -s 2 time" and "man -s 3c localtime" on agave).
- Write a C program which expects one or more filenames to be given on the
command line and then for each file, prints a line containing the name of
the file, followed by a colon, followed by the size of the file (in bytes).
Hint: look at that stat(2) function.
- (Similar to 2, aspects of 1) Write a C program which expects one or more
filenames to be given on the command line and then for each file, prints a
line containing the name of the file, followed by a colon, followed by the
last access time, the last modification time and the last file status
change. Each of the times should be in the format hh:mm:ss dd/mm/yyyy.
- Write a C program which reads any number of lines from standard input,
each containing a single (double precision) floating point number. At EOF,
the numbers are sorted using qsort and output to standard output.
- Write a C program which expects a single filename as a command line
argument. The file can be assumed to be a text file (i.e. does not contain
null characters) containing lines of text. Your program should sort the
lines of text in the file (using qsort) into order from shortest to longest
and then output the lines in this order. The file can be of any size, can
have any number of lines and the the lines can be of any length. Hints:
you'll need to use malloc (and possibly realloc) to allocate space for the
lines of text from the file (and for the array to hold pointers to the lines
of text). Look at the fopen(3c) and fclose(3c) functions. For reading the
files, you may want to consider fread(3c) or fgets(3c).
- Assuming a computer system requires natural alignment (i.e. types which
occupy n bytes must begin on an address which is a multiple of n) and that
characters, shorts, ints, longs, floats, doubles and pointers to objects
occupy 1, 2, 4, 4, 4, 8 and 4 bytes respectively, what is the result
of the sizeof() operator in each of the following cases?
(a) sizeof(short)
(b) sizeof(short[5])
(c) sizeof(float[4])
(d) sizeof(struct {char c; double d; int i;})
(e) sizeof(struct {char c; int i; double d;})
(f) sizeof(struct {char c[4]; float f[2]; int i[3]; double d[2];})
(g) sizeof(union {short s[2]; int i[4];double[2]; char c[13];})
(h) sizeof(struct {char* cp[3], double* dp, short s;})
(i) sizeof(struct {char c[2]; union {int i; double d; char* cp;} data; char c2[2];})
(j) sizeof(struct {char c[2]; char c2[2]; union {int i; double d; char* cp;} data;})
- Describe (in English) the type of var in each of the following
declarations:
(a) char** var[];
(b) int* a, var;
(c) int var(int);
(d) int (*var)(int);
(e) void (*var)();
(f) double *(*var)(double);
(g) void (*var)(void*, int);
(h) int *(*(*var)(int*))(void);
(i) void *(*var)(void *(*)(void*), void*(*)(void*),void*);
(j) int (*(*var[3])())(void (*)());