Introduction
Announcements

Schedule
Labs
Assignments
TA office hours

Tests, exam

Topic videos
Some course notes
Extra problems
Lecture recordings

Discussion board

Grades so far

CSC 209 additional problems for C programming


Also see software tools problems, and actually most of the suggested problems I've got in these web pages. The following problems are mostly non-unix-like C programs.


1. Write the "add" program, which takes no command-line arguments and simply adds together all of the numbers in its standard input. The input consists of free-format integers and should be read with scanf("%d",&x) or similar. scanf("%d",&x) returns 1 for a successful input, EOF (i.e. -1) for end-of-file, and 0 if you are trying to read non-numeric input. In the non-numeric-input case, you should write "add: non-numeric input\n" to stderr and not write anything to stdout. Otherwise, you should write the total (and nothing else except a terminating \n) to stdout.

Note that "add </dev/null" should produce the output "0".

Example use: ls -l | nth 5 | add

[solution]


2. Write a C program whose output is its input, reversed line by line.

[solution]


3. Write a void function which takes two FILE* arguments. It reads a sequence of integers from the first file, bubblesorts them, then outputs the sorted list to the second, one per line, no additional data. The input file is free-format; i.e. you will want to use fscanf(). It is okay if your program mistakes non-numeric input for end-of-file; i.e. you will probably want to use while (fscanf(...) == 1).

You could test your sortfile.c with the following:

    #include <stdio.h>
    int main()
    {
	sortfile(stdin, stdout);
	return 0;
    }
if you add in an appropriate declaration for sortfile [mini-solution: the above with sortfile declaration added in].

Your sortfile.c should accept up to a hundred numbers (exactly). If there are more, print an error message to stderr and exit; in this case, do not output anything to the output file.

[solution]


4. Write a main() to prompt for and read the input file and output file names. Then open them both (with fopen(), calling perror() and exiting if the fopen() fails), and call the above sortfile(infp, outfp). Put this function in a new file called main.c. This main.c should be able to be compiled with your sortfile.c (i.e. "gcc -Wall main.c sortfile.c").

[solution]


5. Write a new version of the above sortfile() which calls the qsort() library function instead of using the bubblesort algorithm. You can link it with either the previous tiny test main() or your main() from question 3 above.

qsort() is discussed in section 17.7 of the King book, and summarized on page 622. The on-line manual page "man qsort" is more comprehensive although equally terse.

Here is a suitable comparison function to pass to qsort() for an array of ints:

    int comparator(const void *p, const void *q)
    {
	return *(int *)p - *(int *)q;
    }

[solution]


6. Write a new main() for the above which expects the two file names to be specified on the unix command-line, instead of prompting for and inputting them.

[solution]


7 (big). Write a program which takes one argument which is a .c file with compile errors. The argument file name must end in ".c" (checking that is another problem, also recommended). Run "gcc -Wall -c file" and collect the errors. Note the output format with line numbers.

If the programmer goes about fixing these errors in order, then earlier error fixing will often change the line numbers such that later errors are harder and harder to locate. Your script will take this error data, and the lines of the original .c file, and produce output with the error messages inserted. Also insert "ZZZ" so that the user can search for this to find the next error.

Example:

...
    double x;
...
        printf("%d\n", x);
...

gcc -Wall outputs:

file.c: In function `main':
file.c:21: warning: int format, double arg (arg 2)

Your script discards "file.c: In function `main':" (or any other line not containing a line number) and produces on stdout a file which looks like this:

...
        printf("%d\n", x);
ZZZ file.c:21: warning: int format, double arg (arg 2)
...

Perhaps the eventual version of this program should replace file.c, but especially during testing I strongly recommend developing the version which outputs to stdout instead.