Sunday, November 29, 2009

Fraction Addition

Today, I created a program that adds two fractions together, and then reduces them. This took me about 2 hours to complete, and about 25 minutes to write. Yep, that means I sat there and debugged the program for over an hour and a half. Sheesh, I hope this isn't what programming is always like ;)


The way this program works, is I set up the main function to call all of the other functions. However, in function Addition, I call the reduce function. This is because I needed to pass on the data from the addition function directly into the reduce function. If I were to call the reduce fraction from within the main function, the private data of function addition wouldn't have been passed onto being reduced. Here is my code:



#include<iostream.h>

void InputData (int &Num,int &Denom,int &Num2,int &Denom2)
{

//This function takes data from the user.

    cout << "Enter the numerator of the first fraction." << '\n';
    cin >> Num;
    cout << "Enter the denominator of the first fraction." << '\n';
    cin >> Denom;
    cout << "Enter the numerator of the second fraction." << '\n';
    cin >> Num2;
    cout << "Enter the denominator of the second fraction." << '\n';
    cin >> Denom2;
    cout << '\n';
}

void Reduce (int &Num,int &Denom,int &Num2,int &Denom2)
{

//This function reduces both fractions, before adding them.

    int a, b, c, d, i, j = 0;
    
    a = Num;
    b = Denom;
    c = Num2;
    d = Denom2;
    
    for (i = a * b; i > 1; i--)
    {
        if ((a % i == 0) && (b % i == 0))
        {
            a /= i;
            b /= i;
        }
    }
    
    for (j = c * d; j > 1; j--)
    {
        if ((c % j == 0) && (d % j == 0))
        {
            c /= j;
            d /= j;
        }
    }
    Num = a;
    Denom = b;
    Num2 = c;
    Denom2 = d;
}

void Reduce (int &Num,int &Denom)
{

//This function reduces the added fractions.

    int a = 0;
    int b = 0;
    int i = 0;
    
    a = Num;
    b = Denom;
    
    for (i = a * b; i > 1; i--)
    {
        if ((a % i == 0) && (b % i == 0))
        {
            a /= i;
            b /= i;
        }
    }
    Num = a;
    Denom = b;
}

void Addition (int &Num,int &Denom,int &Num2,int &Denom2)
{

//This adds both of the raw fractions. In order to do this, you need to make sure both the numerator and denominator are equal.

    if (Denom != Denom2)
    {
        Num = Num * Denom2;
        Num2 = Num2 * Denom;
        Denom = Denom * Denom2;
        Denom2 = Denom2 * Denom;
        Num = Num + Num2;
    }
    else
    {
        Num = Num + Num2;
    }
    Reduce (Num, Denom);
}

void OutputData (int &Num,int &Denom)
{

//This function simply displays the final fraction.

    cout << Num << "/" << Denom << '\n';
}

int main ()
{

//This is the main loop, it will keep firing as long as char a is equal to y or Y.

    char a;
    
    do{
        int Num, Denom, Num2, Denom2 = 0;
    
        InputData (Num, Denom, Num2, Denom2);
        Reduce (Num, Denom, Num2, Denom2);
        Addition (Num, Denom, Num2, Denom2);
        OutputData (Num, Denom);
    
        cout << "Would you like to add two more fractions (y/n)?" << '\n';
        cin >> a;
    }while ((a == 'y') || (a == 'Y'));
    
    return 0;
}


And there she is! The program should work without the & symbol in the functions' parameters, my reference code used it, so so did I :)

While creating this program, I used this source as a reference, although, I made sure to only look at it when I completely forgot how to do something, and throughout when I was debugging. Here is the link to my reference source code: http://cplusplus.com/files/fraction.zip

Speaking of that bug, take a look at what I had wrong! Look up in my source code, you see that red code? That is what I had mixed up, here is what I had when my code was bugged:





    Num = a;
    Denom = b;
    Num2 = a;
    Denom2 = b;

Pretty ridiculous eh? That simple, tiny, infinitesimal error cost me over an hour and a half of debugging!

That is what I completed for the day. While creating that, I gained understanding of the % (modulo) symbol, and one of it's practical uses. I also gained an understanding of a very useful loop; the do while loop. The do while loop, which is what my entire main function was constructed of, will complete it's task, and then complete a comparison. So basically, the while loop makes the comparison before it executes code, and the do while loop executes its code before the comparison.

Tomorrow, I shall work on this source code: http://cplusplus.com/files/rectangle.zip

That code demonstrates creating a square with tokenizing. His code looks pretty ugly, hopefully I can come up with something much nicer, using his code as a reference.


Here's an interesting read I'll hopefully finish up before the end of tonight! It's about game engines. I doubt I'll actually put any of this into practice any time soon, but it's nice to read things like this: http://www.gamasutra.com/view/feature/4199/book_excerpt_game_engine_.php

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.