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

Enter data for Object 2:
Enter the number of elements (max 20): X	Y	GCD

Object 2 has the minimum GCD.
X	Y	GCD