Programming Exercises

I l @ ve RuBoard

Programming Exercises

  1. Some users might be daunted by being asked to enter an EOF character. Modify getarray() so that a # character terminates input the first time it is entered.

  2. Create a program that sorts float numbers in increasing order.

  3. Modify the sort () function in Listing 13.12 so that it uses the suggestion given in the "Comments" section following the sample output; namely, that the function keep track of the index of the largest value and swap values only after the inner loop terminates.

  4. Write and test in a loop a function that returns the number of times it has been called.

  5. Write a program that generates a list of 100 random numbers in the range 1 “10 in sorted decreasing order.

  6. Write a program that generates 1,000 random numbers in the range 1 “10. Don't save or print the numbers, but do print how many times each number was produced. Have the program do this for ten different seed values. Do the numbers appear in equal amounts? You can use the functions from this chapter or ANSI C rand() and srand () functions, which follow the same format that our functions do. This is one way to test the randomness of a particular random-number generator.

  7. Write a program that behaves like the modification of Listing 13.9, which we discussed after showing the output of Listing 13.9. That is, have the program produce output like the following:

     Enter the number of sets; enter q to stop. 18 How many sides and how many dice? 6 3 Here are 18 sets of 3 6-sided throws.   12  10   6   9   8  14   8  15   9  14  12  17  11   7  10   13   8  14 How many sets? Enter q to stop. q 
  8. Write an interactive program that lets the user enter as many as 20 words. Have the program display the words in sorted order (recall the string sorting example from Chapter 11, "Character Strings and String Functions" ) and then ask the user if the words should be saved in a file. If the user responds with a yes, have the program request a name for the file and then write the words into a file by that name .

  9. Write a function that skips over input until encountering a digit. It then stores that digit and subsequent digits in a string until encountering a nondigit. The nondigit is placed back in the input, and the function converts the digit string to a numeric value. The function should use a pointer argument to supply the numeric value to the calling program. Use the function return value to return EOF if the function encounters end of file; have it return 1 otherwise . Use getc() and ungetc () . In short, this function finds the next integer in output, whether it is isolated or embedded in text, as in be22again .

  10. Modify Exercise 8 so that the function recognizes an optional minus sign. That is, confronted with an input of be-22now , it extracts the value -22 .

  11. Construct a text file containing ten lines, each composed of a name, a colon , and three integers. Write a program that reads the file and prints the lines in order of increasing average value of the integers in the line. That is, the line

     Shalla Woogie: 80 70 84 

    would precede the line

     Hagar Joe Plinty: 70 90 80 

    because the average of its three values is less than the second average. Also, have the program append the average to each line when it is printed. Note that the name part of the input need not consist of exactly two names .

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