# Thread: Need help with for loops and arrays

1. ## Need help with for loops and arrays

Hello, I'm currently a student with a beginner programming skill level and have trouble with programming problem.

The problem is:

Let X and Y be two arrays of size 10. Let Z be an array of size 19. The values of arrays X and Y are available in the
ﬁle "input-xy.txt", where every line contains three values: (1) index of the array, i -- an integer; (2) value of element
in array x, x[i] -- a double value, and (3) value of element i in array y, y[i] -- a double value. The value of x and y ele-
ments for undeﬁned indices are assumed to be zero.

Element j of array z, z[j] is computed as below: j = 0, 1, 2, . . . , 18.

for the summation:
http://i36.tinypic.com/1roldu.jpg

Now what I currently have is

Code:
```#include <stdio.h>

FILE *inp;

main()
{
int i, k, j, x[10], y[10], z[19];
int sum = 0

inp = fopen("input-xy.txt", "r");

fscanf(inp, "%d ", &b);
fscanf(inp, " %d %d ", &x[b], &y[b]);
printf(" %d %d %d ", b, &x[b], &y[b]);

for ( j = 0; j <= 9; j++)
{
k = 0;
z[j] = x[k] * y[j-k];

for ( j = 11; j <=19; i++)
{```
What I want to know does everything look alright? I'm making my k max equal 0 for 0<j<9 so everything would be simplified. That would be all my values for x are less than or equal to 9.

But also can I do this:
z[j] = x[k] * y[j-k] in the for loop? Can I write a function with the array bracket?

2. b is not a declared variable, so no. It can't fly like that.

b is not a declared variable, so no. It can't fly like that.
Opps I mean't for that too to be j.

4. Assuming you manage to fix the rest of the b's, and add a semicolon where necessary, then the reading in is good. For the summation, you will have to sum things together.

5. OK, once you get the j in there. Then there's this bit:

Code:
```
for ( j = 0; j <= 9; j++)
{
k = 0;
z[j] = x[k] * y[j-k];

for ( j = 11; j <=19; i++)
{```
Which is indented to look like the second for loop is outside the first, and it's not, clearly. You either need another counter variable for the second for loop, or you need to close the first for loop, before starting the second one.

I'm not sure what you're asking here:
z[j] = x[k] * y[j-k] in the for loop? Can I write a function with the array bracket?
That code is already inside your first for loop, and "writing a function with the array bracket" leaves me adrift.

OK, once you get the j in there. Then there's this bit:

Code:
```
for ( j = 0; j <= 9; j++)
{
k = 0;
z[j] = x[k] * y[j-k];

for ( j = 11; j <=19; i++)
{```
Which is indented to look like the second for loop is outside the first, and it's not, clearly. You either need another counter variable for the second for loop, or you need to close the first for loop, before starting the second one.

I'm not sure what you're asking here:

That code is already inside your first for loop, and "writing a function with the array bracket" leaves me adrift.
Yeah it seems I forgot to close the first loop, what I'm asking for the second part is could I put something like y[j-k], the j-k function within the bracket for it to identify itself with a certain index.

7. Absolutely yes.