Back to DFS's C Page


OAC Problem Set IV

Random Values, Files & structs #1

Program I: Creating Random Values and Saving Them to Disk

You are to write a program dice1.c which will generate ten (10) simulated rolls of a pair of dice. These values along with the total of each roll should be saved in a two-dimensional array and printed in a labeled table. You should then save the two values for each roll in a text file called rolls10.dat, each pair followed by a newline character.

To learn about generating random numbers, you should start by reading the man page for srand(). Then proceed with the Generating Random Values page.


Program II: Reading Data From a File & Printing a Table

Using the following compiler directive and declarations

#define MAXROLLS 10

struct roll_s {
   int die1,
       die2,
       total; };
struct roll_s rolls[MAXROLLS];

write a program named dice2.c which reads in ten rolls of a pair of dice from the file rolls10.dat and stores them in an array of structs declared as above.

Your program must follow the pseudocode provided below.

  1. Fill the rolls array with data read from a file for which the user provides the name
  2. Fill in the totals element of each struct using a loop
  3. Print out a labeled table of data, using a function call for the printing of each row in the table

N.B. A graceful exit from the program is required if the user-specified file does not exist.


Created 7 Jan 02
© DFStermole 2002