From 28ffd29d4c5b5ccbfb19def617da359d551397e7 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Wed, 27 May 2020 18:51:39 -0400 Subject: [PATCH] update documentation for gcd_of_n_numbers --- others/gcd_of_n_numbers.cpp | 43 ++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/others/gcd_of_n_numbers.cpp b/others/gcd_of_n_numbers.cpp index 37e82173a..0a187f6d0 100644 --- a/others/gcd_of_n_numbers.cpp +++ b/others/gcd_of_n_numbers.cpp @@ -1,23 +1,42 @@ -// This program aims at calculating the GCD of n numbers by division method +/** + * @file + * @brief This program aims at calculating the GCD of n numbers by division + * method + * + * @see ../math/greatest_common_divisor.cpp + * greatest_common_divisor_euclidean.cpp + */ #include -int main() { - int n; - std::cout << "Enter value of n:" << std::endl; - std::cin >> n; - int *a = new int[n]; - int i, j, gcd; - std::cout << "Enter the n numbers:" << std::endl; - for (i = 0; i < n; i++) std::cin >> a[i]; - j = 1; // to access all elements of the array starting from 1 - gcd = a[0]; +/** Compute GCD using division algorithm + * + * @param[in] a array of integers to compute GCD for + * @param[in] n number of integers in array `a` + */ +int gcd(int *a, int n) { + int j = 1; // to access all elements of the array starting from 1 + int gcd = a[0]; while (j < n) { if (a[j] % gcd == 0) // value of gcd is as needed so far j++; // so we check for next element else gcd = a[j] % gcd; // calculating GCD by division method } - std::cout << "GCD of entered n numbers:" << gcd; + return gcd; +} + +/** Main function */ +int main() { + int n; + std::cout << "Enter value of n:" << std::endl; + std::cin >> n; + int *a = new int[n]; + int i; + std::cout << "Enter the n numbers:" << std::endl; + for (i = 0; i < n; i++) std::cin >> a[i]; + + std::cout << "GCD of entered n numbers:" << gcd(a, n) << std::endl; + delete[] a; return 0; }