CMPT 102 - Assignment 3

Out: Wednesday, Oct. 19, 2005
Due: Wednesday, Nov. 2, 2005 at 5:00pm
Weight: 10%
Total: 100 marks


Question 1 [20]

Write a recursive function which, given a positive integer, prints it digit by digit, with one blank after each digit. Test your function with 10 random numbers.


Question 2 [40]

Check your program for Q1 of A2 against the posted solution and make any necessary corrections. Rewrite your program into a function. Test your function with 10 random lines of characters.


Question 3 [40]

Write a function to simulate a lottery drawing. Generate an HTML page with the simulation results displayed in an HTML table.

  • The prototype for the function is
    void lottery(int n);
    The lottery function simulates a lottery drawing that uses balls numbered from 1 through 10. Assume that three balls are drawn at random. The argument n specifies the number of lottery drawings to simulate. The function prints text to the screen which includes the following information:
    • the number of drawings;
    • the percentage of the time that the result contains three even numbers in the simulation;
    • the percentage of the time that the number 7 occurs in the three numbers in the simulation;
    • the percentage of the time that the three numbers are 1, 2, and 3 in the simulation.
  • Your main function calls the lottery function with arguments 10i for i from 1 to 8. It generates an HTML page with the simulation results displayed in an HTML table. Thus the output of your program should include HTML tags indicating how the document is structured. In general, the output should have the following form:
      
    <html>  
    <body>  
    <h2>Lottery Simulation Results</h2>  
    <table cellpadding=4 cellspacing=1 border=2> 
    <tr>  
    <td># of Drawings</td><td>Case 1</td><td>Case 2</td><td>Case 3</td>
    </tr>
    <tr>
    <td>ROW1-COLUMN1</td><td>ROW1-COLUMN2</td><td>ROW1-COLUMN3</td><td>ROW1-COLUMN4</td>
    </tr>  
    ... 
    <tr>  
    <td>ROWn-COLUMN1</td><td>ROWn-COLUMN2</td><td>ROWn-COLUMN3</td><td>ROWn-COLUMN4</td>
    </tr>  
    </table>  
    </body>  
    </html>
    
    Parts of the document is static and can be included directly in the output as it appears in the above. However, the ROWi-COLUMNj parts of the document will be dynamic and depend on the actual execution of your program. So your lottery function should calculate the values for ROWi-COLUMNj and print the text beginning with the <tr> tag and end with the </tr> tag.
  • Save your program in a file called lottery.c.
  • Run your program by typing the following command:
    ./lottery > results.html
    The > symbol here is used to redirect the output of the program to the file results.html. You will need to wait for a while for the program to finish its execution.
  • Now you can open results.html with your favorite web browser, and view the simulation results as a neatly formatted webpage.
  • Here is an example of the HTML file that will be generated by your program (I only include 4 rows of the table). You can download it and see what it is like if opened with a text editor.

How to submit

Electronic submission

You should submit the files digits.c, q1.out, tests.c, tests.dat, q2.out, lottery.c, results.html. You can do this by typing these commands:
tar cvf a3.tar digits.c q1.out tests.c tests.dat q2.out lottery.c results.html
gzip a3.tar
Then submit the file a3.tar.gz

Hard-copy submission

You should submit the printout of the files you submit electronically. Also, don't forget to print out a copy of the sample cover page, fill it out, and sign it.