Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

what the recursive method for quotient and remain?

0 votes
asked May 3, 2020 by jud├Ęs Tinasoady (120 points)

1 Answer

0 votes
answered Jul 13, 2020 by xDELLx (6,200 points)
#include <iostream>

using namespace std;

/*

Below function is the heart of the prog & actually does the dirty work

*/

int div(int numer, int denom, int *remainder){
    
    if (denom == 0 ) return (-1);
    
    if (numer >= denom) {
        return (1 + div(numer-denom , denom , remainder));
    }
    else{
        *remainder = numer;
        return 0;
    }
}

//Below func will handle -ve inputs

int fix_negative_input( int n, int d){
    if ( (n < 0 && d < 0 ) || (n > 0 && d >0 ) ){
        return 1;
    }
    return -1;
}
//This func is an interface to look pretty & clean ,but delegates actual work to others

//will take input as the numerator & denominator
//but after completion if will return the quotient & remainder in the same args
//return value will indicate if the operation was successful or exception occured
bool recursive_quo_remain( int *numer , int *denom){
    int r=0;
    int n=*numer>0?*numer:*numer*-1; // we could use abs(x) also
    int d=*denom>0?*denom:*denom*-1;
    int q = div(n,d,&r);
    if (q >= 0 ) {
        *numer = fix_negative_input(*numer, *denom) * q ;
        *denom = r;
        return true;
    }
    return false;
}

int main()
{
    //printf("Hello World\n");
    int r =0;
   // int q = div(9,0,&r);
   // printf ("q:%d , r:%d\n" , q,r);
   int numerator = 10;
   int denominator = 0;
   bool done = recursive_quo_remain(&numerator , &denominator );
   if (done) {
       std::cout << "q:"<< numerator <<" " << "r:"<<denominator<<std::endl;
   }
    else{
        cout <<"something messed up in computation\n";
    }
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...