Hello,

I have this program that simplifies a fraction by calculating the greatest common divisor.
So the fraction 8/4 (largest common divisor = 4)
8/4 4/4 => result would be 2/1 (fraction simplified)

My problem is my method calculated this greatest common divisor.
But now that value has to be divided trough the nominator and denominator of the fraction (wich are a and b in my example).

But i cannot seem to get that dividing to work. (will i need to make a new method or simply in this one...)
Also calling up that method (another small class below the algorithm).

public ulong GenormBreuk(ulong a, ulong b)
    {
      ulong remainder;

      while (b != 0)
      {
        remainder = a % b;
        a = b;
        b = remainder;
      }
      return a;

      //part that is not working....
      ulong gcd = GenormBreuk(a, b);
      a = a / gcd;
      b = b / gcd;
      
   
    }/*GreatestCommonDivisor*/

Also i will call up this class in a other class (with soley conscode that contains writelines, readlines etc...).
I do not understand what arguements to pass along with that, are out parameters required?
Atm if i call up the class ber.GenormsBreuk(n , p) i will just get the value fo the GcD. Its unclear how i have to modifiy my method & the 2 arguements for below to show the result of the simplified fraction.

So the last line should contain the simplified fraction {0} for the nominator / {1} would contain the simplified denominator.

public void TestGCD()
    {
      ClsBereken Ber = new ClsBereken();
      Console.WriteLine("\tTest GCD");
      Console.WriteLine("\t--------");

      Console.WriteLine("Give in a fraction where the denominator & the denominator\nare natural numbers different from 0\n");
      Console.Write("\tNominator...");
      ulong n = ulong.Parse(Console.ReadLine());
      Console.Write("\tDenominator...");
      ulong p = ulong.Parse(Console.ReadLine());

      Console.WriteLine("\nFraction in original shape: {0}/{1}", n , p);

      Console.WriteLine("Simplified fraction {0}/{1}", Ber.GenormBreuk(n, p) , ....?);
    }

Regards.

The reason it is not working is because of Line 11.
You return the Greatest Common Divisor here, the code after that never gets executed.
By luck you did not get a stack overflow, because of the recursive call on Line 14. Your gcd calculation is OK(Line 3-11), call this method gcdCalculation or something and let it do just that, calculate a gcd. Here are two other methods to do that but for the moment you should stick to yours.

The reason it is not working is because of Line 11.
You return the Greatest Common Divisor here, the code after that never gets executed.
By luck you did not get a stack overflow, because of the recursive call on Line 14. Your gcd calculation is OK(Line 3-11), call this method gcdCalculation or something and let it do just that, calculate a gcd. Here are two other methods to do that but for the moment you should stick to yours.

Hello, and thanks for replying.

I see only the value a gets calculated with the method.

But the GcD method only gives 1 value (wich in this case is the gcd which equals a)

But how exactly can i make a method that takes those 2 values (nominator and demonitor) in check.
I am probarly looking it to far, but this method has to return 2 values right? (wich will the divided value of the nominator and the dominator).

So i have tried working with out-parameters (so i can return more then 1 value) but i keep getting errors while trying to implent the calculated value of the previous method.

Not really sure how to get the value from the first method into the 2nd method ( to simplify ) and then return those 2 calculated final results as the eventual answer.

public void SimplifyFraction(out ulong nominator, out ulong denominator)
    {
      //not sure on this line right below the above method returns the GCD value
// but how to actually call this in this method to work with?
      ulong gcd =  GenormBreuk(a, b); // How come i cant call up this method with GeNormBreuk(a, b) ?
      nominator = nominator / gcd;
      denominator = denominator / gcd;
    }

Kind regards.

Any method can only return one "unit", be it an ulong or an object. It can never return two values.
Have to go now the wife just prepared some dinner(dinner time in BE) See ya.

In C# a function can return only one value. You're passing the fraction into the simply function as a numerator and a denominator and the function is unable to return both.

Alternatives would be to 'package' the numerator and denominator into a struct or class which you could pass in and return, or you could make the parameters reference parameters allowing the function to make changes 'in-place' in the variables passed to the function.

This is as simple as I could get this I think.
Hope you can follow, if not : ask!

using System;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            
            Fraction F = new Fraction(152, 128); //make the fraction 152/128
            Console.WriteLine("Numerator = {0} Denominator = {1}", F.GetNumerator(), F.GetDenominator());
            F.Simplify();
            Console.WriteLine("After simplification:");
            Console.WriteLine("Numerator = {0} Denominator = {1}", F.GetNumerator(), F.GetDenominator());
            Console.ReadKey();         
        }

        // ////////////////////////////////////////////
        class Fraction
        {
            private ulong _numerator;
            private ulong _denominator;

            //default creator
            public Fraction()
            {
                _numerator = 0;
                _denominator = 1; //cannot be zero
            }

            //create a Fraction from known numbers
            public Fraction(ulong numer, ulong denom)
            {
                try
                {
                    _numerator = numer;
                    if (denom == 0)
                        throw new ArithmeticException("You cannot use a zero denominator to contruct a Fraction");
                    else
                        _denominator = denom; 
                }
                catch (ArithmeticException e)
                {
                    Console.WriteLine(e.ToString());
                } 
            }

            // greatest common divisor calculation
            //in this case we don't need it outside, so it is private here
            private ulong gcd(ulong a, ulong b) 
            {
                ulong remainder;
                while (b != 0)
                {
                    remainder = a % b;
                    a = b;
                    b = remainder;
                }
                return a;
            }

            //return nothing just perform an action on the class
            public void Simplify()
            {
                ulong factor = gcd(_numerator,_denominator);
                _numerator /= factor;
                _denominator /= factor;             
            }

            //old style but I don't know if you know "properties"
            public ulong GetNumerator()
            {
                return _numerator;
            }

            public ulong GetDenominator()
            {
                return _denominator;
            }
        }
        

        public static ulong gcd(ulong a, ulong b)
        {
            ulong remainder; 
            while (b != 0) 
            { 
                remainder = a % b; 
                a = b; 
                b = remainder; 
            } 
            return a;
        }
    }
}

Hello Dan,

Thanks for the well written comment.

The only part thats unclear to me is the parameters with _ in front of them. I have never seen such a thing before in any code so im wondering what does that exactly mean?

Also the method GCD in your code, you call it with gcd but how come you call it with (_numerator,_denominator)? How come (a, b) is not valid? I thought i could put this calculation in 2 methods (one for the gcd calculatino and one to actually calculate the simpliy fraction and end result)

I call up that method but it says a and b do not exist in the context. Is this because the method only returns one value or?)
Basicly im still confused how to "call" those 2 parameters that will be given in at the beginning (the nominator and denominator and take those 2 orignal value's to eventuelly divide them by the gcd)

public void Simplify() 
    { 
      ulong factor = gcd(_numerator, _denominator); 
      _numerator /= factor; 
      _denominator /= factor; 
    }

Kind regards.

The underscore _ is a char that can be used in variable names. I (among others) use it to denote a private field of a class. It is some sort of convention for myself, you don't have to follow it. A well know convention in C# is to let all interfaces begin with I. So this _ may look strange but it has no special meaning.

The a and b in the gcd method declaration are just placeholders for the real values used in the call to gcd in the Simplify() method. So you could define a gcd method with a and b and then call it with gcd(45,65) or whatever numbers or variables you want, it would always return the greatest common divisor of the two numbers or variables.
Hope this helps.

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.