My first program without consulting with the book in my avatar!

This is a discussion on My first program without consulting with the book in my avatar! within the C++ Programming forums, part of the General Programming Boards category; It's just a basic fraction program (it adds, subtracts, multiplies and divides fractions). It might not seem like much maybe ...

  1. #1
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209

    My first program without consulting with the book in my avatar!

    It's just a basic fraction program (it adds, subtracts, multiplies and divides fractions). It might not seem like much maybe not to you guys. I think it signifies that I'm proficient in basic C++ programming (without pointers and stuff). Anyway, here's the program:

    Code:
    //Fraction Math by homeyg
    
    #include <iostream>
    
    using namespace std;
    
    //function prototypes
    void displayAdd(int num1, int num2, int den1, int den2);
    void displaySub(int num1, int num2, int den1, int den2);
    void displayMul(int num1, int num2, int den1, int den2);
    void displayDiv(int num1, int num2, int den1, int den2);
    int denomChange(int run1, int run2);
    int denomChangeMul(int run1, int run2);
    int denomChangeDiv(int rise, int run);
    int numChangeAdd(int rise1, int rise2, int run1, int run2);
    int numChangeSub(int rise1, int rise2, int run1, int run2);
    int numChangeMul(int rise1, int rise2);
    int numChangeDiv(int rise, int run);
    
    int main()
    {
        //array variable declarations (array is used to perform a check to see if one of the four operators was input
        int arrayCount = 4;
        char opArray[4];
        opArray[0] = '*';
        opArray[1] = '/';
        opArray[2] = '+';
        opArray[3] = '-';
        bool check;
        
        //variable declarations for the numerators and denominators, operation input, and for the loop control
        int cont;
        int num1;
        int denom1;
        int num2;
        int denom2;
        char operation;
        
        cout << "This program performs the four main mathematical operations on fractions, nothing fancy."<<endl
             << "This program will not display mixed numbers, only improper fractions."<<endl
             << "It will also not simplify the answers."<<endl;
        
        //main loop
        while(cont!=0)
        {
            //operation input
            cout << "What operation do you want to do (*,/,+,-)? ";
            cin  >> operation;
            //test to see if one of the four valid operators is used
            //if it is not, the main loop starts over
            for(int ind = 0;ind<arrayCount; ind++)
            {
                if(operation == opArray[ind])
                {
                    check = true;
                    break;   
                }
                if(operation != opArray[ind])
                {
                    check = false;
                }    
            }
            if(check == false)
            {
                cout<<"Invalid operator."<<endl;
                continue;
            }        
            //numerator and denominator input section      
            cout << "Enter the numerator and denominator of the first fraction to be performed on."<<endl;
            cout << "Numerator: ";
            cin  >> num1;
            cout << "Denominator: ";
            cin  >> denom1;
            cout << num1 <<"/"<<denom1;
            cout << "\nEnter the numerator and denominator of the second fraction to be performed on."<<endl;
            cout << "Numerator: ";
            cin  >> num2;
            cout << "Denominator: ";
            cin  >> denom2;
            cout << num2 <<"/"<<denom2;
            //the function corresponding to the operator is called
            switch(operation)
            {
                case '+' : displayAdd(num1, num2, denom1, denom2);
                break;
                case '-' : displaySub(num1, num2, denom1, denom2);
                break;
                case '*' : displayMul(num1, num2, denom1, denom2);
                break;
                case '/' : displayDiv(num1, num2, denom1, denom2);
                break;
            }        
    
            //test if you want to continue
            cout << "Press any number to continue and 0 to quit. ";
            cin  >> cont;
        }    
        
        
        return 0;
    }
    //display functions
    /////////////////////////////////////////////
    
    void displayAdd(int num1, int num2, int den1, int den2)
    {
        cout << "\nThe answer is: "
             << numChangeAdd(num1, num2, den1, den2) <<"/"<<denomChange(den1, den2)<<endl;
    }
    
    void displaySub(int num1, int num2, int den1, int den2)
    {
        cout << "\nThe answer is: "
             << numChangeSub(num1, num2, den1, den2) <<"/"<<denomChange(den1, den2)<<endl;
    }
    
    void displayMul(int num1, int num2, int den1, int den2)
    {
        cout << "\nThe answer is: "
             << numChangeMul(num1, num2) <<"/"<<denomChangeMul(den1, den2)<<endl;
    }
    
    void displayDiv(int num1, int num2, int den1, int den2)
    {
        cout << "\nThe answer is: "
             << numChangeDiv(num1, den2) <<"/"<<denomChangeDiv(num2, den1)<<endl;
    }                       
        
    //denominator calculation functions
    /////////////////////////////////////////////
    
    int denomChange(int run1, int run2)
    {
        if(run1!=run2)
        return run1 * run2;
        if(run1==run2)
        return run1;
    }
    
    int denomChangeMul(int run1, int run2)
    {
        return run1 * run2;
    }
    
    int denomChangeDiv(int rise, int run)
    {
        return rise * run;
    }        
    
    //numerator calculation functions
    /////////////////////////////////////////////
    
    int numChangeAdd(int rise1, int rise2, int run1, int run2)
    {
        if(run1!=run2)
        {
            int firstNum = rise1 * run2;
            int secondNum = rise2 * run1;
            return firstNum + secondNum;
        }
        if(run1==run2)
        return rise1 + rise2;    
    }
    
    int numChangeSub(int rise1, int rise2, int run1, int run2)
    {
        if(run1!=run2)
        {
            int firstNum = rise1 * run2;
            int secondNum = rise2 * run1;
            return firstNum - secondNum;
        }
        if(run1==run2)
        return rise1 - rise2;
    }
    
    int numChangeMul(int rise1, int rise2)
    {
        return rise1 * rise2;
    }
    
    int numChangeDiv(int rise, int run)
    {
        return rise * run;
    }
    Do any of you remember the first program you wrote unnassisted? I did write a smaller program before, but it had many problems before it finally worked. I think I had to resolve to the forums to solve it!

    I don't know if I could figure out how to simplify the fractions or put them as mixed numbers without using another page of code so I just said forget it and went with unsimplified, improper fractions.

    I have another idea for a program, however, it requires finding the square root of a number which I also don't know how to do. Is there a special function or operator for it?
    Last edited by homeyg; 11-29-2004 at 09:06 PM.

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    sqrt()

    All of my first programs were unassisted. It hasn't been until as of late that I've been buying books left and right.

    First program I wrote I believe was a password protection system for comp at school. You had to login to the system or it would reboot the computer. I had the BIOS password protected as well.

    Most embarassing thing that happened was that since I had locked out F5 and F8 in the Windows boot process by using the /SWITCHES option when my program failed due to a NULL pointer I had to remove the case cover, remove the BIOS jumper, and reset the whole thing.

    Those were the days.
    Last edited by VirtualAce; 11-29-2004 at 09:21 PM.

  3. #3
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Whoah, I didn't know it was that simple...

    I have bought 2 books in the last month...
    Last edited by homeyg; 11-29-2004 at 09:24 PM.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Yep. Check math.h.

    So a simple distance formula could be written like this:
    Code:
    double ComputeDistance2D(double x1,double y1,double x2,double y2)
    {
      double diffx=x2-x1;
      double diffy=y2-y1;
      return sqrt((diffx*diffx)+(diffy*diffy));
    }
    I avoided data typecasts here to keep it simple.

  5. #5
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    You must have read my mind because that was exactly what I was going to use - the distance formula.

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Well the distance formula is extremely useful in computer graphics which is what I love to program.

    Using that you can find out how to make one 2D object 'fire at' another.

    I won't go into the code because it might confuse you and you seem to be doing quite well.

  7. #7
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Yeah, I don't think I'm at the graphics programming stage yet.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. URGENT: Help wanted...in C
    By iamjimjohn in forum C Programming
    Replies: 16
    Last Post: 05-18-2007, 05:46 AM
  2. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 04:23 PM
  3. Book inventory program
    By curlious in forum C++ Programming
    Replies: 8
    Last Post: 09-26-2003, 07:47 PM
  4. Books on C and C++
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-28-2002, 03:18 PM
  5. Newbie - MFC code from a book in VC++.Net
    By Guardian in forum Windows Programming
    Replies: 2
    Last Post: 04-27-2002, 07:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21