Thread: Triangle Perimeter (nest structure)Issue

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    11

    Post Triangle Perimeter (nest structure)Issue

    Hey, guys. I am trying to write a code that can calculate triangle perimeter. I am using nest structure to write it. Build is fine 0 error. But my result is weird always prints out some characters combine with number instead of real results. This is my code below...I am not sure which part goes wrong, pleassseee help~~~
    Code:
    #include <stdio.h>
    #include <math.h>
    #define n 3
    struct points
    {
        double x;
        double y;
    
    
    }; //set up 3 point for a triangle
    struct perimeter
    {
        struct points point[n];
    
        double tri_perimeter;
    }tri_p;
    double cal_perimeter(struct perimeter *);
    int main(void)
    {
        int i;
        struct points triangle[n];
        struct perimeter tri_p;
        for(i=0; i<n; i++)
        {
          printf("Please enter point %d of x:", i+1);
          scanf("%lf", &triangle[i].x);
          printf("Please enter point %d of y:", i+1);
          scanf("%lf", &triangle[i].y);
        }
        tri_p.tri_perimeter=cal_perimeter(&tri_p);
        printf("Result is: %lf", tri_p.tri_perimeter);
        return 0;
    }
    double cal_perimeter(struct perimeter *perimeter_ptr)
    {
    
        double side_length_1;
        double side_length_2;
        double side_length_3;
        side_length_1=sqrt(pow((perimeter_ptr->point[0].x-perimeter_ptr->point[1].x),2)+pow((perimeter_ptr->point[0].y-perimeter_ptr->point[1].y),2));
        side_length_2=sqrt(pow((perimeter_ptr->point[1].x-perimeter_ptr->point[2].x),2)+pow((perimeter_ptr->point[1].y-perimeter_ptr->point[2].y),2));
        side_length_3=sqrt(pow((perimeter_ptr->point[2].x-perimeter_ptr->point[0].x),2)+pow((perimeter_ptr->point[2].y-perimeter_ptr->point[0].y),2));
        perimeter_ptr->tri_perimeter=side_length_1+side_length_2+side_length_3;
        return perimeter_ptr->tri_perimeter;
    }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    11
    It works now..sorry, I was stupid, should not declare structure points as triangle[n]....totally mislead...here is the working code
    Code:
    #include <stdio.h>
    #include <math.h>
    #define n 3
    struct points
    {
        double x;
        double y;
    
    
    }; //set up 3 point for a triangle
    struct perimeter
    {
        struct points point[n];
    
        double tri_perimeter;
    }tri_p;
    double cal_perimeter(struct perimeter *);
    int main(void)
    {
        int i;
    
    
        for(i=0; i<n; i++)
        {
          printf("Please enter point %d of x:", i+1);
          scanf("%lf", &tri_p.point[i].x);
          printf("Please enter point %d of y:", i+1);
          scanf("%lf", &tri_p.point[i].y);
        }
        tri_p.tri_perimeter=cal_perimeter(&tri_p);
        printf("Result is: %lf", tri_p.tri_perimeter);
        return 0;
    }
    double cal_perimeter(struct perimeter *perimeter_ptr)
    {
    
        double side_length_1;
        double side_length_2;
        double side_length_3;
        side_length_1=sqrt(pow((perimeter_ptr->point[0].x-perimeter_ptr->point[1].x),2)+pow((perimeter_ptr->point[0].y-perimeter_ptr->point[1].y),2));
        side_length_2=sqrt(pow((perimeter_ptr->point[1].x-perimeter_ptr->point[2].x),2)+pow((perimeter_ptr->point[1].y-perimeter_ptr->point[2].y),2));
        side_length_3=sqrt(pow((perimeter_ptr->point[2].x-perimeter_ptr->point[0].x),2)+pow((perimeter_ptr->point[2].y-perimeter_ptr->point[0].y),2));
        perimeter_ptr->tri_perimeter=side_length_1+side_length_2+side_length_3;
        return perimeter_ptr->tri_perimeter;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Area Perimeter
    By rossipoo in forum C Programming
    Replies: 5
    Last Post: 11-09-2010, 09:20 PM
  2. bit fields issue in structure.
    By avee137 in forum C Programming
    Replies: 6
    Last Post: 09-29-2010, 09:13 AM
  3. nest if statements
    By clover in forum C Programming
    Replies: 7
    Last Post: 05-22-2004, 03:12 AM
  4. Structure Issue
    By fkheng in forum C Programming
    Replies: 2
    Last Post: 06-09-2003, 01:15 AM
  5. An class structure issue
    By conright in forum C++ Programming
    Replies: 5
    Last Post: 12-31-2002, 04:56 PM

Tags for this Thread

Website Security Test