This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: COSC 1002/1902 Computational Science in C Practical 3 This practical session includes defining functions in C, the procedure for writing to file, and the use of gnuplot to display results. Before you begin these exercises, you should review the material in lecture 3. You should also download the codes appearing in the lecture notes for lecture 3, calc fact.c and print fact.c , which are available via WebCT or from the website http://www.physics.usyd.edu.au/ wheat/cosc1002 . Compile and run the codes, following the steps outlined in the lecture notes, and confirm that you ob tain the results described in the lecture notes. Also try plotting the contents of the file factorials.txt produced by print fact.c using gnuplot, as described in the lecture. You may also want to read the document Introduction to gnuplot available on the unit web pages. If you succeed with this then you are ready to begin the exercises. If not consult a tutor, who can provide assistance. The exercises are for students in both units except where indicated. Remember to get a tutor to mark off a checkpoint when you reach it. Exercises 1. The factorial function presented in lecture 3 only works up to 12! because 13! exceeds the limit 2 31 1 on the size of integers (see lecture 2). In this exercise you will modify the code calc fact.c so that the factorial function returns a floating point value (in single precision, i.e. of type float ), by following the steps below. This has the advantage of allowing larger factorials to be calculated, at the expense of some accuracy. Specifically, you need to perform the following steps. Change the type of the function to float Change the type of the variable result used inside the function fact.c to float Rename the modified function ffact.c (floating point factorial) and rename the mod ified code calc ffact() In the printf() statement to show the result, use the formatting option %g to force scientific notation: printf("n factorial=%g\n",nfact); Check that the code calc ffact.c works by comparison of results with a calculator. What is the largest integer n for which ffact(n) returns a value? Explains what happens for larger integers, and why it happens. 1 Answer: 2. The number of ways in which m objects can be chosen from n objects where order is important is P n m n ! ( n m )! . (1) The quantity P n m is also called the number of permutations of m objects selected from n objects....
View
Full
Document
 Three '09
 wheatland

Click to edit the document details