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.
- The prototype for your recursive function is
void printDigits(int n);
For example, the function call printDigits(32767) prints the following:3 2 7 6 7
Note that no newline character should be printed.
You will receive 0 on this part if you write a nonrecursive function. - Your main function uses a loop to generate 10 random positive integers and does the following for each of them: prints the number, prints a colon symbol, calls printDigits with the number, and prints a newline symbol.
- Save your program in a file called digits.c.
- Run your program and save the results in a file called q1.out.
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.
- The following are the prototypes for the functions you will write:
void generateLine(FILE *fp); void analyzeLine(FILE *fp); void generateFile(void); void analyzeFile(void);
- The generateLine function generates a random line of characters and writes it into the specified file. It works as follows: First, it generates a random integer n between 1 and 80. Then it generates n random printable characters and writes them into the file. Finally, it writes a newline character into the file. Note that the printable characters have integer codes between 32 and 126.
- The analyzeLine function is adapted from your program for Q1 of A2. Instead of reading from the keyboard, it reads a line of characters from the specified file.
- The generateFile function creates a file called tests.dat and writes 10 lines of characters into it by calling the function generateLine.
- The analyzeFile function analyzes each line of the file tests.dat by calling the function analyzeLine.
- Your main function simply calls the two functions generateFile and analyzeFile.
- Instead of using the rand_int function defined on Page 171 of the textbook, you should define and use a macro to generate a random integer between specified limits.
- Save your program in a file called tests.c.
- Run your program and save the results in a file called q2.out. Have a look at the file tests.dat.
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:
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.<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>
- 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:Then submit the file a3.tar.gztar cvf a3.tar digits.c q1.out tests.c tests.dat q2.out lottery.c results.html gzip a3.tar