Programming Exercises

I l @ ve RuBoard

Programming Exercises

  1. Write a file copy program that takes the original filename and the copy file from the command line. Use standard I/O and the binary mode, if possible.

  2. Write a program that sequentially displays onscreen all the files listed in the command line. Use argc to control a loop.

  3. Modify the program in Listing 12.6 so that it uses a command-line interface instead of an interactive interface.

  4. Programs using command-line arguments rely on the user's memory of how to use them correctly. Rewrite the program in Listing 12.2 so that, instead of using command-line arguments, it prompts the user for the required information.

  5. Write a program that opens two files whose names are provided by command-line arguments.

    1. Have the program print line 1 of the first file, line 1 of the second file, line 2 of the first file, line 2 of the second file, and so on, until the last line of the longer file (in terms of lines) is printed.

    2. Modify the program so that lines with the same line number are printed on the same line.

  6. Write a program that takes as command-line arguments a character and zero or more filenames. If no arguments follow the character, have the program read the standard input. Otherwise, have it open each file in turn and report how many times the character appears in each file. The filename and the character itself should be reported along with the count. Include error-checking to see whether the number of arguments is correct and whether the files can be opened. If a file can't be opened, have the program report that fact and go on to the next file.

  7. Modify the program in Listing 12.3 so that each word is numbered according to the order in which it was added to the list, starting with 1. Make sure that, when the program is run a second time, new word numbering resumes where the previous numbering left off.

  8. Write a program that opens a text file whose name is obtained interactively. Set up a loop that asks the user to enter a file position. The program then prints the part of the file starting at that position and proceeding to the next newline character. Let non-numeric input terminate the user-input loop.

  9. Write a program that takes two command-line arguments. The first is a string; the second is the name of a file. The program should then search the file, printing all lines containing the string. Because this task is line-oriented rather than character-oriented, use fgets() instead of getc() . Use the standard C library function strstr () ( briefly described in Exercise 7, Chapter 11) to search each line for the string.

  10. Write a function that reads one word from a file, leaving the boundary character (a tab, space, or newline) in the input buffer. Don't use scanf() or fscanf() .

  11. Create a text file consisting of 20 rows of 30 integers. The integers should be in the range 0 “9 and be separated by spaces. The file is a digital representation of a picture, with the values through 9 representing increasing levels of darkness . Write a program that reads the contents of the file into a 20-by-30 array of int . In a crude approach toward converting this digital representation to a picture, have the program use the values in this array to initialize a 20-by-31 array of char , with a value corresponding to a space character, a 1 value to the period character, and so on, which each larger number represented by a character that occupies more space. For example, you might use # to represent 9. The last character (the 31st) in each row should be a null character, making it an array of 20 strings. Have the program display the resulting picture (that is, print the strings) and also store the result in a text file. For example, suppose you start with this data:

    graphics/12fig04.gif

    The output could look like this:

    graphics/12fig05.gif
  12. Digital images, particularly those radioed back from spacecraft, may have glitches. Add a deglitching function to Programming Exercise 11. It should compare each value to its immediate neighbors to the left and right, above and below. If the value differs by more than 1 from each of its neighbors, replace the value with the average of the neighboring values. You should round the average to the nearest integer value. Note that the points along the boundaries have fewer than four neighbors, so they require special handling.

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net