# Basic arrays, not sure if i'm right

1. Jan 8, 2013

### unscientific

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. Jan 8, 2013

### rcgldr

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 π?

3. Jan 8, 2013

### 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
4. Jan 8, 2013

### SteamKing

Staff Emeritus
Problem statement in OP clearly asks that y store the cos () values.

5. Jan 8, 2013

### Staff: Mentor

Now fixed in my code snippet.

6. Jan 8, 2013

### unscientific

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);
}

7. Jan 9, 2013

### 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
sum = sum + 3 // sum is 3
sum = sum + 6 // sum is now 9
sum = sum + 8 // sum is now 17
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
8. Jan 9, 2013

### unscientific

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]
....

9. Jan 9, 2013

### 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;

10. Jan 11, 2013

### unscientific

Ah, this clears things up, thanks! I'll rewrite the code again