Final Exam Programs

advertisement
Final Exam Programs
Create a subdirectory of your home directory named Final (note the case).
Save your solutions in files named p01.c through p10.c. Your programs are
due Thursday December 10 at 6:00PM. You will not receive any credit if your
program is late, has the wrong filename, or is in the wrong directory.
The first five problems ask for programs that produce a specific output.
Make sure that your programs do not produce any other output.
1. Write a C program that opens a file named as a command line argument,
and the counts the number of lines that have at least 25 characters (not
counting the newline). The output should be just one number (the number
of lines meeting the condition) followed by a newline.
2. Write a C program that reads text from stdin and capitalizes all the
words. The output should be the same as the input except for the capitalization.
3. Write a C program that reads one line of text from stdin and removes all
duplicate characters. Only the first occurrence of each character should
be printed. For example, if the input is
Abcabc abcxyz the end.
the output should be
Abca xyzthend.
4. Write a C program that reads a file containing one word per line, creates
a binary search tree (in alphabetical order), and prints out all words at a
specified level in the tree. The file should be given as the first command
line argument, and the level as a second command line argument. For
example, one might run the program as follows.
a.out /u1/junk/wordlist 3
In this case the program should print all words (in order) that are at level
3 in the binary search tree. Note: the root node is at level 1.
5. Write a C program that reads a data file (from stdin) containing several
integers on each line. The program should determine which line has the
most integers on it. For example, if the data file is
34 67
999 -2 23456901
12
the correct output is
2 3
since line 2 contains 3 numbers.
For each of the following problems write a C function that does the required
job. Your function should produce no extraneous output and should return
the appropriate value. Make sure your file contains only the function (and not
main). I will use your function in my program.
Your function may use other functions that you write, but may not use
functions in one of the standard libraries. For example, if you want a function
to compute the length of a string, you may write your own string length function
and put it in your solution file, but you may not use the string library strlen
function.
6. Write a C function which returns the bit position of the highest order onebit in an unsigned long integer. The correct prototype for your function
will be:
int highbit(unsigned long word);
Note: the bit positions are numbered from 1 to 64.
7. Write a C function that has one unsigned 64-bit parameter and which
sums the 16 4-bit values. The prototype for this function is
int fourbitsum(unsigned long word);
8. Write a C function that has two parameters: an integer array and an integer array size. The function should return the index of the first zero
element in the array, or -1 if no such element is found. The correct prototype for the function is:
int findzero(int a[], int len);
9. Write a function that has two integer parameters, and array and a length.
The function should sort the array in the following way. All odd integers
should be sorted in increasing order, followed by all even numbers in decreasing order (hint: write a comparison function). The function should
return the index where the even numbers begin. The correct prototype is:
int oddevensort(int a[], int len);
10. Write a C function that has one parameter, a pointer to a linked list, and
which returns a pointer to the modified list obtained by moving the first
element to the end of the list. If there are fewer than two elements in the
list, the function does not change anything, it just returns the list pointer.
The program I use to test your function will contain the declaration (found
in /u1/junk/p10.h):
typedef struct node {
int data;
struct node *link;
} NODE;
The correct prototype for your function will be:
NODE *movefirst(NODE *list);
Download