fork download
  1.  
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. class GCD {
  6. private:
  7. int X[20];
  8. int Y[20];
  9. int n;
  10. int G[20];
  11. public:
  12. int calcGCD(int a, int b) {
  13. if (b == 0) return a;
  14. return calcGCD(b, a % b);
  15. }
  16.  
  17. void read() {
  18. cout << "Enter the number of elements (max 20): ";
  19. cin >> n;
  20.  
  21. for (int i = 0; i < n; i++) {
  22. cout << "Enter X[" << i+1 << "]: ";
  23. cin >> X[i];
  24. cout << "Enter Y[" << i+1 << "]: ";
  25. cin >> Y[i];
  26.  
  27. if (X[i] <= 0 || Y[i] <= 0) {
  28. cout << "Error: X and Y must be positive integers. Try again." << endl;
  29. i--;
  30. continue;
  31. }
  32.  
  33. G[i] = calcGCD(X[i], Y[i]);
  34. }
  35. }
  36.  
  37. void display() {
  38. cout << "X\tY\tGCD" << endl;
  39. for (int i = 0; i < n; i++) {
  40. cout << X[i] << "\t" << Y[i] << "\t" << G[i] << endl;
  41. }
  42. }
  43.  
  44. int MinGCD() {
  45. int minGCD = G[0];
  46. for (int i = 1; i < n; i++) {
  47. if (G[i] < minGCD) minGCD = G[i];
  48. }
  49. return minGCD;
  50. }
  51.  
  52. void compareMinGCD(GCD obj) {
  53. int minGCD1 = MinGCD();
  54. int minGCD2 = obj.MinGCD();
  55.  
  56. if (minGCD1 < minGCD2)
  57. cout << "Object 1 has the minimum GCD." << endl;
  58. else if (minGCD2 < minGCD1)
  59. cout << "Object 2 has the minimum GCD." << endl;
  60. else
  61. cout << "Both objects have the same minimum GCD." << endl;
  62. }
  63. };
  64.  
  65. int main() {
  66. GCD obj1, obj2;
  67.  
  68. cout << "Enter data for Object 1:" << endl;
  69. obj1.read();
  70. obj1.display();
  71.  
  72. cout << "Enter data for Object 2:" << endl;
  73. obj2.read();
  74. obj2.display();
  75.  
  76. obj1.compareMinGCD(obj2);
  77.  
  78. return 0;
  79. }
  80.  
Success #stdin #stdout 0.01s 5272KB
stdin
1
4
5
2
3
4
5
6
stdout
Enter data for Object 1:
Enter the number of elements (max 20): Enter X[1]: Enter Y[1]: X	Y	GCD
4	5	1
Enter data for Object 2:
Enter the number of elements (max 20): Enter X[1]: Enter Y[1]: Enter X[2]: Enter Y[2]: X	Y	GCD
3	4	1
5	6	1
Both objects have the same minimum GCD.