Thread: Was recommended here, I'm learning C and require some help.

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    1

    Was recommended here, I'm learning C and require some help.

    Hi everyone, I'm a little bit stumped. Please forgive me if the code is poorly written, I did learn C++ about 4 years ago and I currently have to take a C course so I am very rusty.

    I'm getting an error I can't figure out. I will give you a rundown of what the program is supposed to do.

    The user enters 6 numbers that represent x and y coordinates, with this I need to determine the area of the triangle that is formed.

    This is what I currently have, included in this is a rule that checks to see if no triangle is formed in which case it tells that to the user. If all things are good then it calculates the area.


    The error I'm getting when compiling is as follows: error: called object "S - a" is not a function
    I'm also having problems with the values not coming out as decimal points, for example if you enter:

    x1 = 3
    x2 = 2
    y1 = 4
    y2 = 5

    The answer to this is definitely 1.41 but the program spits out -2.00.

    Code:
    #include <stdio.h>
    
    int main (void)
    {
    
        /*Here we declare our values*/
    
        int x1;
        int x2;
        int x3;
    
        int y1;
        int y2;
        int y3;
    
        float a;
        float b;
        float c;
        float S;
        float T;
    
    
    /*Here we will ask the user for the co-ordinates of 3 points*/
    
        printf("Input the x-coordinate of the first point: ");
        scanf("%d", &x1);
    
        printf("Input the y-coordinate of the first point: ");
        scanf("%d", &y1);
    
        printf("Input the x-coordinate of the second point: ");
        scanf("%d", &x2);
    
        printf("Input the y-coordinate of the second point: ");
        scanf("%d", &y2);
    
        printf("Input the x-coordinate of the third point: ");
        scanf("%d", &x3);
    
        printf("Input the y-coordinate of the third point: ");
        scanf("%d", &y3);
    
    
    /*We will now compute the distance between each point in order to obtain 3 lengths which we will then use in Heron's Formula to solve for the area of the triangle*/
    
        a=((x2-x1)^2+(y2-y1)^2)^1/2;
        scanf("%f", &a);
        b=((x3-x1)^2+(y3-y1)^2)^1/2;
        scanf("%f", &b);
        c=((x3-x2)^2+(y3-y2)^2)^1/2;
        scanf("%f", &c);
    
        printf("Side a is: %5.2f units.", a);
        printf("Side b is: %5.2f units.", b);
        printf("Side c is: %5.2f units.", c);
    
        if ((a+b)>c)
        {
           printf("The sides do not fulfill the requirements of the inequality theorum, it is not possible to form a triangle with the specified co-ordinates.");
    
        }
    else
        if ((b+c)>a)
        {
           printf("The sides do not fulfill the requirements of the inequality theorum, it is not possible to form a triangle with the specified co-ordinates.");
    
        }
    else
        if ((a+c)>b)
        {
           printf("The sides do not fulfill the requirements of the inequality theorum, it is not possible to form a triangle with the specified co-ordinates.");
    
        }
    
    else
    {
        /*Here we are solving for "S", which will be used in Heron's formula*/
    
        S=((a+b+c)/2);
        scanf("%f", &S);
    
        /*Now we use Heron's formula to solve for the area, T*/
    
        T=((S*(S-a)*(S-b)*(S-c)))^1/2;
    
        printf ("The area of your triangle is: %5.2f units.", T);
    
    }
    
    
    return 0;
    }
    Where am I faulting, also since I don't understand exactly why its happening if you could provide me with a reason (if its not too much trouble) I would prefer that. I'd rather understand than just get an answer.

    Thank you in advance.
    Last edited by Seeki; 09-25-2012 at 04:36 PM.

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    In C there is no exponentiation symbol. You either call the pow() function (declared in <math.h>) or, for the numbers you are raising to the power 2, multiply the number by itself.

    Also 1/2 is integer division; you need to make a floating-point division.

    Code:
     /* a = ((x2-x1)^2 + (y2-y1)^2) ^ 1/2;                    NO!!! */
        a = pow((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1), 1.0/2); /* ok */
    Last edited by qny; 09-25-2012 at 05:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Require help with several errors (c2106, C4305)
    By Arsalan Ahmad in forum C++ Programming
    Replies: 13
    Last Post: 10-07-2011, 09:14 AM
  2. Require help decrypting XOR.
    By mkthnx001 in forum C++ Programming
    Replies: 15
    Last Post: 05-17-2009, 07:15 PM
  3. why do we require sleep?
    By jinx in forum A Brief History of Cprogramming.com
    Replies: 43
    Last Post: 07-14-2004, 08:21 AM
  4. Require information!!!!
    By sameer in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2002, 06:51 AM