fork(2) 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. }
  42.  
  43. int MinGCD() {
  44. int minGCD = G[0];
  45. for (int i = 1; i < n; i++) {
  46. if (G[i] < minGCD) minGCD = G[i];
  47. }
  48. return minGCD;
  49. }
  50.  
  51. void compareMinGCD(GCD obj) {
  52. int minGCD1 = MinGCD();
  53. int minGCD2 = obj.MinGCD();
  54.  
  55. if (minGCD1 < minGCD2)
  56. cout << "Object 1 has the minimum GCD." << endl;
  57. else if (minGCD2 < minGCD1)
  58. cout << "Object 2 has the minimum GCD." << endl;
  59. else
  60. cout << "Both objects have the same minimum GCD." << endl;
  61. }
  62. };
  63.  
  64. int main() {
  65. GCD obj1, obj2;
  66.  
  67. cout << "Enter data for Object 1:" << endl;
  68. obj1.read();
  69. obj1.display();
  70.  
  71. cout << "Enter data for Object 2:" << endl;
  72. obj2.read();
  73. obj2.display();
  74.  
  75. obj1.compareMinGCD(obj2);
  76.  
  77. return 0;
  78. }
  79.  
Success #stdin #stdout 0s 5292KB
stdin
Standard input is empty
stdout
Enter data for Object 1:
Enter the number of elements (max 20): X	Y	GCD
Enter data for Object 2:
Enter the number of elements (max 20): X	Y	GCD
Both objects have the same minimum GCD.