Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C by d ( 11 years ago )
#include <stdio.h>
#include <math.h>

typedef enum { Polar, Cartesian } coordinatetype ;

typedef struct {
  coordinatetype tag ;
  union {
    struct {
      double x ;
      double y ;
    } cartesian ;
    struct {
      double r ;
      double theta ;
    } polar ;
  } contents ;
} coordinate ;

coordinate convertCoordinates(coordinate cart);
int main(void)
{
  coordinate cart, polar;

  printf("enter a value for x:");
  scanf("%lf", &cart;.contents.cartesian.x);

  printf("enter a value for y:");
  scanf("%lf", &cart;.contents.cartesian.y);

  polar = convertCoordinates(cart);

  printf("the polar coordinates are r = %lf and theta = %lf\n", polar.contents.polar.r, polar.contents.polar.theta);

  return 0;

}

coordinate convertCoordinates(coordinate cart)
{
  coordinate polar;

  polar.contents.polar.r = sqrt(pow(cart.contents.cartesian.x, 2) + pow(cart.contents.cartesian.y, 2));
  polar.contents.polar.theta = atan(cart.contents.cartesian.y/cart.contents.cartesian.x);

  return polar;
}

 

Revise this Paste

Your Name: Code Language: