Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program A3p3.cpp with a class that is a derived class of the class i

ID: 3788604 • Letter: W

Question

Write a C++ program A3p3.cpp with a class that is a derived class of the class in problem 2. Add a private int member var2 to this class. Initialize the member variables var and var2 with a value between 1 and 50 using a constructor. Add a public member function called getgcd that should print out the greatest common divisor of var and var2. (You can refer to gcd.c in Folio’s sample codes directory for how to compute the greatest common divisor of two integers.) In your main function, create an object of this class, initialize the members var and var2 with values on the command line arguments (that is, argv[1] and argv[2]) and then call the play and getgcd functions.

QUESTION 2:

#include <iostream>

using namespace std;
class Question2
{
protected: int var;
public:
   void play();
   Question2(int i){
       var = i;
   }
     
  
};
   void Question2::play(){
cout << var << " "<< " ";
       while(var != 1){
           if (var%2 == 0)
               var = var/2;
           else
               var = 3*(var)+1;
           cout << var << " " << " ";
       }
}

  
int main(int argc, char *argv[]){
   int value2;
   value2 = atoi(argv[1]);
   if( value2 < 1 || value2 > 50){
       cout << "Out of bounds ";
       exit(0);
   }
   Question2 value(value2);
   value.play();
}

THIS IS WHAT I HAVE FOR QUESTION 3:

i keeo getting a floating point exception.

#include <iostream>


using namespace std;
class Question2
{
protected: int var;
public:
   void play();
   Question2(int i){
       var = i;
   }
     
  
};
   void Question2::play(){
cout << var << " " << " ";
       while(var != 1){
           if (var%2 == 0)
               var = var/2;
           else
               var = 3*(var)+1;
           cout << var << " " << " ";
       }
}

class Question3: public Question2
{
protected: int var;
private: int var2;
public:
   void getGCD();
   Question3(int i, int j): Question2(i){
       var = i;
       var2 = j;
   }
     
  
};
void Question3::getGCD(){
   cout <<"GCD is: " << var << " " << var2;
int a, b, t, gcd, value2, value3;
var = value2; var2 = value3;
   while (b != 0) {
       t = var2;
       var2 = var % var2;
       var = t;
   }
       gcd = var;
}
  
  
int main(int argc, char *argv[]){
   int value2;
   int value3;
   value2 = atoi(argv[1]);
   value3 = atoi(argv[2]);
   if( value2 < 1 || value2 > 50){
       cout << "Out of bounds ";
       exit(0);
   }else if (value3 < 1 || value3 >50){
       cout << "Out of bounds ";
       exit(0);
   }
  
   Question3 value(value2, value3);
   value.getGCD();
   value.play();
  
}

  

Explanation / Answer

You are getting a floating point exception because of "var % var2" when var2=0 (divide by zero) in function Question3::getGCD().

Also, in same function, you are using while (b != 0), which will not have any effect as b is never used.

Solution for both the issues is to use: while (var2 != 0).

Here is the modified function:

void Question3::getGCD(){
    cout <<"GCD is: " << var << " " << var2;
    int a, b, t, gcd, value2, value3;
    var = value2; var2 = value3;

    while (var2 != 0) {
        t = var2;
        var2 = var % var2;
        var = t;
    }
    gcd = var;

}

Here is the complete code (Combining Question 2 and Question 3). The number of arguments will decide which one to invoke.

#include <iostream>

using namespace std;

class Question2
{
    protected: int var;
    public:
        void play();
        Question2(int i){
            var = i;
        }
};

void Question2::play(){
    cout << var << " "<< " ";
    while(var != 1){
        if (var%2 == 0)
            var = var/2;
        else
            var = 3*(var)+1;

        cout << var << " " << " ";
    }
}

class Question3: public Question2
{
    protected: int var;
    private: int var2;
    public:
        void getGCD();
        Question3(int i, int j): Question2(i){
            var = i;
            var2 = j;
        }
};

void Question3::getGCD(){
    cout <<"GCD is: " << var << " " << var2;
    int a, b, t, gcd, value2, value3;
    var = value2; var2 = value3;

    while (var2 != 0) {
        t = var2;
        var2 = var % var2;
        var = t;
    }
        gcd = var;
}
  
int main(int argc, char *argv[]){
    int value2;
    int value3;

    cout << "argc: " << argc << endl;
    switch(argc){
      case 2:   // Argc will be 2 in Question 2
      {
       int value2;
        value2 = atoi(argv[1]);
        if( value2 < 1 || value2 > 50){
            cout << "Out of bounds ";
            exit(0);
        }
        Question2 value(value2);
        value.play();
        break;
      }

      case 3: // Argc will be 3 in Question 3
      {
        value2 = atoi(argv[1]);
        value3 = atoi(argv[2]);
        if( value2 < 1 || value2 > 50){
          cout << "Out of bounds ";
          exit(0);
        }else if (value3 < 1 || value3 >50){
          cout << "Out of bounds ";
          exit(0);
        }
        Question3 value(value2, value3);
        value.getGCD();
        value.play();
        break;
      }

      default:
        cout << "Wrong number of arguments." << endl;
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote