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

I need a little more help. I had someone on here help me but I need to know how

ID: 3861799 • Letter: I

Question

I need a little more help. I had someone on here help me but I need to know how to get the program to let the user input the fraction and have the program output the postion of the fraction in the tree. The code I had help with is posted at the end of this.

This program is supposed to output the Calkin-Wilf tree. In other words it's supposed to show the fractions such as 1/1 and then next level is 1/2 and 2/1 ect

Please help with this thanks.

Please do not write it out I have a hard time reading other peoples hand writing please just type it out. thanks

write a c++ program that implements and tests the following two functions related to the Calkin-Wilf enumeration of the positive fractions:

The two functions below are supposed to be used in a new c++ program. These two functions need to be in a new program and tested. Please HELP!!!

Fraction cwfrac(int p);
//Returns the fraction in position p in the Calkin-Wilf enumeration.

int cwpos(Fraction f);
//Returns the position of the fraction f in the Calkin-Wilf enumeration.

Here is the code:

#include <iostream>

using namespace std;

//Class representing the fraction in Calkin-Wilf sequence

// the sequence is obtained as Q(i+1)=1/(2*integral_part(qi)-qi+1)

//if qi is written as a/b, then the above equation can be

//written as 1/2*integral_part(qi)-(a/b)+1

//simplifying it becomees q(i+1)=b/(2*integral-part(qi)-a+b)

class Fraction

{

int numerator;

int denominator;

public:

Fraction() //default construcor

{

numerator=denominator=1;

}

Fraction(int n,int d)

{

numerator=n;

denominator=d;

}

int getNumerator()

{

return numerator;

}

int getDenominator()

{

return denominator;

}

void display()

{

cout<<" "<<numerator<<"/"<<denominator;

}

};

Fraction cwfrac(int p)

{

if(p==1)

return Fraction(1,1);

else

{

int n,d,a=1,b=1,intpart;

for(int i=2;i<=p;i++)

{

intpart=a/b;

n=b; //the new numerator accordig to formula

d=2*intpart*b-a+b; // new denominator

a=n; //set a and b for next term

b=d;

}

return Fraction(n,d);

}

}

//funtion to return position of a fraction in calkin tree

//it works by continuously generating a raction and keeping track of which position it is

// and compares with the given fractiion each tme a new fraction is generated. If matched

//returns the position. A VALID fraction in the sequence need to be provided otherwise

//the program goes on generating new fractions till it finds a match

int cwpos(Fraction p)

{

if(p.getNumerator()==1 && p.getDenominator()==1)

return 1;

else

{

int pos=2;

int n,d,a=1,b=1,intpart,n1=p.getNumerator(),d1=p.getDenominator();

while(true)

{

intpart=a/b;

n=b; //the new numerator accordig to formula

d=2*intpart*b-a+b; // new denominator

a=n; //set a and b for next term

b=d;

if(n==n1 && d==d1)

return pos;

pos++;

}

}

}

int main()

{

for(int i=1;i<15;i++)

cwfrac(i).display();

cout<<" position of 2/1= "<<cwpos(Fraction(2,1));

system("pause");

return 0;

}

Explanation / Answer

#include <iostream>
using namespace std;

//Class representing the fraction in Calkin-Wilf sequence

// the sequence is obtained as Q(i+1)=1/(2*integral_part(qi)-qi+1)

//written as 1/2*integral_part(qi)-(a/b)+1

//simplifying it becomees q(i+1)=b/(2*integral-part(qi)-a+b)

class Fraction {

    int numerator;
    int denominator;

public:
    Fraction() {
        numerator=denominator=1;
    }

    Fraction(int n,int d) {
        numerator=n;
        denominator=d;
    }

    int getNumerator() {
        return numerator;
    }

    int getDenominator() {
        return denominator;
    }

    void display() {
        cout<<" "<<numerator<<"/"<<denominator;
    }
};

Fraction cwfrac(int p)
{
    if(p == 1)
        return Fraction(1,1);
    else {
        int n, d, a = 1, b = 1, intpart;
        for(int i=2;i<=p;i++) {
            intpart = a/b;
            n = b; //the new numerator accordig to formula
            d = 2*intpart*b-a+b; // new denominator
            a = n; //set a and b for next term
            b = d;
        }
    return Fraction(n,d);
    }
}

//funtion to return position of a fraction in calkin tree

//it works by continuously generating a raction and keeping track of which position it is

// and compares with the given fractiion each tme a new fraction is generated. If matched

//returns the position. A VALID fraction in the sequence need to be provided otherwise

//the program goes on generating new fractions till it finds a match

int cwpos(Fraction p)

{
    if(p.getNumerator()==1 && p.getDenominator()==1)
        return 1;

    else {
        int pos=2;
        int n,d,a=1,b=1,intpart,n1=p.getNumerator(),d1=p.getDenominator();
        while(true) {
            intpart=a/b;
            n=b; //the new numerator accordig to formula
            d=2*intpart*b-a+b; // new denominator
            a=n; //set a and b for next term
            b=d;
            if(n==n1 && d==d1)
                return pos;
            pos++;
        }
    }

}

int main()

{
    int Num , Den;
    cout << "Enter fraction"<< endl;
    cout << "Numerator ";
    cin >> Num;
    cout << "Denominator ";
    cin >> Den;
    cout << "Position of fraction " << Num <<"/" << Den <<" is "<< cwpos(Fraction(Num, Den)) << endl;
    return 0;
}

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