arrays - Why does this c++ code work properly in Linux but not in Windows -


i c++ student stuck in tight situation. have code seems functionally work (well, parts have completed) in memory allocation screwing in windows 7, not in ancient laptop runs ubuntu. of course, code compiled , tested on windows 7.

i post entire code can compile , attempt highlight relevant bits jamming up. code designed open file, parse file 2d array, , use array fill few others needed find maximum vales year , on.

following code text file used populate mainarray, referred "energy.dat". if necessary can upload .doc file explain purpose behind of this. gist is, different columns in file different types of energy, column index corresponding values in array "types[]". need sum multiple columns separate arrays "fossilarray", "renewablearray", , "totalarray". in which, in windows 7, getting filled junk values (i.e. 4.3e206), , different values on different computers. occurs shown in comments.

i have asked professor , believed operating system problem. appears true, lack knowledge , understanding fix it. input helpful.

#include <iostream> #include <cstdlib> #include <fstream> #include <iomanip> #include <cmath> #include <string> //#include <windows.h> using namespace std;  void drawline(); int returnmaxindex(double mainarray[][14], int j); int returnmaxindex(double array[]);  int main() {     string yearstring; char answer = '@'; char eanswer = '@';   //initialize value inside while loop      long int year = 1979;                   //same thing here      int i, j;     double temp;      double mainarray[29][14];  //every value loads     double fossilarray[29];  //index 9, 12, 13 junk values     double renewablearray[29]; //also gets occasional junk values     double totalarray[29];     //same here      string types[14] = {"year", "coal", "petroleum", "natural gas", "other gases", "nuclear",                             "hydroelectric (pumped storage)", "hydroelectric", "biomass-wood",                              "biomass-other", "geothermal", "solar", "wind", "other"};      ifstream file("energy.dat");      if(file.fail())         return -1;      for(i = 0; < 29; i++)         //load main array         for(j = 0; j < 14; j++)             file >> mainarray[i][j];      for(i = 0; < 29; i++)         //load fossil sum array         for(j = 1; j < 5; j++)             fossilarray[i] += mainarray[i][j];      for(i = 0; < 29; i++)         //load renewable energy array         for(j = 6; j < 13; j++)             renewablearray[i] += mainarray[i][j];      for(i = 0; < 29; i++)         //load total energy array         for(j = 1; j < 14; j++)             totalarray[i] += mainarray[i][j];  //  for(int = 0; < 29; i++) //  {   for(int j = 0; j < 14; j++) //          {cout << fixed << setprecision(0) << setw(10) << mainarray[i][j] << " ";    if(j == 6) cout << endl;} //      cout << endl << endl;}  //  for(i = 0; < 29; i++)         cout << setw(20) << std::right << fixed << fossilarray[12] << endl;      cout << "a. shows energy output year select between 1980 , 2008"<< endl         << "b. gives year in each form of energy has peak output" << endl         << "c. create excel compatible output file total of type of energy \n\tor percentage year" << endl;       while (static_cast<int>(answer) < 65 || (static_cast<int>(answer) > 67 && static_cast<int>(answer) < 97) || static_cast<int>(answer) > 99)     {         cout << "please choose option a, b, or c: ";         cin.get(answer);     }     drawline();     switch(answer)     {         case 'a': case 'a':         {             cout << "enter year in want see energy output(1980 - 2008): ";             while(year < 1980 || year > 2008)             {                 cout << endl;                 getline(cin, yearstring);                 year = strtol(yearstring.c_str(), null, 10);              }              drawline();              if(year == 1980) = 0; if(year == 1981) = 1; if(year == 1982) = 2; if(year == 1983) = 3; if (year == 1984) = 4; if(year == 1985) = 5;             if(year == 1986) = 6; if(year == 1987) = 7; if(year == 1988) = 8; if(year == 1989) = 9; if (year == 1990) = 10; if(year == 1991) = 11;             if(year == 1992) = 12; if(year == 1993) = 13; if(year == 1994) = 14; if(year == 1995) = 15; if (year == 1996) = 16; if(year == 1997) = 17;             if(year == 1998) = 18; if(year == 1999) = 19; if(year == 2000) = 20; if(year == 2001) = 21; if (year == 2002) = 22; if(year == 2003) = 23;             if(year == 2004) = 24; if(year == 2005) = 25; if (year == 2006) = 26; if(year == 2007) = 27; if(year == 2008) = 28;              cout << "the energy produced in year " << year << " was...(in megawatt hours)   % of total" << endl;             (j = 1; j < 14; j++)             {                 double temp = mainarray[i][j]; double temp2 = totalarray[i];                 cout << setw(36)<< std::left << types[j]  << fixed << setprecision(3) << std::right << setw(16) << mainarray[i][j] << "     " << setprecision(3) << (temp*1.0/temp2)*100.0 << endl;                 //(abs(mainarray[i][j]))/(totalarray[j])             }             drawline();           }break;          case 'b': case 'b':         {             cout << "a. coal  b. petroleum  c. natural gas  d. other gas  e. nuclear" << endl                 << "f. hydro pumped storage  g. hydroelectric  h. biomass-wood" << endl;                 drawline();             cout << "i. biomass-waste  j. geothermal  k. solar  l. wind  m. other energy" << endl                 <<"n. fossil fuels  o. renewable  p. grand total" <<endl;             drawline();              cout << "enter type of energy want see the peak output year: ";             while(static_cast<int>(eanswer) < 65 || (static_cast<int>(eanswer) > 80  && static_cast<int>(eanswer) < 97) || static_cast<int>(eanswer) > 112)             {                 cin.get(eanswer);             }             switch(eanswer)             {                 case 'a': case 'a': j = 1; break; case 'b': case 'b': j = 2; break; case 'c': case 'c': j = 3; break; case 'd': case 'd': j = 4; break;                 case 'e': case 'e': j = 5; break; case 'f': case 'f': j = 6; break; case 'g': case 'g': j = 7; break; case 'h': case 'h': j = 8; break;                 case 'i': case 'i': j = 9; break; case 'j': case 'j': j = 10; break; case 'k': case 'k': j = 11; break; case 'l': case 'l': j = 12; break;                 case 'm': case 'm': j = 13; break; case 'n': case 'n': j = 14; break; case 'o': case 'o': j = 15; break; case 'p': case 'p': j = 16; break;             }             if(j > 0 && j < 14)             {                 = returnmaxindex(mainarray, j);                 cout << "the year of maximum energy production " << types[j] << " " << mainarray[i][0];             }             if(j == 14)             {                 = returnmaxindex(fossilarray);                 cout << "the year of maximum energy production fossil fuels " << mainarray[i][0];             }             if(j == 15)             {                 = returnmaxindex(renewablearray);                 cout << "the year of maximum energy production renewable energy " << mainarray[i][0];             }             if(j == 16)             {                 = returnmaxindex(totalarray);                 cout << "the year of maximum energy production total energy production " << mainarray[i][0];             }         }         break;          case 'c': case 'c':         break;      } }  int returnmaxindex(double array[]) {     double temp = array[0];     for(int = 0; < 29; i++)     {         if(temp < array[i])             temp = array[i];     }     cout << temp << endl;     for(int = 0; < 29; i++)     {         if (temp == array[i])             return i;     } }   int returnmaxindex(double mainarray[][14], int j) {     int k = 0;     double temp = labs(mainarray[k][j]);     for(int = 1; < 29; i++)     {         if(temp < labs(mainarray[i][j]))             temp = labs(mainarray[i][j]);     }     for(int = 0; < 29; i++)     {         if (temp == labs(mainarray[i][j]))             return i;     } }  void drawline() {     cout << "---------------------------------------------------------------------------" << endl; } 

