help me

This is a discussion on help me within the C Programming forums, part of the General Programming Boards category; Dear all, I have a set of data, say x(n)={1,2,3,4} if I want to calculate Code: actually, this is not ...

1. help me

Dear all,
I have a set of data, say x(n)={1,2,3,4} if I want to calculate
Code:
```actually, this is not code.(but if i not type like this, the article cannot post,sorry)
Y[0]=x(0)*x(0)+x(1)*x(1)+x(2)*x(2)+x(3)*x(3)
Y[1]=x(0)*x(1)+x(1)*x(2)+x(2)*x(3)
Y[2]=x(0)*x(2)+x(1)*x(3)
Y[3]=x(0)*x(3)
(every time, the set of data times itself by shift to left by 1,at the begining, it times itself without shifting)```
How can I program it?
Thank you.

2. You lost me. Work the formulae out in words on paper (or a text editor, whatever), and then convert it to code. You'll likely need two loops to do this. For loops usually work fine for this sort of thing. I *kind of* get the idea of what you're doing, but you don't seem to have a real pattern in what you've detailed here.

Some things are multiplied by themselves, some times they aren't. Not very clear.

Quzah.

3. Sorry, not present clearly here, oh, maybe i should say sorry for my poor english.

Actually, this is what autocorrelation does in Digital signal processing.
[question]
A set of data, [X(0),x(1),x(2),x(3)]=[1,2,3,4]
at 0, Y[0]=x(0)*x(0)+x(1)*x(1)+x(2)*x(2)+x(3)*(x(3)
at 1, shift left by 1, that is
....................x(0) x(1) X(2) x(3)
.............x(0) x(1) x(2) x(3)

Y[1]=x(0)*x(1)+x(1)*x(2)+x(2)*x(3)

so on....
until
.....................x(0) x(1) X(2) x(3)
x(0) x(1) X(2) x(3)
Y[3]=x(0)*x(3)
How to program it? I have get two for loop, but it only calculates Y[0] correctly. others return me random number.
[/question]
Is my presentation clearly?

4. Let's try something like this, with debugging printfs left in.
Code:
```/* calculation description
Y[0] = x(0) * x(0) + x(1) * x(1) + x(2) * x(2) + x(3) * x(3)
Y[1] = x(0) * x(1) + x(1) * x(2) + x(2) * x(3)
Y[2] = x(0) * x(2) + x(1) * x(3)
Y[3] = x(0) * x(3)
*/
#include <stdio.h>

#define ARRAYSIZE(x) (sizeof(x)/sizeof(*(x)))

int main(void)
{
int x[] = {1,2,3,4}, y [ ARRAYSIZE(x) ];
size_t i,j;
for(j = 0; j < ARRAYSIZE(y); ++j)
{
y[j] = 0;
printf("y[%d] =", (int)j);
for(i = j; i < ARRAYSIZE(x); ++i)
{
printf("%s x[%d] * x[%d]", i != j ? " +" : "", (int)(i - j), (int)i);
y[j] += x[i - j] * x[i];
}
printf(" = %d\n", y[j]);
}
return 0;
}

/* my output
y[0] = x[0] * x[0] + x[1] * x[1] + x[2] * x[2] + x[3] * x[3] = 30
y[1] = x[0] * x[1] + x[1] * x[2] + x[2] * x[3] = 20
y[2] = x[0] * x[2] + x[1] * x[3] = 11
y[3] = x[0] * x[3] = 4
*/```
Is this in the ballpark?

5. Thank you Dave_Sinkula.
I think this is my want. try it now