Hello would someone please help me with my program, I'm stuck all day with this thing. The program runs, but I think the problem is a logical error. The fraction 1/4 + 2 1/2 should be equal to 2 3/4 but the program's result is 2 3/8. Another thing is the expression 1/8 + 2 1/2 should be equals to 2 5/8. Please help me how to fix the codes. Thank you very much!

using System;

class FractionDemo
{
    static void Main(string[] args)
    {
        Fraction firstfraction = new Fraction();
        Fraction secondfraction = new Fraction();
        firstfraction.Numerator = 1;
        firstfraction.Denominator = 4;
        secondfraction.Numerator = 1;
        secondfraction.Denominator = 8;
        secondfraction.WholeNumber = 2;
        Fraction add = new Fraction();
        add = firstfraction + secondfraction;

        Console.Write("\n {0}/{1}", firstfraction.Numerator, firstfraction.Denominator);
        Console.WriteLine(" + {0}/{1} = {2}/{3}", secondfraction.Numerator, secondfraction.Denominator, add.Numerator, add.Denominator);
        Console.Write("\n {0}/{1}", firstfraction.Numerator, firstfraction.Denominator);
        Console.WriteLine(" + {0} {1}/{2} = {3} {4}/{5}", secondfraction.WholeNumber, secondfraction.Numerator,secondfraction.WholeNumber, add.WholeNumber, add.Numerator, add.Denominator);
        Console.Write("\n {0}/{1}", firstfraction.Numerator, secondfraction.Denominator);
        Console.WriteLine(" + {0} {1}/{2} = {3} {4}/{5}", secondfraction.WholeNumber, secondfraction.Numerator, secondfraction.WholeNumber, add.WholeNumber, add.Numerator, add.Denominator);
        Console.ReadLine();

    }

    public class Fraction
    {
        private int wholenumber;
        private int numerator;
        private int denominator;

        public int WholeNumber
        {
            get
            {
                return wholenumber;
            }
            set
            {
                wholenumber = value;
            }
        }

        public int Numerator
        {
            get
            {
                return numerator;
            }
            set
            {
                numerator = value;
            }
        }

        public int Denominator
        {
            get
            {
                return denominator;
            }
            set
            {
                denominator = value;
                if (denominator > 0)
                {
                    denominator = value;
                }
                else
                {
                    denominator = 1;
                }
            }
        }

        public Fraction(int wholenumber, int numerator, int denominator)
            : this(numerator, denominator)
        {
            WholeNumber = wholenumber;
        }

        public Fraction(int numerator, int denominator)
        {
            WholeNumber = 0;
            Numerator = numerator;
            Denominator = denominator;
        }

        public Fraction()
        {
            WholeNumber = 0;
            Numerator = 0;
            Denominator = 1;
        }

        public int gcd()
        {
            int x = Numerator;
            int y = Denominator;
            int m;

            if (x > y)
                m = y;
            else
                m = x;

            for (int i = m; i >= 1; i--)
            {
                if (x % i == 0 && y % i == 0)
                {

                    return i;

                }
            }
            return 1;
        }

        public void Reduce()
        {
            int gcdNum = gcd();

            if (gcdNum != 0)
            {
                Numerator = Numerator / gcdNum;
                Denominator = Denominator / gcdNum;
            }
            if (Denominator < 0)
            {
                Denominator = Denominator * -1;
                Numerator = Numerator * -1;
            }
            convertFraction();
        }

        public void convertFraction()
        {
            WholeNumber = Numerator / Denominator;
            Numerator = Numerator % Denominator;
        }

        public static Fraction operator +(Fraction firstfraction, Fraction secondfraction)
        {
            int firstNum = (firstfraction.WholeNumber * firstfraction.Denominator) + firstfraction.Numerator;
            int secondNum = (secondfraction.WholeNumber * secondfraction.Denominator) + secondfraction.Numerator;

            Fraction Result = new Fraction();

            Result.Numerator = firstNum * secondfraction.Denominator + firstfraction.Denominator * secondNum;
            Result.Denominator = firstfraction.Denominator * secondfraction.Denominator;
            Result.Reduce();
            return Result;
        }
    }
}

Hi,

The issue that you're facing is not a logical error in your "Fraction" class, it is just you didn't print the values properly.

Look at line 20 of your code:

Console.WriteLine(" + {0} {1}/{2} = {3} {4}/{5}", secondfraction.WholeNumber, secondfraction.Numerator,secondfraction.WholeNumber, add.WholeNumber, add.Numerator, add.Denominator);

you print secondfraction.WholeNumber in field {2} instead of secondfraction.Denominator.. so the real number is (2 1/8) but you're printing it (2 1/2) and nothing wrong in the result (2 3/8)

Just change that line to:

Console.WriteLine(" + {0} {1}/{2} = {3} {4}/{5}", secondfraction.WholeNumber, secondfraction.Numerator, secondfraction.Denominator, add.WholeNumber, add.Numerator, add.Denominator);

Note: I didn't review all of your code.. I just checked that problem.

Good Luck!

Edited 2 Years Ago by The Diamonds

Comments
Great help!

I needed the expression 2 1/2, if put in the secondfraction.Denominator it will now display 2 1/8. What I want to happen is to get the result of 1/4 + 2 1/2. How can I do that?

Simply change the second number (lines 11-13):

secondfraction.Numerator = 1;
secondfraction.Denominator = 2;
secondfraction.WholeNumber = 2;

EDIT:
or create a third number with (2 1/2) value, you don't want to affect previous results.

Edited 2 Years Ago by The Diamonds

I tried to define a new secondfraction1.Denominator which is equal to 2, but still it doesn't displays the correct answer

Ok, when you use third number you need to add new fraction variable to hold the results of the second summation:

// third number
Fraction thirdfraction = new Fraction();
thirdfraction.WholeNumber = 2;
thirdfraction.Numerator = 1;
thirdfraction.Denominator = 2;

// second summation value holder
Fraction secondSum = new Fraction();
secondSum = firstfraction + thirdfraction;

// print the result
Console.Write("\n {0}/{1}", firstfraction.Numerator, firstfraction.Denominator);
            Console.WriteLine(" + {0} {1}/{2} = {3} {4}/{5}", thirdfraction.WholeNumber, thirdfraction.Numerator, thirdfraction.Denominator, secondSum.WholeNumber, secondSum.Numerator, secondSum.Denominator);

If I read this right, your fraction with the whole number should be 22/8 from your overload of the + operator. Hint one.

The problem is in your reduce method. 22/8 = x/GCD or 22/8 = x/4 or (22 X 4)/8 = x or 11 = x. Hint two.

Now 11 is your new numerator and 4 is your denominator. When you convert this, you get 2 for your whole number and 3 as your numerator.

What you have is 22/4 = 5 as your new numerator and 8/4 = 2 as your new denominator. When you convert this, you get 2 for the whole number and 1 for your numerator.

I would also rename gcd to GreatestCommonDenominator, change it to private and change Reduce and ConvertFraction to private as well.

Try adding an print method like

public string Print()
{
    return Wholenumber + ' ' + Numerator + '/' + Denominator;
}

Just some best practices as you would not want someone to override your internal methods and you want a neat way to display the fraction easier.

Good luck!

This article has been dead for over six months. Start a new discussion instead.