theEpsilon theEpsilon - 2 months ago 10
C++ Question

Setting double in c++ gives strange numbers

I am initializing a 2D double array in c++, setting some elements to 0 and others to 100. However, after I have initialized the value, I print it out and get truly strange numbers (like the following):

6.91599e-310
2.96439e-323
6.95262e-310
3.20006e-319
6.52231e-319
4.94066e-324
1.63971e-319


How can I make it so that the system prints out 0 instead of this? I already tried
fixed << setprecision(4) << setw(10)
but I'll still get numbers with a width much larger than 10 and with more than 4 digits after the decimal point. I know computers have problems with floating point, but why am I getting some values printed as
0
and others as the ones above? I am truly baffled.

EDIT:

Here is my (relevant) code:

void initializeArray (double Array[20][20]) {
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
if ((j == 0) || (j == 19)) {
Array[i][j] = 0.0;
}
else if ((i == 0) || (i == 19)) {
Array[i][j] = 100.0;
}
}
}
}

void printArray (double Array[20][20]) {
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 19; j++) {
cout << setprecision(4) << fixed << setw(10) << Array[i][j] << ",";
}
cout << setprecision(4) << fixed << setw(10) << Array[i][19] << endl;
}
}

Answer

The following block of code seems faulty.

         if ((j == 0) || (j == 19)) {
            Array[i][j] = 0.0;
         }
         else if ((i == 0) || (i == 19)) {
            Array[i][j] = 100.0;
         }

It ignores a lot of the elements of the array. I am willing to bet that those elements are left uninitialized.

Here's an updated program that demonstrates that a lot of the array elements are left uninitialized.

#include <iostream>

void initializeArray (double Array[20][20]) {
   for (int i = 0; i < 20; i++) {
      for (int j = 0; j < 20; j++) {
         if ((j == 0) || (j == 19)) {
            Array[i][j] = 0.0;
         }
         else if ((i == 0) || (i == 19)) {
            Array[i][j] = 100.0;
         }
         else
         {
            std::cout << "Not setting the value of Array[" << i << "][" << j << "]\n";
         }
      }
   }
}

int main()
{
   double Array[20][20];
   initializeArray (Array);
}

Output:

