Lab Session10 CSIT 121 Spring 05 n n
Lab Session-10 CSIT 121 Spring ‘ 05 n n n GCD Formula Example Programming Exercise 1
GCD Euclid’s algorithm for finding GCD (Greatest Common Divisor) of two numbers is as follows: n n 1. 2. 3. Take two positive numbers M and N such that M is less than or equal to N Divide N by M (N/M) If remainder R is zero, M is GCD Else N M, M R and go to step 1 2
Example n n n M 36, N 78 N/M yields remainder R 6 Now N M and M R (N 36) (M 6) N/M yields remainder R 0 Therefore GCD of 36, 78 is 6 3
Programming Exercise Due Apr 13 Write a program that computes GCD for 100 pairs of numbers. This program has the following two sections (functions). DATA GENERATION FUNCTION n It uses random number generator to generate and record 100 pairs of numbers produced randomly into a data file in ascending order (Each line in the file consists of two numbers with the first number being less than the second number. You must obtain file name from the user and send it to the data 4 generation function) n
Programming Exercise GCD COMPUTATION SECTION n The main opens the file generated in the previous section, reads each line and calls a value returning function to compute GCD. For each pair of numbers, the GCD obtained is returned to main where it is recorded in a result file. 5
- Slides: 5