+ Reply to Thread
Results 1 to 2 of 2

Thread: Basic Triangle Math [c++]

  1. #1
    Disharmony is offline Newbie
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Basic Triangle Math [c++]

    I'm just starting to get used to object oriented programming. So I figured one of the easiest ways to practice would be to make a program to find the perimeter and/or area of a triangle.

    I was going to add a "do...while loop" to exit, as well as a menu for whether or not you wanted to find the area or perimeter, but I figured that would be a waste of time, because the point was for me to learn about Objects, not deal with loops.

    Code:
    /* Basic Triangle functions code by Disharmony */ 
    // DisHarm0ny[at]yahoo.com
    
    #include <cstdlib>
    #include <iostream>
    
    //defining the class "triangle"
    class triangle
    {
          public:
                 int getSide1();
                 int getSide2();
                 int getSide3();
                 float getBase();
                 float getHeight();
                 void setVars();
                  
                 
                 
          private:
                 float height;
                 int base;
                 int side1;
                 int side2;
                 int side3;
                 float area_value;
                 int perimeter_value;
    };
    
    void triangle::setVars()
    {
        std::cout << "What is the value of side1? \n";
        std::cin >> side1;
        std::cout << "What is the value of side2? \n";
        std::cin >> side2;
        std::cout << "What is the value of side3? \n";
        std::cin >> side3;
        std::cout << "What is the value of base? \n";
        std::cin >> base;
        std::cout << "What is the height of the triangle? \n";
        std::cin >> height;
        
    }
    
    
    
    int triangle::getSide1()
    {
        return side1;
    }
    
    int triangle::getSide2()
    {
        return side2;
    }
    
    int triangle::getSide3()
    {
        return side3;
    }
    
    float triangle::getBase()
    {
        return base;
    }
    
    float triangle::getHeight()
    {
        return height;
    }
    // Done defining the "triangle" class
    
    
    
    
    
    
    float area(float,float);
    int perimeter(int ,int ,int );
    triangle tri;
    using std::cout;
    using std::cin;
       
    
    int main()
    {
        
       tri.setVars(); //prompt for variables
       
       int side1=tri.getSide1(); //getting the private member variables from public methods
       int side2=tri.getSide2();
       int side3= tri.getSide3();
       float base = tri.getBase();
       float height = tri.getHeight();
       
    
       int perimeter_value = perimeter(side1, side2, side3);// calculate perimeter and area
       float area_value = area(height, base);
       
       
       cout << "The triangle's perimeter is: " << perimeter_value <<"\n";
       cout << "The triangle's area is: " << area_value << "\n";
       cout << "------------------------------------\n";
       
        
        
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
    float area(float height,float base)
    {
        float result = height*base;
        float area_value = result / 2;
        return area_value;
    }
    
    int perimeter(int one,int two, int three)
    {
        int perimeter_value;
        perimeter_value = one + two + three;
        
        return perimeter_value;
    }

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,478
    Blog Entries
    75
    Rep Power
    143

    Re: Basic Triangle Math [c++]

    I would only have the user enter the three sides. The software can then calculate the three corresponding heights and the three angles from that information.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Triangle Enumeration
    By Jrb in forum C and C++
    Replies: 4
    Last Post: 04-04-2011, 11:51 AM
  2. Replies: 31
    Last Post: 01-06-2011, 05:08 PM
  3. Triangle?
    By Hamed in forum C and C++
    Replies: 1
    Last Post: 10-07-2010, 10:05 AM
  4. coordinates inside/outside triangle
    By skirmish in forum Pascal and Delphi
    Replies: 5
    Last Post: 09-12-2010, 01:27 PM
  5. Java Triangle
    By DEViANT in forum Java Help
    Replies: 5
    Last Post: 08-15-2010, 06:21 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts