8 Replies - 5984 Views - Last Post: 21 February 2011 - 02:30 PM Rate Topic: -----

#1 wdearth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-February 11

How do I get my c program to calculate area of a triangle with Heron&#

Posted 21 February 2011 - 08:18 AM

This program is supposed to find type of triangle from length of sides entered (if the sides entered form a valid triangle), and then use Heron's formula to find area. But when I enter the sides, it gives me a large number, for example I enter "1 1 1" and it outputs Area: 4519952. when it should be 0.433.

 #include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main (void)
{
	int a, b, c;
	double s, area;
	
	printf("Enter the values of the sides of the triangle: ");
	scanf( "%d %d %d", &a, &b, &c);
	if ((a+b>c && a+c>b && b+c>a) && (a>0 && b>0 && c>0))
	{
		s=(a+b+c)/2;
		area=sqrt((s*(s-a)*(s-B)/>*(s-c)));
		
		if (a==b && b==c)
		{
			printf("Equilateral Triangle. \n");
			printf("Area: %d", &area);
		}
		else if (a==b || b==c || a==c)
		{
			printf("Isosceles Triangle. \n");
			printf("Area: %d", &area);
		}
		else
		{
			printf("Scalene Triangle. \n");
			printf("Area: %d", &area);
		}
	}
	
	else 
		printf("Not a valid triangle");
	
}



how do I get it to calculate the correct area?

Is This A Good Question/Topic? 0
  • +

Replies To: How do I get my c program to calculate area of a triangle with Heron&#

#2 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: How do I get my c program to calculate area of a triangle with Heron&#

Posted 21 February 2011 - 08:40 AM

First, you have to do this :
s=(double)(a+b+c)/2;


Otherwise, the answer is typecast to int.

And you are using %d for a double. Use %lf.
Was This Post Helpful? 1
  • +
  • -

#3 wdearth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-February 11

Re: How do I get my c program to calculate area of a triangle with Heron&#

Posted 21 February 2011 - 11:04 AM

I did what you told me and now it's saying 0.000000 as the area.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main (void)
{
	int a, b, c;
	double s, area;
	
	printf("Enter the values of the sides of the triangle: ");
	scanf( "%d %d %d", &a, &b, &c);
	if ((a+b>c && a+c>b && b+c>a) && (a>0 && b>0 && c>0))
	{
		s=(double)(a+b+c)/2;
		area=sqrt((s*(s-a)*(s-B)/>*(s-c)));
		
		if (a==b && b==c)
		{
			printf("Equilateral Triangle. \n");
			printf("Area: %lf", &area);
		}
		else if (a==b || b==c || a==c)
		{
			printf("Isosceles Triangle. \n");
			printf("Area: %lf", &area);
		}
		else
		{
			printf("Scalene Triangle. \n");
			printf("Area: %lf", &area);
		}
	}
	
	else 
		printf("Not a valid triangle");
	
}


why is it doing that?
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,858
  • Joined: 25-December 09

Re: How do I get my c program to calculate area of a triangle with Heron&#

Posted 21 February 2011 - 11:12 AM

printf("Area: %lf", &area);


Two things with this code first the "%lf" format specifier is not standard for printf. Second you do not need the ampersand. With the ampersand you are trying to print out the address which is not a floating point value.


Jim

This post has been edited by jimblumberg: 21 February 2011 - 11:12 AM

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6075
  • View blog
  • Posts: 23,540
  • Joined: 23-August 08

Re: How do I get my c program to calculate area of a triangle with Heron&#

Posted 21 February 2011 - 11:17 AM

Actually, I think %lf is standard length modifier (although not required) for printf

From my Mac OS X's man 3 printf:

Quote

The following length modifier is valid for the a, A, e, E, f, F, g,
or G conversion:

Modifier a, A, e, E, f, F, g, G
l (ell) double (ignored, same behavior as without it)
L long double

Was This Post Helpful? 1
  • +
  • -

#6 wdearth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-February 11

Re: How do I get my c program to calculate area of a triangle with Heron&#

Posted 21 February 2011 - 11:22 AM

okay, problem solved, it's calculating area correctly.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,858
  • Joined: 25-December 09

Re: How do I get my c program to calculate area of a triangle with Heron&#

Posted 21 February 2011 - 11:37 AM

You are correct as long as you are using C99 or gnu extensions. However:

From this link: http://users.csc.cal...ormessages.html

Quote

warning: ISO C90 does not support the `%lf' printf
The `%lf' placeholder can only be used with scanf, not printf.


Although the ISO C99 allows the '%lf' placeholder.


Jim
Was This Post Helpful? 1
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6075
  • View blog
  • Posts: 23,540
  • Joined: 23-August 08

Re: How do I get my c program to calculate area of a triangle with Heron&#

Posted 21 February 2011 - 11:52 AM

Ah, I see, and you are indeed correct. Guess I'm so used to writing C99-standard code I failed to think of that (use %ld all the time in our code; doubles not so much).
Was This Post Helpful? 0
  • +
  • -

#9 wdearth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-February 11

Re: How do I get my c program to calculate area of a triangle with Heron&#

Posted 21 February 2011 - 02:30 PM

and one last thing, how do I get it to not display decimals when the resulting area is an integer?

here's what I have now.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main (void)
{
	int a, b, c;
	double s, area;
	
	printf("Enter the values of the sides of the triangle: ");
	scanf( "%d %d %d", &a, &b, &c);
	if ((a+b>c && a+c>b && b+c>a) && (a>0 && b>0 && c>0))
	{
		s=(double)(a+b+c)/2;
		area=sqrt((s*(s-a)*(s-B)/>*(s-c)));
		
		if (a==b && b==c)
		{
			printf("Equilateral Triangle. \n");
			printf("Area: %lf \n", area);
		}
		else if (a==b || b==c || a==c)
		{
			printf("Isosceles Triangle. \n");
			printf("Area: %lf \n", area);
		}
		else
		{
			printf("Scalene Triangle. \n");
			printf("Area: %lf \n", area);
		}
	}
	
	else 
		printf("Sides %d, %d, and %d are not valid sides of a triangle \n", a, b, c);
	
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1