Monthly Archives: November 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

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

Gaining Familiarity with C++

Today I decided to solidify my familiarity of C++ by creating a couple rather useless, but working programs from scratch.


The first one I created solves for the hypotenuse vector of a right triangle, using the Pythagorean Theorem. This program is rather straightforward, and took little to no time to write.


// Hypotenuse

#include
#include
#include
using namespace std;

int main()
{
    int x;   //position of x-axis
    int y;   //position of y-axis
    int A;   //square A + square B
    float V;   //resultant vector
  
    cout << "Enter in both adjecants." << '\n';
    cin >> x;
    cin >> y;
    A = (x * x) + (y * y);
    V = sqrt ((double) A );
  
    cout<< V << "\n";   //output of resultant vector
    cin.get();
    cin.get();
    return 0;
}


After creating this program in a few minutes, I went on to try something a bit more challenging. This program below took me about two hours to write; a lot of debugging. At first, I tried to write the entire program within the main function, although, that didn’t really challenge me or enhance my abilities in any way, and it was extremely buggy. I decided to seperate each step in the process of matrix multiplication into seperate functions, and create my main function as a do while loop. It turned out producing correct results! Here is the code, keep in mind this only works with 2×2 matrices:


#include
using namespace std;

/*The functions in this program are fired in the order of top to bottom for readability*/

void Readmatrix(int a, int b, int c, int d, int e, int f, intg, int h)
{

/*This function records the integers placed into each matrices’ variable*/

        cout << "Enter in your first matrix (4 integers): ";
        cin >> a;
        cin >> b;
        cin >> c;
        cin >> d;
        cout << "Enter in your second matrix (4 integers): ";
        cin >> e;
        cin >> f;
        cin >> g;
        cin >> h;
      
}

void multiply(int a, int b, int c, int d, int e, int f, intg, int h)
{

/*This is the algorithm for multiplying the matrices together. It is only compatible with 2×2 matrices*/

        int a1, b1, c1, d1 = 0;
      
        a1 = ((a * e) + (b * g));
        b1 = ((a * f) + (b * h));
        c1 = ((c * e) + (d * g));
        d1 = ((c * f) + (d * h));
      
        a = a1;
        b = b1;
        c = c1;
        d = d1;

}

void Displaymatrix(int a, int b, int c, int d)
{

/*The functions simply displays the 2×2 matrix product*/

        cout << a << " " << b << '\n' << c << " " << d;
}

int main()
{

/*The functions is my main loop, which calls all the above functions*/

        char rawr;
      
        do
        {
                int a, b, c, d, e, f, g, h = 0;

                Readmatrix(a, b, c, d, e, f, g, h);
                multiply(a, b, c, d, e, f, g, h);
                Displaymatrix(a, b, c, d);
                cout << endl;
              
                cout <<"Would you like to do another matrix (y/n)? ";
                cin >> rawr;
                cout << endl;
        } while ((rawr == ‘y’) || (rawr == ‘Y’));

        return(0);
}


I feel like I’m getting a little better each day I do this! Tomorrow, I plan to write out a program that will add, subtract, and reduce fractions. I will be using a reference source code, found here: http://cplusplus.com/files/fraction.zip

I can’t wait to start moving onto programs using windows coding! I plan to become accustomed to using arrays, functions, structures, pointers, and dynamic memory before I move onto more advanced concepts though. If I can write simple programs using the tools I just listed, I’ll have some fundamental skills to use for those more advanced concepts.

Decided to keep a blog.

A friend of mine mentioned to me today that I should keep a blog of my journeys on becoming a video game programmer; I have been accepted to my top choice college: DigiPen! I’m ridiculously excited to start school there next August! I’ll be starting in a Bachelor’s Degree of CS in Real Time Interactive Simulation (RTIS). Here is a link to DigiPen’s site: https://www.digipen.edu/


The goal of this blog is to keep a detailed and prolific log of what it is like to start from no programming experience, to becoming a professional programmer.

