Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

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

  • 21-05-2005 6:18pm
    #1
    Registered Users, Registered Users 2 Posts: 7,501 ✭✭✭


    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,830 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 Posts: 2,426 ✭✭✭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