# Thread: first number in array incorrect...

1. ## first number in array incorrect...

Can anyone tell me if there is anything wrong with this notation?
I have array[200];
then this

Code:
```for(i=0; i<100; i++){
delta_x[i]=array[i];
}
for(i=100; i<200; i++){
delta_y[i-100]=array[i];
}```
In my program everything is okay except that delta_y[0] is not right - it's not in array[200] at all, so it messes everything up by that amount.
This seems right to me, but I thought maybe there was a problem, or does anyone know why this could happen?

2. That assignment statement sets delta_y[0] to array[100], so everything is okay there. Are you sure that the error is here and not some later statement?

3. I'm not sure of anything, actually, this is really puzzling me. I have the same notation for delta_x[100] as I do for delta_y[100] and delta_x is fine all the way.
I printed out array[200] to make sure the mistake's not there, and it's not. I also printed out delta_y[100] by itself, and the first number is wrong.
I don't think there's anything going on between array and delta_y besides the loop I already posted...

4. Rather than having two loops, that essentially do the same thing, try this:

Code:
```for(i=0; i<100; i++){
delta_x[i] = array[i];
delta_y[i] = array[i+100];
}```
QuantumPete

5. lol good call. My whole program's kinda like that. I did it in pieces when I was tired... :P But I just condensed my other loops, too. Unfortunately, it didn't change anything. Still same problem...

6. How did you declare delta_x and delta_y?
Can you post the smallest program that reproduces this problem?
QuantumPete

7. They're declared as double delta_x[100], delta_y[100].
I'm messing with the first part of the program now. Once I get it simplified I'll post some more.

8. All right, I think the problem might actually be here

Code:
```     for (i=0; i<100; i++){
x[i+1]=x[i]+delta_x[i];
y[i+1]=y[i]+delta_y[i];
A[i]=bluemoon(x[i], y[i]);
}```
When I comment this out and print delta_y[100] by itself, the first number is correct.
But how does this change delta_y?
bluemoon is my function. :P

9. I think you are still better off showing the smallest and simplest compilable program that demonstrates the problem.

10. All right:

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

double bluemoon (double x, double y)
{
double function;
double expfunction;

function=-1*x*x;-(1/2)*y*y;
expfunction=exp(function);

return expfunction;
}

main ()
{
double array[200];

int i;
double asta;

for (i = 0; i < 200; i++){
asta=i;
array[i]=asta;
}

double delta_x[100], delta_y[100];
double x[100], y[100], A[100];

for(i=0; i<100; i++){
delta_x[i] = array[i];
delta_y[i] = array[i+100];
}

for (i=0; i<100; i++){
x[i+1]=x[i]+delta_x[i];
y[i+1]=y[i]+delta_y[i];
A[i]=bluemoon(x[i], y[i]);
}

for(i=0; i<100; i++){
printf("&#37;e\n", delta_y[i]);
}

getchar();
return 0;
}```
Same problem.
But if I take out the
Code:
```     for (i=0; i<100; i++){
x[i+1]=x[i]+delta_x[i];
y[i+1]=y[i]+delta_y[i];
A[i]=bluemoon(x[i], y[i]);
}```
section it's fine.

EDIT: It also doesn't matter what bluemoon is defined as, still happens. Unfortunately I need that section... lol

11. According to your code, delta_y[100] does not exist, since delta_y has 100 elements. Consequently, printing delta_y[100] by itself is accessing the array out of bounds.

12. Sorry, I mean printing delta_y[i] from i=0 to i<100.

13. I did a quick test (you really need to properly indent your code), and it seems that the problem is due to the array out of bounds when you assign to x[100] and y[100], both of which do not exist. Note that you assign to x[i+1] and y[i+1], but loop from 0 to 99 inclusive.

14. @#\$%!! I can't believe I didn't notice that! I'd been looking for that for so long, that was all I thought it could be...
I guess I'd stared at it so long I'd convinced myself it was fine.

Oh, well...

Thanks so much for your help. My larger program is working now.