practical03_sol

# practical03_sol - Practical Session 3 — Solutions...

This preview shows pages 1–2. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Practical Session 3 — Solutions Exercises 1. The modification of the code is straightforward. The largest integer for which the code returns a correct result is 34. For 35 and higher the code gives Inf , i.e. a overflow error. This oc- curs because the factorial exceeds a few times 10 38 , the largest single precision floating point number. 2. The left hand side of (2) should be used rather than the right hand side to avoid overflow in the numerator n ! and the denominator ( n- m )! . These individual terms may overflow even when the ratio is within the allowed range for single precision floating point. A suitable code is the following. / * calc_fperm.c * Program demonstrating the use of a function to calculate the * number of permutations of n objects chosen m at a time * / #include <stdio.h> float ffact(int m); float fperm(int n, int m); int main() { int n, m; float nperm; printf("Integer n? "); scanf("%d", &n); printf("Integer m? "); scanf("%d", &m); nperm = fperm(n, m); / * function call * / printf("Number of permutations = %g\n", nperm); return 0; } float fperm(int n, int m) { / * function to calculate number of permutations of m * objects chosen from n objects * / int i; float result; result=1.0; for (i = n; i >= n-m+1; i--) result * =i; return result; 1 }...
View Full Document

## This note was uploaded on 09/29/2009 for the course COSC 1002 taught by Professor Wheatland during the Three '09 term at University of Sydney.

### Page1 / 6

practical03_sol - Practical Session 3 — Solutions...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online