Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,380 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,478 people online right now. Registration is fast and FREE... Join Now!




Distance formula!

 
Reply to this topicStart new topic

Distance formula!

hogben45
4 Feb, 2008 - 10:05 PM
Post #1

New D.I.C Head
*

Joined: 29 Jan, 2008
Posts: 25

Ok, I know this is my second post in a week or so...but this is killing me. I am trying to write a program to calculate the 3 sides of a triangle using the distance formula and the area of the triangle using Heron's formula. Here is my code so far...

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

    main ()
    
    {

    printf("\n\nThis program is the work of Ben Hogan , I.D. #211");

    double a , b , c;

    a = sqrt(3.0 - 1.5)**2 + (0.25 - 2.5)**2;
    b = sqrt(3.0 - 1.0)**2 + (0.25 - 1.0)**2;
    c = sqrt(1.5 - 1.0)**2 + (2.5 - 1.0)**2;

    printf("\nThe value of side a is : %g" , a);
    printf("\nThe value of side b is : %g" , b);
    printf("\nThe value of side c is : %g" , c);

    double    per , x , area;

    per = a + b + c;

    x = .5 * (a + b + c);

    area = sqrt(x * (x - a) * (x - b) * (x - c);

    printf("\nThe perimeter of the triangle is: %g" , per);

    printf("\nThe area of the triangle is: %g" , area);

    system("pause");

    }




Using Microsoft Visual studio , I receive 15 error messages!!! I have no idea what I have done wrong, thank you all so much. I have the error messages if anyone would like to see them blink.gif
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Distance Formula!
4 Feb, 2008 - 10:09 PM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
yes, you should post the error messages.
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Distance Formula!
4 Feb, 2008 - 10:21 PM
Post #3

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
You can't calculate a number raised to a power using **. You'll need to either write out the expression twice (e.g. sqrt(3.0 - 1.5)*sqrt(3.0 - 1.5) ) or use the pow() function from <cmath> ( pow(sqrt(3.0 - 1.5),2.0) ). Note that you need to pass the proper data types to pow, or it will give you an error (in this case, sqrt is returning a double, so a literal double 2.0 is also passed as the second argument).
User is offlineProfile CardPM
+Quote Post

hogben45
RE: Distance Formula!
5 Feb, 2008 - 08:14 AM
Post #4

New D.I.C Head
*

Joined: 29 Jan, 2008
Posts: 25

OK, I read when I was searching throughout this site that you could square something by using the ** notation...Anyways, here are my error messages...

CODE
1>Compiling...
1>area_perimeter.c
1>c:\users\ben\documents\visual studio 2005\projects\heron_triangle\heron_triangle\area_perimeter.c(11) : error C2143: syntax error : missing ';' before 'type'
1>c:\users\ben\documents\visual studio 2005\projects\heron_triangle\heron_triangle\area_perimeter.c(13) : error C2065: 'a' : undeclared identifier
1>c:\users\ben\documents\visual studio 2005\projects\heron_triangle\heron_triangle\area_perimeter.c(13) : error C2100: illegal indirection
1>c:\users\ben\documents\visual studio 2005\projects\heron_triangle\heron_triangle\area_perimeter.c(13) : error C2100: illegal indirection
1>c:\users\ben\documents\visual studio 2005\projects\heron_triangle\heron_triangle\area_perimeter.c(13) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
1>c:\users\ben\documents\visual studio 2005\projects\heron_triangle\heron_triangle\area_perimeter.c(14) : error C2065: 'b' : undeclared identifier
1>c:\users\ben\documents\visual studio 2005\projects\heron_triangle\heron_triangle\area_perimeter.c(14) : error C2100: illegal indirection
1>c:\users\ben\documents\visual studio 2005\projects\heron_triangle\heron_triangle\area_perimeter.c(14) : error C2100: illegal indirection
1>c:\users\ben\documents\visual studio 2005\projects\heron_triangle\heron_triangle\area_perimeter.c(14) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
1>c:\users\ben\documents\visual studio 2005\projects\heron_triangle\heron_triangle\area_perimeter.c(15) : error C2065: 'c' : undeclared identifier
1>c:\users\ben\documents\visual studio 2005\projects\heron_triangle\heron_triangle\area_perimeter.c(15) : error C2100: illegal indirection
1>c:\users\ben\documents\visual studio 2005\projects\heron_triangle\heron_triangle\area_perimeter.c(15) : error C2100: illegal indirection
1>c:\users\ben\documents\visual studio 2005\projects\heron_triangle\heron_triangle\area_perimeter.c(15) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
1>c:\users\ben\documents\visual studio 2005\projects\heron_triangle\heron_triangle\area_perimeter.c(21) : error C2143: syntax error : missing ';' before 'type'
1>c:\users\ben\documents\visual studio 2005\projects\heron_triangle\heron_triangle\area_perimeter.c(23) : error C2065: 'per' : undeclared identifier
1>c:\users\ben\documents\visual studio 2005\projects\heron_triangle\heron_triangle\area_perimeter.c(25) : error C2065: 'x' : undeclared identifier
1>c:\users\ben\documents\visual studio 2005\projects\heron_triangle\heron_triangle\area_perimeter.c(25) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
1>c:\users\ben\documents\visual studio 2005\projects\heron_triangle\heron_triangle\area_perimeter.c(27) : error C2065: 'area' : undeclared identifier
1>c:\users\ben\documents\visual studio 2005\projects\heron_triangle\heron_triangle\area_perimeter.c(27) : error C2143: syntax error : missing ')' before ';'
1>c:\users\ben\documents\visual studio 2005\projects\heron_triangle\heron_triangle\area_perimeter.c(27) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
1>c:\users\ben\documents\visual studio 2005\projects\heron_triangle\heron_triangle\area_perimeter.c(33) : warning C4013: 'system' undefined; assuming extern returning int
1>Build log was saved at "file://c:\Users\Ben\Documents\Visual Studio 2005\Projects\Heron_Triangle\Heron_Triangle\Debug\BuildLog.htm"
1>Heron_Triangle - 15 error(s), 6 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Distance Formula!
5 Feb, 2008 - 05:00 PM
Post #5

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
OK, first
CODE
main()
should be
CODE
int main()
and you should
CODE
return 0;
at the end of the main.
NOTE I know some compiler regard main() as int main() but the standard is int main().

For
CODE
system("pause");
you'll need to
CODE
#include <stdlib.h>


But I suggest you don't use that, instead read the suggestions in this thread:

Holding The Execution Window Open

Since this is C, declare all the variables at the beginning.
User is offlineProfile CardPM
+Quote Post

hogben45
RE: Distance Formula!
5 Feb, 2008 - 05:20 PM
Post #6

New D.I.C Head
*

Joined: 29 Jan, 2008
Posts: 25

Thanks PennyBoki , my program runs now but I am not getting the right output. My output shows as follows:


CODE

This program is the work of Ben Hogan , I.D. #211
The value of side a is : -1.#IND
The value of side b is : -1.#IND
The value of side c is : 2
The perimeter of the triangle is: -1.#IND
The area of the triangle is: -1.#IND
Press any key to continue . .


That cant be right. Thanks in advance.

Edit: Nevermind this post...I have solved the problem...Thanks alot guys.

This post has been edited by hogben45: 5 Feb, 2008 - 05:32 PM
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Distance Formula!
5 Feb, 2008 - 05:30 PM
Post #7

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
please post the updated code and post what the program should look like.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:33AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month