Previous: 14.4 Summary
Up: 14 Storage Class and Scope
Previous Page: 14.4 Summary

14.5 Problems

  1. Modify the functions getchr() and ungetchr() of Section 14.1.6 so that any number of characters, up to a maximum of 40, can be put back into the input stream. Use these functions in a program that reads characters and puts all vowels back until a newline is read. At that point, the program writes the vowels that were put back in the input stream.

  2. Write a program that reads scores from a file, but uses a dynamically allocated array. Assume that the first line of the file has the number of students. Read the value of the number of students, dynamically allocate an array for the scores, read the scores, and print them out.

  3. Modify 2 so a student record is a structure. The file lists the number of students in the first line and the number of exams in the second line. Assume that an old weighted average is present as the last column in the file and that the first two columns are student name and an id number. Use dynamic allocation to write a menu-driven grading program that allows all possible options: add student, delete student, change grade, add new exam scores, compute various averages, etc.

  4. Write a program that reads and sorts an array of numbers. Use a function to sort the array, but use a pointer to a function to make a comparison of two numbers. If the function returns True, swap the elements; otherwise, the elements are in correct order. Test the program with functions to sort in increasing and in decreasing order.

  5. Write a program that reads an array of transliterated strings that represent equivalent strings in some language. The strings are to be sorted according to the alphabet of that language. First order the ASCII characters according to the alphabet of that language. Then use a function that returns True if two characters are ordered in a correct sequence. Use the function to sort the array of strings.

  6. Use a structure with two members to represent either a complex number or a rational number. We will call each of them an ordered pair number. Write a generic function to add two ordered pair numbers where the addition is performed by a function a pointer to which is passed as an argument.

  7. Repeat 6 to subtract two ordered pair numbers.

  8. Repeat 6 to multiply two ordered pair numbers.

  9. Repeat 6 to divide two ordered pair numbers.

  10. Write a lexical analyzer that finds tokens of the following type in a string:

    identifier
         integer
         float
         operator
         end of string

  11. Repeat 10 without using an external variable. Use a pointer to a character as an argument to a function get_token() which indirectly returns the character read, but unused in a token.

  12. Repeat 11, but use a static character variable in get_token() instead of indirectly returning the character read but unused. The static character will remain unchanged and may be used for the start of the next token.



Previous: 14.4 Summary
Up: 14 Storage Class and Scope
Previous Page: 14.4 Summary

tep@wiliki.eng.hawaii.edu
Sat Sep 3 07:21:51 HST 1994