1. ## Wierd problem

The problem I'm having with my code is located at
Code:
```void fGR(double x[NSTEPS], double y[NSTEPS], double sig[NSTEPS],
double **GRalpha, double **GRbeta, int ma, double chi,
int nu, double Q, double GRa[GRma])

* * *

fout1 << "GRma:" << ma << " GR[]:" << GRa[0] << " " << GRa[1] << endl;

fout1 << "FF L50:  GRalpha[][0]\tGRalpha[][1]\tGRbeta[][0]" << endl;
for(i=1; i<=ma; i++)
{
fout1 << endl << "54GR:" << GRa[0] << " " << GRa[1] << endl;
fout1 << "[" << i << "][] \t ";
for(j=1; j<=ma; j++)
{
GRalpha[i][j] = 0.0;
fout1 << GRalpha[i][j] << "\t\t";
}
fout1 << endl << "61GR:" << GRa[0] << " " << GRa[1] << endl;
GRbeta[i][0] = 0.0;
fout1 << GRbeta[i][0] << endl;
}```
As you can see, there doesn't seem to be any trouble; I'm mostly just outputting some data to a file. The problem I'm having is (output; not code):

Code:
```GRma:2 GR[]:430.819 6.0904
FF L50:  GRalpha[][0]   GRalpha[][1]    GRbeta[][0]

54GR:430.819 6.0904
[1][]    0              0
61GR:430.819 6.0904
0

54GR:430.819 6.0904
[2][]    0              0
61GR:0 6.0904
0```
I input into the function GRa[0] = 430.819, GRa[1] = 6.0904. After the second iteration of the outer for loop, it suddenly changes GRa[0] to 0 for no apparent reason. Why would a program suddenly do this?

2. This isn't C, it's C++.

Run it through a debugger and you shall see the answer . It'd also help if you cleaned the code up a bit, including indenting, avoid writing 'endl' until the end of lines (not at the start).

3. If you just look at the output "fout <<", yeah, it would be C++, but that wasn't the point since I'm mostly writing C with C++ convenience.

I can see that the could be confusing, but this isn't the entire code. Basically it comes down to my final question: why would a for-loop suddenly change the value through one iteration when I'm not even adjusting that term in the function?

Here is a cleaner version of that portion (lines labeled where I send output):

Code:
```void fGR(double x[NSTEPS], double y[NSTEPS], double sig[NSTEPS],
double **GRalpha, double **GRbeta, int ma, double chi,
int nu, double Q, double GRa[GRma])
{
* * *

(50)

for(i=1; i<=ma; i++)
{
(54)
fout1 << "[" << i << "][] \t ";
for(j=1; j<=ma; j++)
{
GRalpha[i][j] = 0.0;
fout1 << GRalpha[i][j] << "\t\t";
}
(61)
GRbeta[i][0] = 0.0;
fout1 << GRbeta[i][0] << endl;
}

* * *
}```

4. Remove the file output stream and use printf instead, then check to see if does the same thing...

5. I'd guess that you are going beyond the size of your GRAlpha array, and it's overwriting other variables. Try changing your 0.0 to -4711.3 and see if the value that gets overwritten is changing along with it - I'm 99% sure it will. If that is what happens, then we need to see the declaration (and allocation if there is one) of the GRAlpha array.

I'm particularly thinking that this line:
Code:
`      for(j=1; j<=ma; j++)`
goes one iteration further than you think.

--
Mats

6. Yeah, it does (output, not code):

Code:
```GRma:2 GR[]:430.819400 6.090400

54GR:430.819400 6.090400

61GR:430.819400 6.090400

54GR:430.819400 6.090400

61GR:0.000000 6.090400```
I only replaced the lines for that particular problem (separately replacing all of those in the loop). Additionally, I turned it into a dynamically allocated pointer (changed to double *GRa). It works up to this point as well.

7. Here is if I increase the number:
Code:
```GRma:2 GR[]:4308.194000 6.090400

54GR:4308.194000 6.090400

61GR:4308.194000 6.090400

54GR:4308.194000 6.090400

61GR:0.000000 6.090400```
Here is if I decrease the value:
Code:
```GRma:2 GR[]:4.308194 6.090400

54GR:4.308194 6.090400

61GR:4.308194 6.090400

54GR:4.308194 6.090400

61GR:0.000000 6.090400```
Here is the pertinent declaration in my main() code:
Code:
```int main()
{
* * *
double GRbeta[GRma][0], GRalpha[GRma][GRma];
double **beta, **alpha, *a;
double GRX, GRN;

//  GRX = 430.8194;
GRX = 4.308194;
GRN = 6.0904;

GR[0] = GRX;
GR[1] = GRN;

alpha = dmatrix(1,GRma,1,GRma);
beta  = dmatrix(1,GRma,0,0);
a     = dvector(1,GRma);

for(i=1; i<=GRma; i++)
{
alpha[i] = GRalpha[i];
beta[i]  = GRbeta[i];
}
a = GR;

oFile << "GR:" << a[0] << " " << a[1] << endl;

fGR(x, y, sig, alpha, beta, GRma, GRchi, GRnu, GRQ, a);

* * *
}```
Here is the function as of now:
Code:
```void fGR(double x[NSTEPS], double y[NSTEPS], double sig[NSTEPS],
double **GRalpha, double **GRbeta, int ma, double chi,
int nu, double Q, double *GRa)
{}```
That is everything relevant to this problem. If there's anything I've missed, I'm basing this on the Numerical Recipes' gaussj and mrqmin functions.

8. As Mats pointed out, GRalpha[GRma] and GRbeta[GRma] just don't exist (arrays in C go from 0 to n-1, not 1 to n).