Thread: Ellipse problem

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    5

    Ellipse problem

    Hey,
    I need help with my program. I'm really lost and don't know where to start.
    Hopefully someone can help me.

    1. Use #define to set the value for PI=3.14159265359
    2. Consider an ellipse in the x, y-plane. Let a be the semi-major axis
    and b be semi-minor axis of the ellipse. The center of the ellipse
    is located at (x0, y0). If the major axis coincides with the x-axis,
    the polar representation of the ellipse is
    x - x0=a cos(theta) and y-y0=b sin(theta) (1)
    However, if the major axis of the ellipse is rotated by theta0 from
    the x- axis in the counterclockwise direction, the x, y-coordinates
    become
    x = x0 + a cos(theta)cos(theta0)-b sin(theta)sin(theta0) (2)
    y = y0 + a cos(theta)sin(theta0)+b sin(theta)cos(theta0) (3)

    Create the void function:

    void ellipse(double a, double b, double x0, double y0,
    double theta0);

    The input arguments are the semi-major axis a, the minor axis b,
    the origin of the ellipse (x0, y0), and the tilt angle theta0
    (in degrees) of the major axis from the x-axis.
    In ellipse() compute and print (x, y) from theta=0 to 360 degrees
    with the increment of 10 degrees. To print use %4d, %12.3e, and
    %12.3e for theta, x, and y, respectively. Your output in ellipse()
    should look like as follows:

    -------------------------------
    theta x-coord y-coord
    -------------------------------
    0 3.732e+00 2.000e+00
    10 3.619e+00 2.135e+00
    ... ......... .........
    360 3.732e+00 2.000e+00

    3. In prob1(), call ellipse() for a=2.0, b=1.0, x0=2.0, y0=1.0,
    theta0=30.0.
    4. Copy "in_ellipse.dat" from the public directory to your directory
    5. In prob2() define the file stream *infile to read "in_ellipse.dat"
    6. In prob2() read "in_ellipse.dat" using fgets(). Print each line as
    it is read.
    7. Finally, print the total number of lines in the file.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    This is what i have so far
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <strings.h>
    #include <math.h>
    //use #define for PI
    
    //Function prototypes
    void prob1(void);
    void prob2(void);
    void ellipse(double a, double b, double x0, double y0, double theta0);
    int main(void)
    {
            int menu;
            printf("\nThere are two parts.\n");
            printf("Enter the part number to execute (1, or 2): ");
            scanf("%d", &menu);
            // form a switch to execute one part
            switch(menu){
                case 1:
    prob1();
                    break;
                case 2:
                    prob2();
                    break;
                default:
                    printf("part %d does not exist.\n", menu);
            }
            exit(0);
    }
    void prob1(void)
    {
    //Declare a, b, x0, y0, and theta0 here
    int a, b, x0, y0, theta0;
    
    //Call ellipse here
    ellipse()
    
            return;
    }
    void ellipse(double a, double b, double x0, double y0, double theta0)
    {
    
    //Add your statements
    
            return;
    }
    void prob2(void)
    {
    //Add your statements
    
            return;
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > from theta=0 to 360 degrees with the increment of 10 degrees.
    Your tutor just told you what for loop to write

    > To print use %4d, %12.3e, and %12.3e for theta, x, and y, respectively.
    Those are the formats you pass to printf()

    The formulae for x and y are in the text - read it again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    does the for loop goes inside
    void ellipse(double a, double b, double x0, double y0, double theta0)??

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    What do you think?

    Or more to the point, what does your assignment say you should do. It plainly says where it should go from what I'm reading.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    This is my program so far i don't get the right values =/
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <strings.h>
    #include <math.h>
    //use #define for PI
    #define PI 3.14159265359
    
    //Function prototypes
    void prob1(void);
    void prob2(void);
    void ellipse(double a, double b, double x0, double y0, double theta0);
    int main(void)
    {
            int menu;
            printf("\nThere are two parts.\n");
            printf("Enter the part number to execute (1, or 2): ");
            scanf("%d", &menu);
            // form a switch to execute one part
            switch(menu){
                case 1:
                    prob1();
                    break;
                case 2:
                    prob2();
                    break;
                default:
                    printf("part %d does not exist.\n", menu);
            }
            exit(0);
    }
    void prob1(void)
    {
    //Declare a, b, x0, y0, and theta0 here
            double a=2.0, b=1.0, x0=2.0, y0=1.0, theta0=30.0;
    //Call ellipse here
            ellipse( a, b, x0, y0, theta0);
    
            return;
    }
    void ellipse(double a, double b, double x0, double y0, double theta0)
    {
    //Add your statement
    
            int n, degree;
            double x, y, theta;
    
            printf("#-------------------------------\n");
            printf("#theta   x-coord      y-coord\n");
            printf("#-------------------------------\n");
    
            for (n=0; n<=36; n++){
            degree = n/.1;
            theta = degree*(180/PI);
    
            if(theta==0){
                    x=x0+a*cos(theta);
                    y=y0+b*sin(theta);
                    printf("%4d %12.3e %12.3\n",degree, x, y);
            }else{
            x = x0 + a*cos(theta)*cos(theta0)-b*sin(theta)*sin(theta0);
            y = y0 + a*cos(theta)*sin(theta0)+b*sin(theta)*cos(theta0);
            printf(" %4d %12.3e %12.3e\n", degree, x, y);
            }}
            return;
    }
    void prob2(void)
    {
    //Add your statements
            FILE *infile;
            char text [75];
            int count =0;
            printf("\n");
            infile = fopen("in_ellipse.dat", "r");
            if (infile==NULL){
            printf("The input file does not exist.\n");
            exit(0);
            }else
            while(fgets(text,75,infile)!=NULL)
            {
            count++;
            printf("%s",text);
            }
            printf("\n There are %d lines.\n",count);
            fclose(infile);
             return;
    }

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    my problem is that theta is being read as radians instead of degrees.. can anyone help me

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    3

    your loop

    Did you figure it out? Im curious about that degree change too.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    I am getting super big numbers too. Did you get it?

  10. #10
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    I tried it, I got big numbers also and it almost looks as if the loop isnt running properly.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    damn, i still cant get it

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So, what exactly do you have problems with?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Nov 2010
    Posts
    2
    it should be
    theta = degree* (PI/180.0)

  14. #14
    Registered User
    Join Date
    Nov 2010
    Posts
    2
    you also have to do
    theta0 = 30.0*(PI/180.0)

  15. #15
    Registered User
    Join Date
    Nov 2010
    Posts
    3

    .

    The question was how to convert radians to degrees in order to run it in the for loop so that the intervals would be increasing at 10 degrees each time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM