Title: Computer Programming Lab 7 Solution
1Computer ProgrammingLab 7 Solution
- Shyh-Kang Jeng
- Department of Electrical Engineering/
- Graduate Institute of Communication Engineering
- National Taiwan University
2Lab Exercise 6 Greatest Common Divisor
- The greatest common divisor (GCD) of two integers
is the largest integer that evenly divides into
each of the two integers. Write a function gcd
that returns the greatest common divisor of two
integers.
3Sample Output
Enter two integers 6 8 The greatest common
divisor of 6 and 8 is 2 Enter two integers 789
4 The greatest common divisor of 789 and 4 is
1 Enter two integers 9999 27 The greatest
common divisor of 9999 and 27 is 9 Enter two
integers 73652 8 The greatest common divisor of
73652 and 8 is 4 Enter two integers 99 11 The
greatest common divisor of 99 and 11 is 11
4Solution (1/3)
- // CPLab7\Main.cpp
- include ltiostreamgt
- using stdcout
- using stdcin
- using stdendl
- int gcd( int, int )
- int main()
- int a
- int b
- // allow the five sets of numbers to be input
- for ( int j 1 j lt 5 j )
- cout ltlt "Enter two integers "
- cin gtgt a gtgt b
5Solution (2/3)
- cout ltlt "The greatest common divisor of " ltlt a
- ltlt " and " ltlt b ltlt " is "
- ltlt gcd( a, b ) ltlt "\n\n"
- // end for
- return 0
- // end main
6Solution (3/3)
- // function gcd definition
- int gcd( int x, int y )
- int greatest 1
- for( int i 2 i lt ( ( x lt y ) ? x y ) i
) - if ( x i 0 y i 0 )
- greatest i
- return greatest
- // end gcd
7Follow-Up Activity 1
- Use global variables to rewrite the program so
that gcd takes no arguments. Why is it
considered bad programming practice to use global
variables? - Answer Global variables can be referenced from
anywhere inside a program, hence it is difficult
to find out where they are changed.
8Follow-Up Activity 1 Solution (1/3)
- // CPLab7\Main.cpp
- include ltiostreamgt
- using stdcout
- using stdcin
- using stdendl
- int a
- int b
- int gcd()
- int main()
- // allow the five sets of numbers to be input
- for ( int j 1 j lt 5 j )
- cout ltlt "Enter two integers "
- cin gtgt a gtgt b
9Follow-Up Activity 1 Solution (2/3)
- cout ltlt "The greatest common divisor of " ltlt a
- ltlt " and " ltlt b ltlt " is "
- ltlt gcd() ltlt "\n\n"
- // end for
- return 0
- // end main
10Follow-Up Activity 1 Solution (3/3)
- // function gcd definition
- int gcd()
- int greatest 1
- for( int i 2 i lt ( ( a lt b ) ? a b ) i
) - if ( a i 0 b i 0 )
- greatest i
- return greatest
- // end gcd
11Follow-Up Activity 2
- Create another function called input, that takes
two integer values passed via call-by-reference
and replaces their values with two input by the
user. Use the function to input the values for a
and b.
12Follow-Up Activity 2 Solution (1/3)
- // CPLab7\Main.cpp
- include ltiostreamgt
- using stdcout
- using stdcin
- using stdendl
- int gcd( int, int )
- void input( int, int )
- int main()
- int a
- int b
- // allow the five sets of numbers to be input
- for ( int j 1 j lt 5 j )
- input( a, b )
13Follow-Up Activity 2 Solution (2/3)
- cout ltlt "The greatest common divisor of " ltlt a
- ltlt " and " ltlt b ltlt " is "
- ltlt gcd( a, b ) ltlt "\n\n"
- // end for
- return 0
- // end main
14Follow-Up Activity 2 Solution (3/3)
- // function input definition
- void input( int x, int y )
- cout ltlt "Enter two integers "
- cin gtgt x gtgt y
- // end input
- // function gcd definition
- int gcd( int x, int y )
- int greatest 1
- for( int i 2 i lt ( ( x lt y ) ? x y ) i
) - if ( x i 0 y i 0 )
- greatest i
- return greatest
- // end gcd