Basic arrays, not sure if i'm right

  • Thread starter Thread starter unscientific
  • Start date Start date
  • Tags Tags
    Arrays
Click For Summary

Discussion Overview

The discussion revolves around a homework problem involving the construction of two arrays in C, where one array is meant to store sine values and the other cosine values. Participants are addressing issues related to the implementation of for loops, the correctness of the mathematical functions used, and the calculation of dot products to verify orthogonality.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents an initial code attempt for creating two arrays, x and y, where x is intended to store sine values and y cosine values.
  • Another participant points out that the code incorrectly assigns sine values to both arrays and suggests that y should store cosine values instead.
  • There are discussions about the use of parentheses in mathematical expressions to ensure that values are treated as doubles.
  • Some participants clarify the need for braces in for loops to avoid potential errors in code execution.
  • Concerns are raised about the calculation of dot products, with suggestions to accumulate sums rather than overwrite them in each iteration.
  • Participants discuss the importance of moving print statements outside of loops to avoid excessive output.

Areas of Agreement / Disagreement

Participants generally agree that y should store cosine values, but there is no consensus on the correctness of the initial code provided. Multiple competing views on coding practices and mathematical correctness remain unresolved.

Contextual Notes

Some participants express uncertainty about the correct implementation of the for loop and the proper accumulation of sums for dot products. There are also unresolved issues regarding the handling of array indices and the correct mathematical functions to use.

unscientific
Messages
1,728
Reaction score
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
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 π?
 
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:
Problem statement in OP clearly asks that y store the cos () values.
 
SteamKing said:
Problem statement in OP clearly asks that y store the cos () values.
Now fixed in my code snippet.
 
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);
}
 
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:
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]
...
 
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.[/color] 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.[/color] 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
 

Similar threads

  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 21 ·
Replies
21
Views
4K