Sadly, I already have a small amount of programming experience. My first language I ever built a program in was AS3, which is the language behind Flash. This was the ever popular Hello World program. From this point, I ventured into forums and online tutorials, and within a couple weeks of study I was able to create a simple game (which I currently still have on my flash drive), and then continue additions to the game once the tutorial was over.

I couldn’t, even if I wanted to, recode this game from scratch -I only had a deep enough understanding of AS3 to modify pre-existing code from the tutorial. So, the goal of this blog is still attainable!

After this little flash game, I stopped coding altogether.

Now, two years later, I am accepted to a great college, am learning C++ from the ground up on my spare time, and finishing up my senior year of highschool.

The reason I have chose to spend my time learning C++, is because the language that 90% of video games are made with is C and C++, not to mention C and C++ are taught extensively at DigiPen, so anything I learn now will be very valuable during college.

I’ve spent two days reading through the tutorial found on this website http://cplusplus.com/, and I’ve gained a working knowledge of all categories up to Classes(1). I’ve learned to use variables, constants, basic input/output via cin and cout, control structures (like for and while loops, if and else statements) , arrays, pointers, dynamic memory, and data structures.

While progressing through this tutorial, I take effort into not only understanding the concepts taught by the examples, but I go and create my own programs similar to the examples from scratch best I can.

Over these two days, I have written two console programs. Here is the code for the my first program created during this tutorial, it’s function is to have the user guess what number the number input by the user is being muliplied by. It only works with integers:




#include
using namespace std;

int subtraction (int a)
{
    int r;
    r = a*5;
    return (r);
}

int main ()
{
    int x, y, z;
    y = 5;
    loop:
    cout << "Enter a number into the secret equation." << '\n';
    cin >> x;
    cout << "Now I will modify the number you entered; it is now:" << '\n';
    cin.get();
    cout << subtraction (x) << " - Press enter to continue" << '\n';
    cin.get();
    cout << "Can you guess by what number I multiplied yours by?" << '\n';
    cin >> z;
  
    while (z != 5)
    {
          cout << "You guessed the wrong number!" << '\n';
          goto loop;
          }
        
    return 0;
}


Here is the code for my second program created while running through this tutorial. This program stores strings entered by the user, and then spits them back out:




#include
#include
#include
using namespace std;

struct animals_t {
    string danger;
    string size;
}dog, mouse, alligator;

void printdetails (animals_t details)
{
    cout << details.danger << '\n';
    cout << "And this large: ";
    cout << details.size;
}

int main ()
{
    string input;
  
    cout << "How large is a dog!? ";
    getline (cin,dog.size);
    cout << "How dangerous is a dog!? ";
    getline (cin,dog.danger);
    cout << "How large is a mouse!? ";
    getline (cin,mouse.size);
    cout << "How dangerous is a mouse!? ";
    getline (cin,mouse.danger);
    cout << "How large is an alligator!? ";
    getline (cin,alligator.size);
    cout << "How dangerous is an alligator!? ";
    getline (cin,alligator.danger);
    cout << '\n';
  
    cout << "A dog is this dangerous: ";
    printdetails (dog);
    cout << '\n' << '\n';
  
    cout << "A mouse is this dangerous: ";
    printdetails (mouse);
    cout << '\n' << '\n';
  
    cout << "An alligator is this dangerous: ";
    printdetails (alligator);
    cout << '\n' << '\n';
  
    cin.get();
  
    return 0;
}


I’m finding that coding in C++ is actually extremely rewarding! Just creating these two programs, from scratch, without any help has revitalized me and peaked my interest of programming even more so than it was before!

I am now currently reading an article about 2d physics engines here: http://www.gamedev.net/reference/programming/features/verletPhys/ and should finish reading it before I finally get to sleep. The article is fairly simple in terms of concepts, but the actual code is way over my head.. Ugh. I can understand the math and the concepts, but I can’t wrap my head around every bit of code presented.

Here is an extremely useful podcast about becoming a game programmer (right click save target as): https://www.digipen.edu/uploads/media/digipen_podclass_issue_37.mp3

My goal is to submit a daily entry here. Once college starts, that may change.