Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Unable to understand reason for certain piece of c++ code?

  • 21-05-2005 07:18PM
    #1
    Registered Users, Registered Users 2 Posts: 7,544 ✭✭✭


    This code is used to find the max area of a scalene triangle of perimeter 300meters.

    #include <windows.h>
    #include <conio.h>
    #include <math.h>
    #include <iostream.h>
    
    float calculate_area(float sidea, float sideb, float sidec);
    
    int main(void)
    {
    	float sidea, sideb, sidec;
    	float maxarea = 0, area, maxa, maxb, maxc;
    
    	while(sidea<=300)
    	{
    		for(sideb=1;sideb<300;sideb++)
    		{
    			for(sidec=1;sidec<300;sidec++)
    			{
    				if((sidea+sideb+sidec) == 300)
    				{
    					if(sidea < 150 && sideb < 150 && sidec < 150)
    					{
    
    						area = calculate_area(sidea, sideb, sidec);
    						if(area > maxarea)
    						{
    							maxarea = area;
    							maxa = sidea;
    							maxb = sideb;
    							maxc = sidec;
    						}
    					}
    				}
    			}
    		}
    		sidea++;
    	}
    	clrscr();
    	cout<<"Side A = "<<maxa<<endl;
    	cout<<"Side B = "<<maxb<<endl;
    	cout<<"Side C = "<<maxc<<endl;
    	cout<<"Max Area = "<<maxarea<<endl;
    	system("pause");
    	cout<<"\n";
    return 0;
    }
    
    
    float calculate_area(float sidea, float sideb,float sidec)
    {
    	float area = 0, areatemp=0,temp=0;
    	temp = (sidea + sideb + sidec) / 2;
    	areatemp = temp*((temp-sidea)*(temp-sideb)*(temp-sidec));
    	area = sqrt(areatemp);
    	return area;
    }
    
    

    In the code above there is an if statement that i dont understand but without it the program runs but gives an error "sqrt: DOMAIN error". The if statement in question is
    if(sidea < 150 && sideb < 150 && sidec < 150)
    {
    
    	area = calculate_area(sidea, sideb, sidec);
    	if(area > maxarea)
    	{
    		maxarea = area;
    		maxa = sidea;
    		maxb = sideb;
    		maxc = sidec;
    	}
    }
    

    If anyone knows the reason for this error could they please inform me. Thanks


Comments

  • Registered Users, Registered Users 2 Posts: 441 ✭✭robfitz


    if(sidea < 150 && sideb < 150 && sidec < 150)
    

    This stops the length of any sides being greater then 149. If the length of a side is greater then or equal to 150 the shape can't be a triangle with perimeter length 300.

    If the length of a side is greater then 150, this causes areatemp in calculate_area to become a negative number. sqrt(x) fails when x is a negative number and sets errno to EDOM (Math argument out of domain of function).

    	float sidea, sideb, sidec;
    	float maxarea = 0, area, maxa, maxb, maxc;
    
    	while(sidea<=300)
    

    Also sidea is being used before it is properly assigned.


  • Technology & Internet Moderators Posts: 28,859 Mod ✭✭✭✭oscarBravo


    In the code above there is an if statement that i dont understand but without it the program runs but gives an error "sqrt: DOMAIN error".
    Walk through the code logically: it's a good habit to get into. First, this line:
    temp = (sidea + sideb + sidec) / 2;
    
    is effectively redundant. The function only gets executed if the sum of the three side is 300; temp will always equal 150. If any of the sides is greater than 150, (temp-sidex) will be less than zero. If this is the case, then areatemp will be less than zero, and the sqrt() function will fail.


  • Registered Users, Registered Users 2, Paid Member Posts: 2,427 ✭✭✭ressem


    The reason for the if statement is to stop values of sides that cannot be present in a triangle.

    sides a+b > c, a+c>b, b+c>a, otherwise you don't get a triangle, because the two shortest sides can't meet. Hence the sqrt exception.


    Nerdy efficiency BS:

    You're using Heron's formula, and feeding in every value of a,b,c less than 150.

    Why bother with the c loop and a+b+c check?

    Why just not loops for a (< 150)), and (b > (150-a), b < 150) then set c = 300-a-b?
    Then all should be valid. You can make it more efficient, e.g. take symmetry into account, but that might be going too far for a simple assignment.


Advertisement