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;
}

Dani AI

Generated

Quick, practical addendum that fills the gaps left in the thread: reducing a fraction reliably means (1) validating input, (2) normalizing the sign so the denominator is positive, and (3) dividing numerator and denominator by their greatest common divisor (GCD). was correct pointing to GCD; 's divisor-scan idea works but is slower than Euclid's algorithm. The main bugs seen in Post #3 were calling the function as reduceFraction(int&, int&) (a prototype style, not a call) and using names inside the reducer that weren't the function's parameters.

Key rules to follow

  • Call the reducer with actual variables: reduceFraction(firstNum, secondNum); and declare the prototype/definition with parameter names (for example void reduceFraction(int &n, int &d);).
  • Refuse/handle den == 0. Decide how the program reports that error.
  • If n == 0, represent the result as 0/1.
  • Keep the denominator positive: if d < 0 flip signs before reducing.

Example implementations (C++17 and pre-C++17):

// C++17+: simplest
#include <numeric>   // std::gcd
#include <cstdlib>   // std::abs

void reduceFraction(int &n, int &d) {
    if (d == 0) return;              // caller must handle error
    if (n == 0) { d = 1; return; }
    if (d < 0) { n = -n; d = -d; }
    int g = std::gcd(std::abs(n), std::abs(d));
    n /= g; d /= g;
}
// pre-C++17: Euclid's algorithm
int gcd(int a, int b) {
    a = std::abs(a); b = std::abs(b);
    while (b) { int t = a % b; a = b; b = t; }
    return a;
}

Quick test cases to verify behavior: 4 2 -> 2/1, 0 5 -> 0/1, 6 -8 -> -3/4, 5 0 -> invalid.

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.