Previous: 8.6 Exercises
Up: 8 Functions and Files
Previous Page: 8.6 Exercises

8.7 Problems

  1. Write a program to make a table of decimal, octal, and hexadecimal unsigned integers from 0 to 255.

  2. Write a program to print a calendar for a month, given the number of days in the month and the day of the week for the first day of the month. For example, given that there are 30 days and the first of the month is on Tuesday, the program should print the calendar for the month.
    CALENDAR FOR THE MONTH
         sun  mon  tue  wed  thu  fri  sat
                   1    2    3    4    5
         6    7    8    9    10   11   12
         13   14   15   16   17   18   19
         20   21   22   23   24   25   26
         27   28   29   30

  3. Write a program to read the current date in the order: year, month, and day of month. The program then prints the date in words: Today is the nth day of Month of the year Year. Example:

  4. Write a program that prints a calendar for a year given the day of the week on the first day of the year. (Use Problem . for the definition of a leap year).

  5. Write a program that prints a calendar for any year in this century given the day of the week for the first day of the current year.

  6. Write a function that returns the value of a random throw of two separate dice.

  7. Write the following functions:

    first\_card() that draws a random card from a full deck.
         second\_card() that draws a random card from the remaining deck.
         Similarly, write third\_card() and fourth\_card().

    For the last three functions, you will need arguments that indicate what cards have already been drawn from the deck.

  8. Write a program using the functions of Problem ? to play a game of "black jack" with the user. Each side is dealt cards alternately. First each side is dealt two cards, but one at a time. Then, if necessary a maximum of one more card is allowed for each player. The player with the highest score, not exceeding 21, wins. In a tie, the user wins. The program should reshuffle the cards and play the game as long as the user wishes. The score is obtained by summing the value of each card. The value of a card is the face value of the card, except that an ace can be either 1 or 11 and all picture cards are 10.

  9. Randomly toss a coin: repeat and count the number of heads and tails in 100 tosses, 500 tosses, 1000 tosses.

  10. Write a program to play a board game with the user. The game uses a throw of two dice. The rules of the game are as follows. Each player takes a turn and is allowed a succession of throws. If a player's first throw is seven or eleven, he/she loses the turn. Otherwise, the player's score is increased by the value of each throw until the dice show a seven or a eleven. The turns continue between the user and the program until a pre-set limit for the score is reached.

  11. Write a program to compare the routine sq_root() written in Chapter ? with the standard library routine. How close are the routines?

  12. Write a function that returns the hypotenuse, given the two sides of a right angled triangle. A hypotenuse is the square root of the sum of the squares of the two sides of a right angled triangle.

  13. Find all the angles of a right angled triangle if the lengths of the two sides are given. Since it is a right triangle, one angle is pi / 2. Also, the ratio of the two sides in a right triangle gives the tangent of one of the other angles. Therefore, one angle is the arctangent of the ratio of the two sides. Use a library function to get the arctangent of a value. The other angle is easily obtained since the three angles must add up to pi.

  14. Use library routines to compare values of sine, cosine, and exponential with those calculated by Chapter 3 problems 3.1 through 3.3.

  15. Write a menu-driven program that handles the grades for a class. The program allows the following commands.
    Get data: gets id numbers and integer scores for a set of 3 projects from a
    file. Assume that the id numbers start at 0 and go up to a maximum of 99.
        Print data: prints the scores.
        Average scores: averages each set of scores.
        Change scores: allows the user to make changes in scores for any project
    and for any id number.

  16. Write a program that reads a text of characters from a file and keeps track of the frequency of usage of each letter, digit, and punctuation.

  17. Write a menu-driven program that reads input data from a file. The program reads and stores for each student the ID number, course numbers, credits, and grades. Assume a maximum of 3 courses per student. The program should compute and store the GPR for each student. At the end of input, the program writes to a file as well as to the standard output all the input data and GPR for all students.

  18. Write a program that shuffles and deals out all 52 cards of a deck of playing cards to 4 players. Each card is dealt in sequence around a table to players in the following order: west, north, east, south. Print out the hands of each player. You must use a random generator, but discard a possible card if it has already been dealt out. Use an array of 52 elements to keep track of the cards already dealt out.

  19. Write a program to play the game of 21 with a limit of five cards for each player. Assume the program plays south and deals the cards. The other three players are in order west, north, and east. Cards must be dealt randomly.

  20. Write a program that reads a positive integer n; it then generates a set of n random numbers in a range from 0 to 99. Store and count the frequency of occurrence of each number. Print the frequency of each number.

  21. Use an array to read from a file and store the sample values of an experiment at regular intervals. Plot the graph of the sample values versus time. Time should increase vertically downwards. Use '*' to mark a point. Write a program to read in sample values and call a function to plot the values.

  22. Repeat Problem 27, but plot a bar chart for the sample values.



Previous: 8.6 Exercises
Up: 8 Functions and Files
Previous Page: 8.6 Exercises

tep@wiliki.eng.hawaii.edu
Wed Aug 17 09:15:23 HST 1994