Thread: Program to determine area,perimeter of geometric shapes using coordinates

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    9

    Program to determine area,perimeter of geometric shapes using coordinates

    Hello,

    I am only 2 weeks into programming so I am at a very basic level but I am very willing to learn and apply any help and advice.

    I have to write a program that determines the area and perimeter of a rectangle, circle, and triangle.

    The user will choose a shape by enter 1-3, or 4 to exit.

    after selecting the shape the user must enter the x,y coordinates.(the program assumes that the user only enters coordinates that actually makes sense)

    I have chosen the distance formula as my algorithm to find the length of sides/radius/height, then use the standard area/perimeter formulas.

    My problem is mainly setting up the menu system, and debugging my formulas because the error log says "invalid operands to binary."
    I would be very grateful for any help/advice. Thanks.

    Here is my code so far:
    Code:
    #include <stdio.h>
    #include <math.h>
    void main()
    //Program to determine area and perimeter of geometric shapes using the x,y coordinates
    
    {
    int choice,x1,y1,x2,y2,x3,y3,x4,y4;
    
    
    float a,b,c,r,A,P,l,w;
    // user chooses shape.
    printf("Welcome to Cartesian geometry! Please choose your shape by entering its number:\n");
    printf("rectangle -> enter 1\n");
    printf("circle -> enter 2\n");
    printf("triangle -> enter 3\n");
    printf("to exit -> enter 4\n");
    scanf("%d", &choice);
    
    
    if (choice==1)
        {
        prinf("Please enter the coordinates for the rectangle from bottom left to right, then top right to left");
        printf("\nenter value for x1:");
        scanf("%d", &x1);
        printf("enter value for y1:");
        scanf("%d", &y1);
        printf("enter value for x2:");
        scanf("%d", &x2);
        printf("enter value for y2");
        scanf("%d", &y2);
        printf("enter value for x3:");
        scanf("%d", &x3);
        printf("enter value for y3:");
        scanf("%d", &y3);
        printf("enter value for x4:");
        scanf("%d", & x4);
        printf("enter value for y4:");
        scanf("%d", &y4);
        l = (((x1-x2)^2 + (y1-y2)^2))^(1/2);
        w = (((x1-x4)^2 + (y1-y4)^2))^(1/2);
        A=l*w;
        printf("the area is %f\n", A);
        P=2*(l+w);
        printf("the perimeter is %f", P);
        }
    if (choice==2)
        {
        printf("Please enter the centre of the circle, and a point on the circle\n");
        printf("enter value for x1\n");
        scanf("%f", &x1);
        printf("enter value for y1\n");
        scanf("%f", &y1);
        printf("enter value for x2\n");
        scanf("%f", &x2);
        printf("enter value for y2\n");
        scanf("%f", &y2);
    
    
        r=(((x2-x1)^2 + (y2-y1)^2))^(1/2);
        A=3.14*(r)^2;
        printf("\nThe Area of the triangle is %f", A);
        P=2*3.14*r;
        printf("The Perimeter of the triangle is %f", P);
    
    
    if (choice==3)
        {
        printf("Please enter the x,y coordinates of the triangle starting with the bottom left to right, then the top:");
        scanf("%f", &x1);
        printf("\nenter value for y1");
        scanf("%f", &y1);
        printf("please enter value for x2");
        scanf("%f", &x2);
        printf("please enter value for y2");
        scanf("%f", &y2);
        printf("enter value for x3");
        scanf("%f", &x3);
        printf("enter value for y3");
        scanf("%f", &y3);
    
    
        a=((x2-x1)^2 + (y2-y1)^2))^(1/2);
        b=((x3-x2)^2 + (y3-y2)^2))^(1/2);
        c=((x3-x1)^2 + (y3-y1)^2))^(1/2);
    
    
        A=(.25)*(((a^2+b^2+c^2))^(2) - (a^4+b^4+c^4))^(1/2);
        printf("The area is %f", A);
        P=a+b+c;
        printf("The perimeter is %f", P);
    
    
    if (choice>=4)
        {
        return 0;
        }
    }

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    C is not a math parser, it is a programming language. '^' has nothing to do with raising to a power. I suggest you look at the functions available to you in the math library.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    For the menu system I have often seen it implemented as a while loop, which runs until the input character is equal to the quit-item. Something like this:
    Code:
    char inp;
    print_menu();
    inp = scanf("%c", &inp);
    while (inp != 'q') { /* 'q' for quit */
        switch (inp) {
        case '1':
            do_menuitem_one_stuff();
            break;
        case '2':
            ....
            break;
        default:
            /* handle invalid menu option */
            break;
        }
        print_menu();
        inp = scanf("%c", &inp);
    }

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    9
    I've done some more work on it, thanks to the advice. My only question is how to exit the program when the user chooses to? and here's what I have now:
    Code:
     #include <stdio.h> /* Lab 1 program to find the Area/Perimeter of Rectangles, Triangles, Circles, depending on user input*/#include <math.h>
    void main()
    {
    
    
    
    
    //program to calculate area of rectangle/cirlce/triangle using Cartesian coordinates entered by user
    int choice;
    float x1,y1,x2,y2,x3,y3,x4,y4,side_1,side_2,side_3,r,A,P; /*declare variables*/
    
    
    //ask user for the choice of shape
    
    
    
    
    printf("Welcome to Cartesian geometry! Please choose your shape by entering its number:\n");
    printf("Rectangle -> enter 1\n");
    printf("Circle -> enter 2\n");
    printf("Triangle -> enter 3\n");
    printf("Exit Program -> enter 4\n");
    scanf("%d", &choice);
    
    
    if (choice==1) /*The User enters the Cartesian coordinates of the rectangle*/
        {
        printf("Please enter the coordinates for the rectangle from bottom left to right, then top right to left");
        printf("\nenter value for x1:");
        scanf("%f", &x1);
        printf("enter value for y1:");
        scanf("%f", &y1);
        printf("enter value for x2:");
        scanf("%f", &x2);
        printf("enter value for y2:");
        scanf("%f", &y2);
        printf("enter value for x3:");
        scanf("%f", &x3);
        printf("enter value for y3:");
        scanf("%f", &y3);
        printf("enter value for x4:");
        scanf("%f", & x4);
        printf("enter value for y4:");
        scanf("%f", &y4);
    
    
        side_1=pow(pow((x2-x1), 2) + pow((y2-y1), 2), .5); /*Calculate the length of side 1 using the distance formula*/
        side_2=pow(pow((x3-x2), 2) + pow((y3-y1), 2), .5); /*Calculate the length of side 2 using the distance formula*/
    
    
        A= side_1*side_2;
        printf("The Area is %f", A); /* Display the Area and Perimeter*/
    
    
        P=2*(side_1+side_2);
        printf("The Perimeter is %f", P);
        }
    
    
    if (choice==2)
        {
        printf("You have chosen a cirlce, please enter the coordinates of the centre, then a point on the circle\n"); /* User enters the coordinates of the circle*/
        printf("enter value for x1\n");
        scanf("%f", &x1);
        printf("enter value for y1\n");
        scanf("%f", &y1);
        printf("enter value for x2\n");
        scanf("%f", &x2);
        printf("enter value for y2\n");
        scanf("%f", &y2);
    
    
        r=pow(pow((x2-x1), 2) + pow((y2-y1), 2), .5); /* Calculate radius using the distance formuala*/
    
    
        A=3.141*(pow(r, 2));
        printf("\n the area is %f", A); /* Display Area and Perimeter*/
    
    
        P=2*3.141*r;
        printf("\n the perimeter is %f", P);
        }
    
    
    if (choice==3)
        {
         printf("You have chosen a triangle, please enter in the coordinates of its vertices from bottom left to right, then top."); /*User enters coordiantes for the triangle*/
         printf("\nEnter value for x1");
         scanf("%f", &x1);
         printf("\nplease enter value for y1:");
         scanf("%f", &y1);
         printf("\nplease enter value for x2:");
         scanf("%f", &x2);
         printf("\nplease enter value for y2:");
         scanf("%f", &y2);
         printf("\nplease enter value for x3:");
         scanf("%f", &x3);
         printf("\nplease enter value for y3:");
         scanf("%f", &y3);
    
    
         side_1=pow(pow((x2-x1), 2) + pow((y2-y1), 2), 0.5); /*Calculate length of side 1*/
         side_2=pow(pow((x3-x2), 2) + pow((y3-y2), 2), 0.5); /*Calculate length of side 2*/
         side_3=pow(pow((x3-x1), 2) + pow((y3-y1), 2), 0.5); /*Calculate length of side 3*/
    
    
         A=0.25*pow(pow(pow(side_1, 2) + pow(side_2, 2) + pow(side_3, 2), 2) - 2*(pow(side_1, 4) + pow(side_2, 4) + pow(side_3, 4)), 0.5); /*Area of triangle using Heron's formula*/
         printf("the area is %f", A);    /* Display the Area and Perimeter*/
    
    
         P= side_1 + side_2 + side_3;
         printf("the perimeter is %f", P);
        }
    
    
    if (choice==4) /* User can chooses to exit*/
        return(0);
    }

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    You might use exit() function; it might be considered bad programming if used in the wrong place by some people.
    I did not really look at your code.

    exit - C++ Reference

    Note: Exit exists in normal C; I find searching on c++ and the function works faster for me to find links to post.

    Tim S.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by cda67 View Post
    I've done some more work on it, thanks to the advice. My only question is how to exit the program when the user chooses to? and here's what I have now:
    As I said in my previous post, I would do as posted below. Is it required that the user can quit anywhere in the program, or just at the menu?

    Code:
    #include <stdio.h> 
    
    
    void print_menu()
    {
        printf("Welcome to Cartesian geometry! Please choose your shape by entering its number:\n");
        printf("Rectangle -> enter 1\n");
        printf("Circle -> enter 2\n");
        printf("Triangle -> enter 3\n");
        printf("Exit Program -> enter 4\n");
    }
    int main(void)
    {
        int choice;
        print_menu();
        scanf("%d", &choice);
    
    
        while(choice != 4)
        {
            if (choice == 1) /*The User enters the Cartesian coordinates of the rectangle*/
            {
               /* ... */
            }
    
    
    
    
            else if (choice == 2)
            {
                /* ... */
            }
    
    
    
    
            else if (choice == 3)
            {
              /* ... */
            }
            
            print_menu();
            scanf("%d", &choice);
        }
        
        return 0;
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's more common to put the choice at the beginning of the menu line, not the end:
    Code:
    printf( "1. do one thing\n" );
    printf( "2. do other thing\n" );
    ...
    Also, a do-while is more suited:
    Code:
    do
    {
        menu();
        if( scanf( " %d", &choice ) != 1 )
            break; /* bad data, handle it somehow */
    
        switch( choice )
        {
            case 1: one(); break;
            case 2: two(); break;
            case 3: three(); break;
            case 4: printf( "Goodbye.\n" ); break;
            default: /* invalid, do something */ break;
        }
    } while( choice != 4 );

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program to create shapes in PPM
    By cpsestudent in forum C Programming
    Replies: 3
    Last Post: 02-08-2011, 02:11 AM
  2. Area Perimeter
    By rossipoo in forum C Programming
    Replies: 5
    Last Post: 11-09-2010, 09:20 PM
  3. Program for average, geometric mean, harmonic mean
    By Northstar in forum C Programming
    Replies: 5
    Last Post: 11-07-2007, 11:33 AM
  4. Universal area forumula for equilateral + equiangular shapes?
    By Bird Killer in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 03-05-2006, 02:55 PM
  5. Displaying more shapes in the same client Area
    By sajeev_js in forum Windows Programming
    Replies: 1
    Last Post: 06-11-2005, 01:52 PM

Tags for this Thread