I am having trouble with getting the math from paper to C++ code ... I am asking for user input for a numerator and a denominator .... the function then treats the variables as a fraction and reduces it to the lowest term ..... I had some math in the program and have since started over as I could not get it working .... please help ... if I can get help with the correct coding for the math equation I should be able to get the rest working ... here is what I have so far...

#include <iostream>
#include <cmath>
using namespace std;


void getNumbers(int& input1, int& input2);
//Reads two intergers fromt the keyboard.

//void

void showResults(int output1, int output2);
//Shows the values of variable1 and variable2, in that order.

int main()
{
    int firstNum, secondNum;

    getNumbers(firstNum, secondNum);

    showResults(firstNum, secondNum);
    return 0;
}

void getNumbers(int& input1, int& input2)
{
    cout << "Enter the numerator and denominator: ";
    cin >> input1
        >> input2;

}

void showResults(int output1, int output2)
{
    cout << " In numerator and the denominator are: "
         << output1 << "  " << output2 << endl;
}

Recommended Answers

All 6 Replies

Lets take test cases an see where that leads us.

<input> 4 2
<output> 4/2

We need the output to show it in the reduced term so 4/2 reduces to
2/1 which reduces to 2.

For the test case above name some things you notice about 4/2 ?

ok did some more but still have errors and this is not functioning .... need some insite here on what im doing wrong ....

#include <iostream>
#include <cmath>
using namespace std;


void getNumbers(int& input1, int& input2);
//Reads two intergers fromt the keyboard.

void reduceFraction(int &, int &);

void showResults(int output1, int output2);
//Shows the values of variable1 and variable2, in that order.

int main()
{
    int firstNum, secondNum;
    char Ans;
    do{
    getNumbers(firstNum, secondNum);
    reduceFraction(int &, int &);
    showResults(firstNum, secondNum);

   cout<<"Repeat Y/N?: ";
   cin>>Ans;
   }while( (Ans == 'Y') || (Ans == 'y'));
   return(0);
}

void getNumbers(int& input1, int& input2)
{
    cout << "Enter the numerator and denominator: ";
    cin >> input1
        >> input2;

}

void reduceFraction(int &, int &)
{
    int Low_Value, High_Value;

   if(firstNum > secondNum)
      {
	 Low_Value = secondNum;
	 High_Value = firstNum;
      }
    else
      {
	 High_Value = secondNum;
	 Low_Value = firstNum;
      }

    for(int i = Low_Value; i > 0;i--)
      {
        if( (Low_Value % i == 0) && (High_Value % i == 0) )
	    {
	       firstNum /= i;
	       secondNum /= i;
	    }
      }
}

void showResults(int output1, int output2)
{
    cout << " The lowest terms for reducing are: "
         << output1 << "  " << output2 << endl;
}

No don't make it that complicated. This is what I was suggesting :

<input> 4 2
<output> 4/2

We see that the numerator is divisible by the denominator right?
That is 4 % 2 == 0. That means we can divide both the numerator
and denominator by 2, so we get (4/2) / (2/2) = 2/1 = 2

See? Now we can repeat this process until necessary. Thats the euler
algorithm for finding GCD, greatest common divisor.

Try to implement that.

ok code is fixed... thinking that i need to use a boolean to convert to lowest terms ... getting lost in the how to take regular math thought to you programming language .... everything aside from the math is working now with no errors ... was thinking something along the lines of using something like this ... am I on the right track here...

for ( int i = 3; i <= num; i++)
    {
        for (int j = 2; j <= num; j++)
        {
            if ( i!=j && i % j == 0)

ok code is fixed... thinking that i need to use a boolean to convert to lowest terms ... getting lost in the how to take regular math thought to you programming language .... everything aside from the math is working now with no errors ... was thinking something along the lines of using something like this ... am I on the right track here...

for ( int i = 3; i <= num; i++)
{
for (int j = 2; j <= num; j++)
{
if ( i!=j && i % j == 0)

No.

More along the lines of

get lowest of num & den in loopval
set i to 2
while i <= loopval/2 
    if num%i = 0 and den%i = 0 then
        num = num /i
        den = den /i
        i = i+1

with soem thought on the loop to get it correct.... :icon_wink:

Thanks for the help and the poke in the right dirrection ... took advice and tweeked it program works great now ... now if I could only figure out how to put solved on here it would be great...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.