1. ## Maths equation

Hi,

I have a mathematical equation i was hoping to get some help with to get it working in C. I'ts a small part of a program and if i figured it out it , i think i could figure out the rest of it! The equation is a second order derivative

z[n] = x[n-1] - 2 x[n] + x[n+1]

so for an array of 8 numbers say {2,5,7,4,3,5,9,8}
it picks the number before the current no. - twice current no. + next number.
For 5 in the array it would be z[n] = (2 - 10 + 7) = -1

Here is my very poor attempt so far
Code:
```#define MAX 8

int z[MAX];
int a[MAX] = {2,5,7,4,3,5,9,8};

int main(void)
{
for (int i=0; i<8; i++)
z[i] = a[i-1] - (2(a[i]) + a[i+1];
}```
I was hopingto store the ouput values into an array z. I know there is going to be a problem with the first value because n-1 = 0
Any help would be greatly appreciated 2. (2(a[i])
C doesn't understand implicit multiplication like in math. Make it 2*a[i].

For your problem with the array bounds you could increment your array size by 1 and let the first element be a 0. 3. thanks, the following error came up "for loop initial declaration used outside C99 mode" ??? 4. i don't think you can declare your loop variables inside the loop, try doing it this way

Code:
```int i = 0;
for(i; i<8; i++)``` 5. Excellent thanks, that fixed that problem and it compiles now anyway, i'm not sure what calculation it's doing exactly though but its not what i expected!

Code:
```#include <stdlib.h>
#include <stdio.h>
#define MAX 8

int a[MAX] = {14,10,5,0,65531,65524,65517,65511};
int i;

int z[MAX];

/*      second order derivative      */
int main(void)
{

int i = 0; for(i; i<8; i++)             // z[n] = x[n-1] - 2 x[n] + x[n+1]
z[i] = a[i-1] - (2*a[i]) + a[i+1];
printf("z[%d] = %d\n",i,z[i]);

int ch;
printf ("Press [Enter] to continue");
while ((ch = getchar()) != '\n' && ch != EOF);
return 0;
}```
This returns z = 4007044, i was hoping to fill the array Z with correct answers to the formula and display them all, any ideas? 6. An array of size 8 may be indexed from 0 to 7. So take extra care with such things as a[i-1] and a[i+1] (for example, plug in the starting and ending values of i and check which index this would be, and whether it is within the allowable range). 7. as suggested ealier i plugged in 0s at the start and end of the array
Code:
```#define MAX 10

int a[MAX] = {0,14,10,5,0,65531,65524,65517,65511,0};```
and changed the for loop to
Code:
`  int i = 1; for(i; i<9; i++)`
so the array shouldnt go out of bounds for testing however ultimately i want to be able to pull in an array of 8 numbers and use this as a functon so i'm not sure how i'll be able to add the zeros at the start and end automatically and return 8 values to the array z?

Now the output just displays z = 0 , i'm stuck  8. Ah I get it! It would be important to make this loop a compound statement. Try:
Code:
```  int i = 1;
for(i; i < 9; i++)
{
z[i] = a[i-1] - (2*a[i]) + a[i+1];
printf("z[%d] = %d\n",i,z[i]);
}``` 9. Excellent thanks a million, it performs the calculation perfectly I've just one more small problem though if anyone could help. Would i be able to modify the it so it could read in an array of 8 numbers as a function, add the zeros at either end so the calculations is performed okay and the array doesnt go out of bounds, and return the values into the z array from z to z (at the moment the values are perfect but it goes from z to z)

Code:
```#include <stdlib.h>
#include <stdio.h>
#define MAX 10

int a[MAX] = {0,2,5,7,4,3,5,9,8,0};
int i;

int z[MAX];

/*      second order derivative      */
int main(void)
{

int i = 1;
for(i; i < 9; i++)
{
z[i] = a[i-1] - (2*a[i]) + a[i+1];
printf("z[%d] = %d\n",i,z[i]);
}

int ch;
printf ("Press [Enter] to continue");
while ((ch = getchar()) != '\n' && ch != EOF);
return 0;
}``` 10. Originally Posted by supermeew
as suggested ealier i plugged in 0s at the start and end of the array
I don't think that was the suggestion. My suggestion was to see what happens to i-1 and i+1 when i=0 and i=7, and whether you get indices of, say, -1 or 8 when you can only go from 0 to 7. But changing the loop to go from 1 to MAX-1 was the way to go. Note that the calculation cannot be done at the endpoints of the array. And by the way, if you are using MAX, don't later use a hard-coded 8. Frankly I don't care to use #defines this way.

As far as defining another function, I might suggest starting with something like this.
Code:
```#include <stdio.h>

void foo(const int *a, size_t size, int *z)
{
size_t i;
puts("foo");
for ( i = 1; i < size - 1; ++i )
{
z[i] = a[i-1] - (2 * a[i]) + a[i+1];
printf("z[%d] = %d\n", (int)i, z[i]);
}
}

int main(void)
{
int a[] = {-5,-1,23,115,347,815,1639,2963};
int b[] = {0,2,5,7,4,3,5,9,8,0};
int z[sizeof b / sizeof *b]; /* same size as the bigger b array */
foo(a, sizeof a / sizeof *a, z);
foo(b, sizeof b / sizeof *b, z);
return 0;
}

/* my output
foo
z = 20
z = 68
z = 140
z = 236
z = 356
z = 500
foo
z = 1
z = -1
z = -5
z = 2
z = 3
z = 2
z = -5
z = -7
*/``` 11. Cheers, thanks for yor help Popular pages Recent additions 