Basic arrays, not sure if i'm right

  • Thread starter unscientific
  • Start date
  • Tags
    Arrays
In summary, the conversation discusses a coding problem where the code needs to be completed before the person has access to Xcode on their Mac. The question involves creating two arrays using a for loop and calculating values for sine and cosine. The solution attempted involved using the sin() function, but was later corrected to use the cos() function for the y array. The conversation also includes a discussion on calculating scalar products and the importance of using braces in for loops. The final code snippet provided includes the necessary corrections for both parts of the problem.
  • #1
unscientific
1,734
13

Homework Statement



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.

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;



}
 
Physics news on Phys.org
  • #2
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
unscientific said:

Homework Statement



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.

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;



}


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:
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:
  • #4
Problem statement in OP clearly asks that y store the cos () values.
 
  • #5
SteamKing said:
Problem statement in OP clearly asks that y store the cos () values.
Now fixed in my code snippet.
 
  • #6
Mark44 said:
Now fixed in my code snippet.

Mark, many thanks for the reply! So the code should be something like this:

Part (a)

Code:
#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:
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
unscientific said:
Mark, many thanks for the reply! So the code should be something like this:

Part (a)

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

}
The above won't work. The code below will behave exactly the same as what you wrote.
Code:
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.
unscientific said:
(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)
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


unscientific said:
Code:
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);
}
You should move the printf statement outside the loop so that it prints once, not 100 times.
 
Last edited:
  • #8
Mark44 said:
The above won't work. The code below will behave exactly the same as what you wrote.
Code:
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.

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:
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
unscientific said:
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:
for ( i = 0; i <100; i++)

{

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

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

}
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.
unscientific said:
/****/ 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
...
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.
unscientific said:
Oh btw, do i put the printf" function inside the loop or outside?
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."


unscientific said:
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]
...
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
Mark44 said:
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;


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

1. What is an array?

An array is a data structure that allows us to store multiple values of the same data type in a single variable. It is a collection of elements that are indexed and can be accessed individually.

2. How do you declare an array?

To declare an array, we use square brackets [ ] after the variable name, followed by an equal sign (=) and a list of values inside curly braces { }. For example: int[] myArray = {1, 2, 3, 4, 5};

3. How do you access elements in an array?

Elements in an array can be accessed using their index, which starts from 0. For example, myArray[0] will give us the first element in the array, which is 1. We can also use a loop to iterate through the array and access each element individually.

4. Can an array hold different data types?

No, an array can only hold elements of the same data type. For example, an array declared as int[] can only hold integer values.

5. How do you find the length of an array?

To find the length of an array, we use the .length property. For example, myArray.length will give us the length of the array, which is 5 in the previous example.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
713
  • Engineering and Comp Sci Homework Help
Replies
3
Views
634
  • Engineering and Comp Sci Homework Help
Replies
4
Views
879
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
836
Back
Top