# Thread: Preforming operations on an array

1. ## Preforming operations on an array

For my assignment I have to have an array with only zeros.

Code:
`int a[20] = { 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0 };`
Then I need to send it into a function that makes the array like this

Code:
`int a[20] = {0,1,2,3,4,5,6, ... , 19}`
Which I have done here

Code:
```int initialize(int a[], int n)

{
int m = 0;
int i;

printf("\n");
printf("Now the array is \n");

for (i=0; i<20; i++ )
{

a[i] = a[i] + m;

m = m + 1 ;

printf ("%3d", a[i]);

}

}```
Now I need to do the following with the array.

I need to take whatever value is in each position and add that value to all of the previous values.

like this.

Code:
```a[3] = a[3] + a[2] + a[1] + a[0]

only for every a[i]```
I know that I can code this the long way, but I just can't see to be able to find out how to do this a better way.

Thanks.

by the way its my first post.

2. For the first one you could have just done:
Code:
```   for (i=0; i<20; i++ )
a[i] = i;```
For the next one, you'll need to use a nested loop (a loop within a loop).

Popular pages Recent additions