contents of energy.dat

1980  1161562368  245994189  346239900        0  251115575         0  276020970    275366    157797   5073079       0         0        0 1981  1203203232  206420775  345777173        0  272673503         0  260683544    245201    122628   5686163       0         0        0 1982  1192004204  146797490  305259749        0  282773248         0  309212893    195940    124979   4842865       0         0        0 1983  1259424279  144498593  274098458        0  293677119         0  332129735    215867    162745   6075101       0      2668        0 1984  1341680752  119807913  297393596        0  327633549         0  321150245    461411    424540   7740504    5248      6490        0 1985  1402128125  100202273  291945965        0  383690727         0  281149418    743294    639578   9325230   10630      5762        0 1986  1385831452  136584867  248508433        0  414038063         0  290844099    491509    685234  10307954   14032      4189        0 1987  1463781289  118492571  272620803        0  455270382         0  249694973    783088    693941  10775461   10497      3541        0 1988  1540652774  148899561  252800704        0  526973047         0  222939683    935986    738258  10300079    9094       871        0 1989  1562366197  159004961  297295127   454066  529354717         0  269189209   5582109   7742914  14593443  250601   2112043   282046 1990  1572108922  118863929  309486351   621112  576861678  -3507741  289753124   7032446  11499927  15434271  367087   2788600    11913 1991  1568845635  112798164  317773359   719074  612565087  -4541435  286019443   7735675  13853928  15966444  471765   2950951   402581 1992  1597713819   92237912  334274122  1212475  618776263  -4176582  250015684   8491095  15923885  16137962  399640   2887523   479806 1993  1665464154  105425325  342221829   966508  610291214  -4035572  277523663   9151852  16223338  16788565  462452   3005827   407651 1994  1666276091   98676618  385689325  1092023  640439832  -3377825  254004826   9232281  16983843  15535453  486622   3447109   239129 1995  1686056319   68145851  419178592  1926832  673402123  -2725131  305410435   7596774  17985777  13378258  496821   3164253   213275 1996  1771972991   74782864  378757294  1341140  674728546  -3088078  341158836   8386379  17816200  14328684  521205   3234069   201222 1997  1820761761   86479050  399595822  1533366  628644171  -4039905  350647962   8680229  18484565  14726102  511168   3288035    62807 1998  1850193304  122211090  449292578  2314896  673702104  -4467280  317866620   8608130  19233174  14773918  502473   3025696   158942 1999  1858617724  111539127  472995956  1606583  728254124  -6096899  314663058   8960705  19493050  14827013  495082   4487998   138942 2000  1943111290  105192123  517977999  2027956  753892940  -5538860  271337693   8916073  20307087  14093158  493375   5593261   124885 2001  1882826136  119148891  554939683   585791  768826308  -8823445  213749291   8293796  12944430  13740501  542755   6737332  6541565 2002  1910612813   89733266  607683246  1969851  780064087  -8742928  260491387   9009328  13145020  14491310  554831  10354279  9091465 2003  1952713826  113697198  567303392  2647093  763732695  -8535065  271511659   9527678  13807633  14424231  534001  11187467  8607470 2004  1957187710  114678307  627171620  3568233  788528387  -8488210  265063848   9736404  13061787  14810975  575155  14143741  8322440 2005  1992053878  116481854  683828924  3777156  781986365  -6557788  267039777  10569886  13031085  14691745  550294  17810549  6928168 2006  1969737146   59708237  734416872  4253528  787218636  -6557842  286253922  10341481  13927432  14568029  507706  26589137  7112762 2007  1998390297   61306315  814751904  4042131  806424753  -6896352  245842714  10711289  14294304  14637213  611793  34449927  6776960 2008  1976173298   42301486  798574077  3195712  806181935  -6238403  246100140  10901875  14872266  14859238  843054  52025898  6879905 

fossilarray seems not initialized before fossilarray[i] += mainarray[i][j]; line.

the same may hold other arrays well.


the problem on line:

double fossilarray[29]; 

the c++ standard doesn't guarantee local variables initialized. visual studio sets uninitialized local variables non-zero value locating problem.


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -