1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Basic arrays, not sure if i'm right

  1. Jan 8, 2013 #1
    1. The problem statement, all variables and given/known data

    First of all, i wrote this code but i can't seem to get xcode to work on my mac and i'll only get to school in 2 days time, while this needs to be completed before school! I'm using xcode on Mac, programming language C.

    Here's the question:

    (a)Using a for loop, construct two 100 element arrays, x and y, such that element i of x stores the value sin(2∏i/100)) and the corresponding element of y stores cos((2∏i/100)). Print the values stored in the elements of x and y as you calculate them.

    (b)Compute the scalar (i.e. dot) products x.x, y.y, and x.y, to check that sin and cos are orthogonal.

    3. The attempt at a solution

    (a)

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>

    int main()

    {

    double x[100];
    double y[100];
    int i;

    for ( i = 0; i <100; i++)

    x = sin (2*M_PI*i/100);

    y = sin (2*M_PI*i/100);

    printf("%.3f \t %.3f \n ", x, y);

    return 0;



    }


    (b)

    /****/ I only did the first x.x part here, cause the others are pretty straightforward once I get this right..


    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>

    int main()

    {

    double x[100];
    double y[100];
    double sumx;
    double sumy;
    int i;

    for ( i = 0; i <100; i++)

    x = sin (2*M_PI*i/100);
    sumx = x*x;

    y = sin (2*M_PI*i/100);
    sumy = y*y;

    printf("%.3f \t %.3f \n ", sumx, sumy);

    return 0;



    }
     
  2. jcsd
  3. Jan 8, 2013 #2

    rcgldr

    User Avatar
    Homework Helper

    Shouldn't one of those (probably the x) be cos(...)? Also, you may want to change the expressions to that all the values used in the computation are doubles (the extra set of parenthesis on i aren't needed, but make the code easier to follow):

    x = cos(2.*M_PI*((double)i)/100.);
    y = sin(2.*M_PI*((double)i)/100.);

    I assume that M_PI is a predefined double for the value of π?
     
  4. Jan 8, 2013 #3

    Mark44

    Staff: Mentor



    For the b) part, your code isn't correct. To calculate x##\cdot##y, you want to calculate this sum: x0 * x0 + x1 * x1 + ... + x99 * x99, so your loop should look like this:

    Code (Text):

    sumx = 0.0;
    sumy = 0.0;
    for ( i = 0; i <100; i++)
    {
        x[i] = sin (2.0 * M_PI * i/100.0);
        sumx += x[i] * x[i];
       
        y[i] = cos (2.0 * M_PI * i/100.0);
        sumy += y[i] * y[i];
    }
    Note that I used braces for the body of the for loop. It is dangerous to not include braces - if you change the loop body in your code, it's easy to think that an additional statement will also be part of the loop body, but it isn't. Many beginning programmers have gotten bit by this.

    BTW, when you post code, put a [ code ] tag at the top and a [ /code ] tag (without extra spaces) at the bottom.
     
    Last edited: Jan 8, 2013
  5. Jan 8, 2013 #4

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    Problem statement in OP clearly asks that y store the cos () values.
     
  6. Jan 8, 2013 #5

    Mark44

    Staff: Mentor

    Now fixed in my code snippet.
     
  7. Jan 8, 2013 #6
    Mark, many thanks for the reply! So the code should be something like this:

    Part (a)

    Code (Text):


    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>

    int main()

    {

    double x[100];
    double y[100];
    int i;

    for ( i = 0; i <100; i++)

    x[i] = sin (2*M_PI*i/100);

    y[i] = sin (2*M_PI*i/100);

    printf("%.3f \t %.3f \n ", x[i], y[i]);

    return 0;

    }

     
    (b)i For x.x and y.y and x.y

    (Why in the for loop do you use +=? when you can simply use = since sumx simply becomes x*x in each step then the next step x becomes x[i+1]...the increment of i is taken care of at the end of each loop)

    Code (Text):



    double x[100];
    double y[100];
    int i;

    sumx = 0.0;
    sumy = 0.0;
    sumxy= 0.0;


    for ( i = 0; i <100; i++)

    {
        x[i] = sin (2.0 * M_PI * i/100.0);
        sumx = x[i] * x[i];
       
        y[i] = cos (2.0 * M_PI * i/100.0);
        sumy = y[i] * y[i];

        sumxy = sumx * sumy;

        printf("%.3f \t %.3f \t %.3f\n", sumx, sumy, sumxy);
    }

     
     
  8. Jan 9, 2013 #7

    Mark44

    Staff: Mentor

    The above won't work. The code below will behave exactly the same as what you wrote.
    Code (Text):

    for ( i = 0; i <100; i++)
    {
       x[i] = sin (2*M_PI*i/100);
    }
    y[i] = sin (2*M_PI*i/100);
    printf("%.3f \t %.3f \n ", x[i], y[i]);

     
    Do you see why? And do you understand why it's wrong?

    Also, the line with y should use cos, not sin.
    I used += instead of just = because I didn't want to overwrite the value stored in the previous loop iteration - I wanted to increment what had been stored.

    sumx += x * x;
    is effectively the same as
    sumx = sumx + x * x;


    Think about how you might add together four numbers, 3, 6, 8 and 11.

    If I have a variable called sum, I have to start it off at 0.

    sum = 0
    Now add the first number:
    sum = sum + 3 // sum is 3
    Now add the second number:
    sum = sum + 6 // sum is now 9
    Now add the third number:
    sum = sum + 8 // sum is now 17
    Now add the fourth number:
    sum = sum + 11 // sum is now 28


    You should move the printf statement outside the loop so that it prints once, not 100 times.
     
    Last edited: Jan 9, 2013
  9. Jan 9, 2013 #8




    I'm still not sure why part (a) is wrong...

    for part (a) they are simply asking me to print the following values in array form:

    x[0] = sin (0)
    x[1] = sin (2∏*1/100)
    x[2] = sin (2∏*2/100)
    x[3] = sin (2∏*3/100)
    ...


    y[0] = cos (0)
    y[1] = cos (2∏*1/100)
    y[2] = cos (2∏*2/100)
    y[3] = cos (2∏*3/100)
    ...

    which is simply

    Code (Text):


    for ( i = 0; i <100; i++)

    {

    x[i] = sin (2*M_PI*i/100);

    y[i] = cos (2*M_PI*i/100);

     
    }

    /****/ which is the same as this:

    Step 0
    x[i0] = sin (2*∏*i0/100)
    y0] = cos (2*∏*i0/100)

    Next step, step 1 /****/ i0 now becomes i1
    x[1] = sin (2*∏*i1/100)
    y[1] = cos (2*∏*i1/100)

    Next step, step 2....i1 now becomes i2
    ....


    Oh btw, do i put the printf" function inside the loop or outside?

    for part (b), they are not asking for the sum, they are simply asking me to print out each value of x.x which is

    x[0] * x[0] = 0
    x[1]* x[1]
    x[2] * x[2]
    ....

    x[0] * y[0]
    x[1]* y[1]
    x[2] * y[2]
    ....
     
  10. Jan 9, 2013 #9

    Mark44

    Staff: Mentor

    This is fine, except that you should have a printf statement inside the body of the loop to print out the values of x and y right after you calculate them.

    For formatting, you should indent the statements that make of the body of the for loop. This makes it easier for human readers to understand your program logic.
    There really is no i0 and i1, etc. This would imply that i is an array variable, which it isn't. In the first step, i is 0. In the next step, i is 1, and so on.
    For the a) part, printf needs to be inside the loop body. From your problem statement, "Print the values stored in the elements of x and y as you calculate them."


    You are mistaken. They are NOT asking you to print out "each value" of x.x, y.y, and x.y. x and y are vectors, so I will write them in bold.

    x = <x0, x1, x2, ..., x99>
    y = <y0, y1, y2, ..., y99>

    x##\cdot## x = x0*x0 + x1*x1 + x2*x2 + ... + x99*x99

    y##\cdot## y is calculated very similarly.

    For x##\cdot## y, each product has an xi factor and a yi factor.

    I explained this before. To calculate the three sums (the three dot products) you need to do the accumulation thing I talked about: i.e., sumx += x * x; or sumx = sumx + x * x;
     
  11. Jan 11, 2013 #10


    Ah, this clears things up, thanks! I'll rewrite the code again
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Basic arrays, not sure if i'm right
  1. Confused with array (Replies: 3)

  2. Array C++ (Replies: 2)

  3. C - array (Replies: 2)

  4. Function and array (Replies: 26)

Loading...