Thread: New to Programming - Simple Math Program Issue

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    70

    New to Programming - Simple Math Program Issue

    Hello! I am new here and to programming as a whole. I am using GCC on Fedora to do a summer semeseter in C Programming. I am currently learning "if statements" while doing a program that calculates the area of a triangle. My problem is that the result is always "0". I am pretty sure the problem lies within my math, but I can't figure out how I have set it up wrong. Thanks for reading/looking.

    I am using Heron's Formula. Here it is for reference:

    area=sqrt(s(s-a)(s-b)(s-c)) where s=(a+b+c)/2

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main ()
    {
        
    float area, sideA, sideB, sideC, halfP;
    
    printf("\n This program will calculate the area of a triangle.");
    printf("Please enter the value for the first side: ");
    scanf("%f", &sideA);
    printf("Please enter the value for the second side: ");
    scanf("%f", &sideB);
    printf("Please enter the value for the third side: ");
    scanf("%f", &sideC);
    
    if (0 > sideA)
        printf("The distance must be positive. \n");
    else if (0 > sideB)
        printf("The distance must be positive. \n");
    else if (0 > sideC)
        printf("The distance must be positive. \n");
    else
        halfP = (sideA+sideB +sideC)/2;
        area = sqrt(halfP*(halfP-sideA)*(halfP-sideB)*(halfP-sideC));
        printf("\n The area of the triangle is %f \n", &area);
    
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    The last printf() should not have an & before area:

    Code:
    /* ... */
        printf("\n The area of the triangle is %f \n", area);
    Also the last else needs braces so that it applies to the next 3 lines:
    Code:
    /* ... */
        else{
            halfP = (sideA+sideB+sideC)/2;
            area = sqrt(halfP*(halfP-sideA)*(halfP-sideB)*(halfP-sideC));
            printf("\n The area of the triangle is %f \n", area);
        }
    Last edited by rcgldr; 06-10-2013 at 08:44 PM.

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    It actually worked fine once I removed the "&" from the last printf line on area. I am a noobie, but I don't think you need the brackets if the "else" line is part of a if-else chain. When I tried adding brackets it just gave me errors when I compiled. When I left them off again it worked fine.

    EDIT: Ok, I see what you were saying about adding brackets to just the "else" part. That made it to where if someoen entered a number <0 it didn't continue to try and compute and print the result. Thanks!

    [cameron@localhost Program3]$ ./Exercise6

    This program will calculate the area of a triangle.Please enter the value for the first side: 5
    Please enter the value for the second side: 5
    Please enter the value for the third side: 5

    The area of the triangle is 10.825317
    A bit sloppy looking, but I will clean it up now that it is working haha.
    Last edited by Cameron Taylor; 06-10-2013 at 09:08 PM.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    This isn't needed, but you could combine the 0 > number checks and eliminate the need for that final else:

    Code:
    /* ... */
        if(0 > sideA || 0 > sideB || 0 > sideC){
            printf("The distance must be positive. \n");
            return 0;
        }
    
        halfP = (sideA+sideB +sideC)/2;
        area = sqrt(halfP*(halfP-sideA)*(halfP-sideB)*(halfP-sideC));
        printf("\n The area of the triangle is %f \n", area);

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Ah HA!, I didn't think about using hte "or" statement. I was tapping my head trying to think of how to put them all on one "if" statement. Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with math
    By Paul Omans in forum C Programming
    Replies: 8
    Last Post: 05-22-2013, 10:38 PM
  2. Simple C program issue...
    By mbxs3 in forum C Programming
    Replies: 2
    Last Post: 02-02-2013, 10:41 PM
  3. simple programming issue!
    By ke121885 in forum C Programming
    Replies: 5
    Last Post: 09-27-2009, 06:57 PM
  4. Issue with pointer math
    By Kudose in forum C Programming
    Replies: 6
    Last Post: 03-18-2008, 04:25 AM
  5. Simple Math Program
    By RazielX in forum C Programming
    Replies: 3
    Last Post: 03-04-2004, 06:22 PM