Incorrect values from array

void MyCode(){
    //data(# of points, # of dimensions)

    const int nBx    = 24;
    const int nBy    = 10;
    const int nBz    = 5;
    const int nTotal = 39;
    
    char temp_Bx[500];
    sprintf(temp_Bx,"BX_BV.dat");
    ifstream filein_Bx(temp_Bx);
    char temp_By[500];
    sprintf(temp_By,"BY_BV.dat");
    ifstream filein_By(temp_By);
    char temp_Bz[500];
    sprintf(temp_Bz,"BZ_BV.dat");
    ifstream filein_Bz(temp_Bz);

    double X_Bx[nBx], Y_Bx[nBx], Z_Bx[nBx], f_Bx[nBx];
    double X_By[nBy], Y_By[nBy], Z_By[nBy], f_By[nBy];
    double X_Bz[nBz], Y_Bz[nBz], Z_Bz[nBz], f_Bz[nBz];


    i = 0;
    while (!filein_Bx.eof())
    {
        filein_Bx >> X_Bx[i] >> Y_Bx[i] >> Z_Bx[i] >> f_Bx[i];
        if (filein_Bx.fail()) break;
        // Print out data to verify that code is reading it //
        cout << X_Bx[i] << '\t' << Y_Bx[i] << '\t' << Z_Bx[i] << '\t' << f_Bx[i]<< endl;
        i++;
    }


    i = 0;
    while (!filein_By.eof())
    {
        filein_By >> X_By[i] >> Y_By[i] >> Z_By[i] >> f_By[i];
        if (filein_By.fail()) break;
        // Print out data to verify that code is reading it //
        cout << X_By[i] << '\t' << Y_By[i] << '\t' << Z_By[i] << '\t' << f_By[i] << '\t' << endl;
        i++;
    }

    i = 0;
    while (!filein_Bz.eof())
    {
        filein_Bz >> X_Bz[i] >> Y_Bz[i] >> Z_Bz[i] >> f_Bz[i];
        if (filein_Bz.fail()) break;
        // Print out data to verify that code is reading it //
        cout << X_Bz[i] << '\t' << Y_Bz[i] << '\t' << Z_Bz[i] << '\t' << f_Bz[i] << '\t' << endl;
        i++;
    }

    Double_t listOfxCoords[nBx][3];
    Double_t listOfyCoords[nBy][3];
    Double_t listOfzCoords[nBz][3];

    for (int i = 0; i < nBx; i++){
        listOfxCoords[nBx][0] = X_Bx[i];
        listOfxCoords[nBx][1] = Y_Bx[i];
        listOfxCoords[nBx][2] = Z_Bx[i];
    }
    for (int i = 0; i < nBy; i++){
        listOfyCoords[nBy][0] = X_By[i];
        listOfyCoords[nBy][1] = Y_By[i];
        listOfyCoords[nBy][2] = Z_By[i];
    }
    for (int i = 0; i < nBz; i++){
        listOfzCoords[nBz][0] = X_Bz[i];
        listOfzCoords[nBz][1] = Y_Bz[i];
        listOfzCoords[nBz][2] = Z_Bz[i];
    }
    for (int i = 0; i < nBx; i++){
        cout << listOfxCoords[i][0] << "\t" << listOfxCoords[i][1] << "\t" << listOfxCoords[i][2] << endl;
    }

I’ll summarize the above code. First I define some constants. Then I read numbers from three data files (it’s not an issue of the format of the files I think because the output of the numbers from the file work) Then I define from arrays and put some number in. This is my output:

-14.8492	-14.8492	17	35.3045
-14.8492	-14.8492	10	35.435
-14.8492	-14.8492	0	34.5886
-14.8492	-14.8492	-10	33.1371
-14.8492	-14.8492	-17	31.9911
-14.8492	14.8492	-17	32.0216
-14.8492	14.8492	17	35.9479
14.8492	14.8492	17	27.1014
14.8492	14.8492	10	25.8471
14.8492	14.8492	0	24.3797
14.8492	14.8492	-10	23.0572
14.8492	14.8492	-17	21.9726
14.8492	-14.8492	10	30.5551
-21	0	17	40.096
-21	0	10	39.5165
-21	0	0	38.0866
-21	0	-10	36.3905
21	0	-10	25.177
21	0	0	26.0179
21	0	17	30.0884
0	-21	-10	29.0206
0	21	-17	24.3675
0	21	0	27.7086
0	21	10	28.5181
-14.8492	14.8492	0	10.2052	
14.8492	-14.8492	-10	-7.77521	
-21	0	-17	0.118734	
21	0	-17	-3.30971	
21	0	10	-3.19474	
0	-21	17	-12.6824	
0	-21	10	-10.4383	
0	-21	-17	-8.94926	
0	21	-10	12.0026	
0	21	17	15.929	
-14.8492	14.8492	-10	0.751485	
-14.8492	14.8492	10	-2.25447	
14.8492	-14.8492	-17	7.64471	
14.8492	-14.8492	0	2.70853	
14.8492	-14.8492	17	-3.00309	


0	21	17
0	6.95313e-310	6.95313e-310
6.95313e-310	6.94102e-310	0
0	0	6.94102e-310
0	6.94102e-310	0
6.94102e-310	0	0
-1.14327e-120	0	6.95293e-310
0	6.95313e-310	6.95313e-310
6.95313e-310	6.95313e-310	2.82789e-306
0	4.94066e-324	0
6.94102e-310	6.94102e-310	6.94102e-310
6.94102e-310	6.95313e-310	6.95313e-310
6.95313e-310	6.94102e-310	0
0	6.95313e-310	6.95313e-310
6.95313e-310	6.94102e-310	4.94066e-324
6.95313e-310	6.95313e-310	6.95313e-310
6.94102e-310	0	0
6.94102e-310	0	0
6.94102e-310	0	6.94102e-310
6.94102e-310	0	0
6.94102e-310	0	0
6.94102e-310	0	0
6.94102e-310	0	0
6.94102e-310	0	0

The first half of rows of numbers is no problem; that is the data set. The second set of numbers is the problem here. I have no idea what these numbers are. Where did I go wrong?

When filling the arrays:

should be

       listOfxCoords[i][0] = X_Bx[i];

and the same for all arrays.

1 Like