Not setting the value of Array[1][1]
Not setting the value of Array[1][2]
Not setting the value of Array[1][3]
Not setting the value of Array[1][4]
Not setting the value of Array[1][5]
Not setting the value of Array[1][6]
Not setting the value of Array[1][7]
Not setting the value of Array[1][8]
Not setting the value of Array[1][9]
Not setting the value of Array[1][10]
Not setting the value of Array[1][11]
Not setting the value of Array[1][12]
Not setting the value of Array[1][13]
Not setting the value of Array[1][14]
Not setting the value of Array[1][15]
Not setting the value of Array[1][16]
Not setting the value of Array[1][17]
Not setting the value of Array[1][18]
Not setting the value of Array[2][1]
Not setting the value of Array[2][2]
Not setting the value of Array[2][3]
Not setting the value of Array[2][4]
Not setting the value of Array[2][5]
Not setting the value of Array[2][6]
Not setting the value of Array[2][7]
Not setting the value of Array[2][8]
Not setting the value of Array[2][9]
Not setting the value of Array[2][10]
Not setting the value of Array[2][11]
Not setting the value of Array[2][12]
Not setting the value of Array[2][13]
Not setting the value of Array[2][14]
Not setting the value of Array[2][15]
Not setting the value of Array[2][16]
Not setting the value of Array[2][17]
Not setting the value of Array[2][18]
Not setting the value of Array[3][1]
Not setting the value of Array[3][2]
Not setting the value of Array[3][3]
Not setting the value of Array[3][4]
Not setting the value of Array[3][5]
Not setting the value of Array[3][6]
Not setting the value of Array[3][7]
Not setting the value of Array[3][8]
Not setting the value of Array[3][9]
Not setting the value of Array[3][10]
Not setting the value of Array[3][11]
Not setting the value of Array[3][12]
Not setting the value of Array[3][13]
Not setting the value of Array[3][14]
Not setting the value of Array[3][15]
Not setting the value of Array[3][16]
Not setting the value of Array[3][17]
Not setting the value of Array[3][18]
Not setting the value of Array[4][1]
Not setting the value of Array[4][2]
Not setting the value of Array[4][3]
Not setting the value of Array[4][4]
Not setting the value of Array[4][5]
Not setting the value of Array[4][6]
Not setting the value of Array[4][7]
Not setting the value of Array[4][8]
Not setting the value of Array[4][9]
Not setting the value of Array[4][10]
Not setting the value of Array[4][11]
Not setting the value of Array[4][12]
Not setting the value of Array[4][13]
Not setting the value of Array[4][14]
Not setting the value of Array[4][15]
Not setting the value of Array[4][16]
Not setting the value of Array[4][17]
Not setting the value of Array[4][18]
Not setting the value of Array[5][1]
Not setting the value of Array[5][2]
Not setting the value of Array[5][3]
Not setting the value of Array[5][4]
Not setting the value of Array[5][5]
Not setting the value of Array[5][6]
Not setting the value of Array[5][7]
Not setting the value of Array[5][8]
Not setting the value of Array[5][9]
Not setting the value of Array[5][10]
Not setting the value of Array[5][11]
Not setting the value of Array[5][12]
Not setting the value of Array[5][13]
Not setting the value of Array[5][14]
Not setting the value of Array[5][15]
Not setting the value of Array[5][16]
Not setting the value of Array[5][17]
Not setting the value of Array[5][18]
Not setting the value of Array[6][1]
Not setting the value of Array[6][2]
Not setting the value of Array[6][3]
Not setting the value of Array[6][4]
Not setting the value of Array[6][5]
Not setting the value of Array[6][6]
Not setting the value of Array[6][7]
Not setting the value of Array[6][8]
Not setting the value of Array[6][9]
Not setting the value of Array[6][10]
Not setting the value of Array[6][11]
Not setting the value of Array[6][12]
Not setting the value of Array[6][13]
Not setting the value of Array[6][14]
Not setting the value of Array[6][15]
Not setting the value of Array[6][16]
Not setting the value of Array[6][17]
Not setting the value of Array[6][18]
Not setting the value of Array[7][1]
Not setting the value of Array[7][2]
Not setting the value of Array[7][3]
Not setting the value of Array[7][4]
Not setting the value of Array[7][5]
Not setting the value of Array[7][6]
Not setting the value of Array[7][7]
Not setting the value of Array[7][8]
Not setting the value of Array[7][9]
Not setting the value of Array[7][10]
Not setting the value of Array[7][11]
Not setting the value of Array[7][12]
Not setting the value of Array[7][13]
Not setting the value of Array[7][14]
Not setting the value of Array[7][15]
Not setting the value of Array[7][16]
Not setting the value of Array[7][17]
Not setting the value of Array[7][18]
Not setting the value of Array[8][1]
Not setting the value of Array[8][2]
Not setting the value of Array[8][3]
Not setting the value of Array[8][4]
Not setting the value of Array[8][5]
Not setting the value of Array[8][6]
Not setting the value of Array[8][7]
Not setting the value of Array[8][8]
Not setting the value of Array[8][9]
Not setting the value of Array[8][10]
Not setting the value of Array[8][11]
Not setting the value of Array[8][12]
Not setting the value of Array[8][13]
Not setting the value of Array[8][14]
Not setting the value of Array[8][15]
Not setting the value of Array[8][16]
Not setting the value of Array[8][17]
Not setting the value of Array[8][18]
Not setting the value of Array[9][1]
Not setting the value of Array[9][2]
Not setting the value of Array[9][3]
Not setting the value of Array[9][4]
Not setting the value of Array[9][5]
Not setting the value of Array[9][6]
Not setting the value of Array[9][7]
Not setting the value of Array[9][8]
Not setting the value of Array[9][9]
Not setting the value of Array[9][10]
Not setting the value of Array[9][11]
Not setting the value of Array[9][12]
Not setting the value of Array[9][13]
Not setting the value of Array[9][14]
Not setting the value of Array[9][15]
Not setting the value of Array[9][16]
Not setting the value of Array[9][17]
Not setting the value of Array[9][18]
Not setting the value of Array[10][1]
Not setting the value of Array[10][2]
Not setting the value of Array[10][3]
Not setting the value of Array[10][4]
Not setting the value of Array[10][5]
Not setting the value of Array[10][6]
Not setting the value of Array[10][7]
Not setting the value of Array[10][8]
Not setting the value of Array[10][9]
Not setting the value of Array[10][10]
Not setting the value of Array[10][11]
Not setting the value of Array[10][12]
Not setting the value of Array[10][13]
Not setting the value of Array[10][14]
Not setting the value of Array[10][15]
Not setting the value of Array[10][16]
Not setting the value of Array[10][17]
Not setting the value of Array[10][18]
Not setting the value of Array[11][1]
Not setting the value of Array[11][2]
Not setting the value of Array[11][3]
Not setting the value of Array[11][4]
Not setting the value of Array[11][5]
Not setting the value of Array[11][6]
Not setting the value of Array[11][7]
Not setting the value of Array[11][8]
Not setting the value of Array[11][9]
Not setting the value of Array[11][10]
Not setting the value of Array[11][11]
Not setting the value of Array[11][12]
Not setting the value of Array[11][13]
Not setting the value of Array[11][14]
Not setting the value of Array[11][15]
Not setting the value of Array[11][16]
Not setting the value of Array[11][17]
Not setting the value of Array[11][18]
Not setting the value of Array[12][1]
Not setting the value of Array[12][2]
Not setting the value of Array[12][3]
Not setting the value of Array[12][4]
Not setting the value of Array[12][5]
Not setting the value of Array[12][6]
Not setting the value of Array[12][7]
Not setting the value of Array[12][8]
Not setting the value of Array[12][9]
Not setting the value of Array[12][10]
Not setting the value of Array[12][11]
Not setting the value of Array[12][12]
Not setting the value of Array[12][13]
Not setting the value of Array[12][14]
Not setting the value of Array[12][15]
Not setting the value of Array[12][16]
Not setting the value of Array[12][17]
Not setting the value of Array[12][18]
Not setting the value of Array[13][1]
Not setting the value of Array[13][2]
Not setting the value of Array[13][3]
Not setting the value of Array[13][4]
Not setting the value of Array[13][5]
Not setting the value of Array[13][6]
Not setting the value of Array[13][7]
Not setting the value of Array[13][8]
Not setting the value of Array[13][9]
Not setting the value of Array[13][10]
Not setting the value of Array[13][11]
Not setting the value of Array[13][12]
Not setting the value of Array[13][13]
Not setting the value of Array[13][14]
Not setting the value of Array[13][15]
Not setting the value of Array[13][16]
Not setting the value of Array[13][17]
Not setting the value of Array[13][18]
Not setting the value of Array[14][1]
Not setting the value of Array[14][2]
Not setting the value of Array[14][3]
Not setting the value of Array[14][4]
Not setting the value of Array[14][5]
Not setting the value of Array[14][6]
Not setting the value of Array[14][7]
Not setting the value of Array[14][8]
Not setting the value of Array[14][9]
Not setting the value of Array[14][10]
Not setting the value of Array[14][11]
Not setting the value of Array[14][12]
Not setting the value of Array[14][13]
Not setting the value of Array[14][14]
Not setting the value of Array[14][15]
Not setting the value of Array[14][16]
Not setting the value of Array[14][17]
Not setting the value of Array[14][18]
Not setting the value of Array[15][1]
Not setting the value of Array[15][2]
Not setting the value of Array[15][3]
Not setting the value of Array[15][4]
Not setting the value of Array[15][5]
Not setting the value of Array[15][6]
Not setting the value of Array[15][7]
Not setting the value of Array[15][8]
Not setting the value of Array[15][9]
Not setting the value of Array[15][10]
Not setting the value of Array[15][11]
Not setting the value of Array[15][12]
Not setting the value of Array[15][13]
Not setting the value of Array[15][14]
Not setting the value of Array[15][15]
Not setting the value of Array[15][16]
Not setting the value of Array[15][17]
Not setting the value of Array[15][18]
Not setting the value of Array[16][1]
Not setting the value of Array[16][2]
Not setting the value of Array[16][3]
Not setting the value of Array[16][4]
Not setting the value of Array[16][5]
Not setting the value of Array[16][6]
Not setting the value of Array[16][7]
Not setting the value of Array[16][8]
Not setting the value of Array[16][9]
Not setting the value of Array[16][10]
Not setting the value of Array[16][11]
Not setting the value of Array[16][12]
Not setting the value of Array[16][13]
Not setting the value of Array[16][14]
Not setting the value of Array[16][15]
Not setting the value of Array[16][16]
Not setting the value of Array[16][17]
Not setting the value of Array[16][18]
Not setting the value of Array[17][1]
Not setting the value of Array[17][2]
Not setting the value of Array[17][3]
Not setting the value of Array[17][4]
Not setting the value of Array[17][5]
Not setting the value of Array[17][6]
Not setting the value of Array[17][7]
Not setting the value of Array[17][8]
Not setting the value of Array[17][9]
Not setting the value of Array[17][10]
Not setting the value of Array[17][11]
Not setting the value of Array[17][12]
Not setting the value of Array[17][13]
Not setting the value of Array[17][14]
Not setting the value of Array[17][15]
Not setting the value of Array[17][16]
Not setting the value of Array[17][17]
Not setting the value of Array[17][18]
Not setting the value of Array[18][1]
Not setting the value of Array[18][2]
Not setting the value of Array[18][3]
Not setting the value of Array[18][4]
Not setting the value of Array[18][5]
Not setting the value of Array[18][6]
Not setting the value of Array[18][7]
Not setting the value of Array[18][8]
Not setting the value of Array[18][9]
Not setting the value of Array[18][10]
Not setting the value of Array[18][11]
Not setting the value of Array[18][12]
Not setting the value of Array[18][13]
Not setting the value of Array[18][14]
Not setting the value of Array[18][15]
Not setting the value of Array[18][16]
Not setting the value of Array[18][17]
Not setting the value of Array[18][18]

I am unable to suggest a fix since your initialization logic is not clear to me.