Another question by a begginer


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
  /*HEllo everybody. i would like to see your recommendations and your quotes.

how can i optimize this code?
what is wrong into the code?
what can it be improved?*/

//excuse me again.


/*23. 4.33 (Sides of a Triangle) Write a program that reads three
nonzero values and determines and prints whether they
could represent the sides of a triangle.
24. 4.34 (Sides of a Right Triangle) Write a program that reads three
nonzero integers and determines and prints whether they’re the
sides of a right triangle.*/
///Ejercicio 4.32; instrucciones de control; Encabezado de la clase;
///Luis Fernando Pinzon
///6/08/18

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

class Triangles{
	
	public:
		
		void decimalsTriangle()
		{	
			double sideA;
		    double sideB;
		    double sideC;
		
		    cout << "Check wether  the sides form a triangle" << endl;
		    cout << "join side A: ";
		    cin >> sideA;
		    cout << "join side B: ";
		    cin >> sideB;
		    cout << "join side c: ";
		    cin >> sideC;
		    cout << "\n";
		
		    ///According to the theorem: inequality of the triangle
		    /// A + B > C
		    /// B + C > A
		    /// A + C > B
		    if(sideA + sideB > sideC)
		    {
		        if(sideB + sideC > sideA)
		        {
		            if(sideA + sideC > sideB)
		            {
		                cout << "the sides form a triangle. " << endl;
		            }
		            else
		            {
		                cout << "Sides A y C are less than side B, therefore it's not a triangle. " << endl;
		            }
		        }
		        else
		        {
		            cout << "Sides B y C are less than side A, therefore it's not a triangle. " << endl;
		        }
		    }
		    else
		    {
		        cout << "Sides A y B are less than side C, therefore it's not a triangle. " << endl;
		    }	
		}
		
		void integerTriangles()
		{
			int sideA;
		    int sideB;
		    int sideC;
		
		    cout << "Check wether  the sides form a triangle" << endl;
		    cout << "join side A: ";
		    cin >> sideA;
		    cout << "join side B: ";
		    cin >> sideB;
		    cout << "join side c: ";
		    cin >> sideC;
		    cout << "\n";
			  
			if(sideA != 0)
			{
				///check if it is a hypotenuse.
				if(sideA > sideB)
				{
					if(sideA > sideC)
					{
						///check for The theorem of Pythagoras.
						if((sideA * sideA) == ((sideC * sideC)+(sideB * sideB)))
						{
							
							cout << "it's a right triangle. " <<  endl;
						}
						else
						{
							cout << "it's  not a right triangle. " << endl;
						}
					}
				}
			}
			else
			{
				cout << "enter a numbre different from zero " << endl;
			}	
			
			if(sideB != 0)
			{
				///check if it is a hypotenuse.
				if(sideB > sideA)
				{
					if(sideB > sideC)
					{
						///check for The theorem of Pythagoras.
						if((sideB * sideB) == ((sideC * sideC)+(sideA * sideA)))
						{
							
							cout << "it's a right triangle. " <<  endl;
						}
						else
						{
							cout << "it's  not a right triangle. " << endl;
						}
					}
				}
			}
			else
			{
				cout << "enter a numbre different from zero " << endl;
			}	
			
			if(sideC != 0)
			{
				///check if it is a hypotenuse.
				if(sideC > sideA)
				{
					if(sideC > sideB)
					{
						///check for The theorem of Pythagoras.
						if((sideC * sideC) == ((sideA * sideA)+(sideB * sideB)))
						{
							
							cout << "it's a right triangle. " <<  endl;
						}
						else
						{
							cout << "it's  not a right triangle. " << endl;
						}
					}
				}
			}
			else
			{
				cout << "enter a numbre different from zero " << endl;
			}	
		}
};

int main()
{
	Triangles objectTriangles;
	objectTriangles.integerTriangles();
	
   
    return 0;
}
My main recommendation would be to NOT stuff plain old functions inside a class for no reason. And if you do then at least make them static. But I don't see the point of putting them in a class at all.
4.33 (Sides of a Triangle) Write a program that reads three nonzero values and determines and prints whether they could represent the sides of a triangle.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <algorithm>

int main()
{
    double a, b, c ;
    std::cout << "enter the three sides: " ;
    std::cin >> a >> b >> c ;
    std::cout << "the values " << a << ", " << b << " and " << c ;

    if( a > 0 && b > 0 && c > 0 )
    {
        using std::swap ; // http://www.cplusplus.com/reference/algorithm/swap/
        if( a > b ) swap(a,b) ; // after this, b >= a
        if( b > c ) swap(b,c) ; // after this, c >= b
        // at his point, c is the largest side

        // check if the triangle inequality holds. https://en.wikipedia.org/wiki/Triangle_inequality
        if( c < (a+b) ) std::cout << " could be the sides of a triangle\n" ;
        else std::cout << " can't be the sides of a triangle (violates the triangle inequality)\n" ;
    }

    else std::cout << " are not the sides of a triangle (all values are not positive)\n" ;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <algorithm>
using namespace std;


bool isTriangle( double a, double b, double c )
{
    if ( a <= 0 || b <= 0 || c <= 0 ) return false;
    return max( { a, b, c } ) < 0.5 * ( a + b + c );      // Use <= to allow degenerate (zero-area) triangles
}


int main()
{
   double a, b, c;
   cout << "Test potential sides of a triangle\n";
   cout << "Enter a b c: ";
   cin >> a >> b >> c;
   cout << "This " << ( isTriangle( a, b, c ) ? "could" : "could not" ) << " be a triangle\n";
}


(Triangle inequality rearranged for symmetry in a, b, c).
Last edited on
"My main recommendation would be to NOT stuff plain old functions inside a class for no reason. And if you do then at least make them static. But I don't see the point of putting them in a class at all."

im sorry, im reall learning how to program on c++ i dind´t know about that though. lo siento sir, i don't understand what do you mean about the old class functions,

JLBorges thanks mrs and last chances, i will check carefully your code to understand those ways for making an efficent code.

Thank so much
Last edited on
Topic archived. No new replies allowed.