Practice Programs can generally be solved with a short program that directly applies the programming principles presented in this chapter.
Write a program that will search a file of numbers of type int
and write the largest and the smallest numbers to the screen. The file contains nothing but numbers of type int
separated by blanks or line breaks. If this is being done as a class assignment, obtain the file name from your instructor.
Write a program that takes its input from a file of numbers of type double
and outputs the average of the numbers in the file to the screen. The file contains nothing but numbers of type double
separated by blanks and/or line breaks. If this is being done as a class assignment, obtain the file name from your instructor.
Compute the median of a data file. The median is the number that has the same number of data elements greater than the number as there are less than the number. For purposes of this problem, you are to assume that the data is sorted (that is, is in increasing order). The median is the middle element of the file if there are an odd number of elements, or the average of the two middle elements if the file has an even number of elements. You will need to open the file, count the elements, close the file and calculate the location of the middle of the file, open the file again (recall the “start over” discussion in this chapter), count up to the file entries you need, and calculate the middle.
If your instructor has assigned this problem, ask for a data file to test your program with. Otherwise, construct several files on your own, including one with an even number of data points, increasing, and one with an odd number, also increasing.
For a sorted file, a quartile is one of three numbers: The first has one-fourth the data values less than or equal to it, one-fourth the data values between the first and second numbers, one-fourth the data points between the second and the third, and one-fourth above the third quartile. Find the three quartiles for the data file you used for part (a).
(Hint: You should recognize that having done part (a) you have one-third of your job done—you have the second quartile already. You also should recognize that you have done almost all the work toward finding the other two quartiles as well.)
Write a program that takes its input from a file of numbers of type double
. The program outputs to the screen the average and standard deviation of the numbers in the file. The file contains nothing but numbers of type double
separated by blanks and/or line breaks. The standard deviation of a list of numbers n1, n2, n3, and so forth is defined as the square root of the average of the following numbers:
(n1 − a)2, (n2 − a)2, (n3 − a)2, and so forth
The number a is the average of the numbers n1, n2, n3, and so forth. If this is being done as a class assignment, obtain the file name from your instructor.
(Hint: Write your program so that it first reads the entire file and computes the average of all the numbers, and then closes the file, then reopens the file and computes the standard deviation.)
Write a program that gives and takes advice on program writing. The program starts by writing a piece of advice to the screen and asking the user to type in a different piece of advice. The program then ends. The next person to run the program receives the advice given by the person who last ran the program. The advice is kept in a file, and the contents of the file change after each run of the program. You can use your editor to enter the initial piece of advice in the file so that the first person who runs the program receives some advice. Allow the user to type in advice of any length so that it can be any number of lines long. The user is told to end his or her advice by pressing the Return key two times. Your program can then test to see that it has reached the end of the input by checking to see when it reads two consecutive occurrences of the character '\n'
.
Write a program that reads text from one file and writes an edited version of the same text to another file. The edited version is identical to the unedited version except that every string of two or more consecutive blanks is replaced by a single blank. Thus, the text is edited to remove any extra blank characters. Your program should define a function that is called with the input- and output-file streams as arguments. If this is being done as a class assignment, obtain the file names from your instructor.
Write a program that merges the numbers in two files and writes all the numbers into a third file. Your program takes input from two different files and writes its output to a third file. Each input file contains a list of numbers of type int
in sorted order from the smallest to the largest. After the program is run, the output file will contain all the numbers in the two input files in one longer list in sorted order from smallest to largest. Your program should define a function that is called with the two input-file streams and the output-file stream as three arguments. If this is being done as a class assignment, obtain the file names from your instructor.