Menu

compiles but unable to execute .exe

phnealjr
2008-09-09
2012-09-26
  • phnealjr

    phnealjr - 2008-09-09

    This is so frustrating!

    I was writing a program on method of linear regression... everything was going fine until i tried to compile and run it..

    it now seemed to compile 'successfully' superfast... and when it attempted to run, the black .exe window would come up empty. soon after, the XP "unknown error has occured" message came up..

    i tried going back and putting the code back where it was.. didnt help.
    i also tried to compile and run a hello world program. it worked... but hello world wont get me a grade in class!
    i'm somewhat of a noob.

    here is the code:

    include<iostream>

    include<iomanip>

    include<cmath>

    using namespace std;

    int main()
    {
    int n;
    float a=0;
    float b=0;
    float c=0;
    float det;

    float sumx=0;
    float sumy=0;
    float sumxy=0;
    float sumx2=0;
    
    float Xarray [n];
    float Yarray [n];
    
    char response;
    
            cout&lt;&lt;&quot;Linear Regression&quot;&lt;&lt;endl;
            cout&lt;&lt;&quot;How many data values?&quot;&lt;&lt;endl;
            cin&gt;&gt;n; 
            cout&lt;&lt;endl;
    
    cout&lt;&lt;&quot;Enter &quot;&lt;&lt;n&lt;&lt;&quot; X values separated by 'Enter':&quot;&lt;&lt;endl;
    
    for(int i=0;i&lt;n;i++)
    {
        cin&gt;&gt;Xarray[i];
    }
    
    cout&lt;&lt;endl&lt;&lt;&quot;Enter &quot;&lt;&lt;n&lt;&lt;&quot; Y values seperated by 'Enter':&quot;&lt;&lt;endl;
    
    for(int i=0;i&lt;n;i++)
    {
        cin&gt;&gt;Yarray[i];
    }
    
       cout&lt;&lt;endl&lt;&lt;endl;
    
       cout&lt;&lt;&quot;Are you expecting a power relation? (y/n)&quot;&lt;&lt;endl;
       cin&gt;&gt;response;
       cout&lt;&lt;endl;
    
       if (response == 'y')
       {     
              for (int j=0;j&lt;n;j++)
              {
                     Xarray[j]=log(Xarray[j]);
                     Yarray[j]=log(Yarray[j]);
                     sumx=sumx+Xarray[j];
                     sumy=sumy+Yarray[j];
                     sumxy=sumxy+(Xarray[j]*Yarray[j]);
                     sumx2=sumx2+(Xarray[j]*Xarray[j]);
              }
    
              det=(n*sumx2)-(sumx*sumx);
    
              if (det&gt;0)
              {
                     a=((sumy*sumx2)-(sumx*sumxy))/det;
                     b=((n*sumxy)-(sumy*sumx))/det;
    
                     c=exp(a);
    
                            cout&lt;&lt;&quot;The best fitting straight line is:\n\n\ty = &quot;&lt;&lt;a&lt;&lt;&quot; + &quot;&lt;&lt;b&lt;&lt;&quot;x&quot;&lt;&lt;endl&lt;&lt;endl;
                            cout&lt;&lt;&quot;The power equation is:\n\n\ty = &quot;&lt;&lt;b&lt;&lt;&quot; x^&quot;&lt;&lt;c&lt;&lt;endl&lt;&lt;endl;
              }
    
              else
              {
                     cout&lt;&lt;&quot;No solution.&quot;&lt;&lt;endl;       
              }      
       }
    
       else
       {
              for (int j=0;j&lt;n;j++)
              {
                     sumx=sumx+Xarray[j];
                     sumy=sumy+Yarray[j];
                     sumxy=sumxy+(Xarray[j]*Yarray[j]);
                     sumx2=sumx2+(Xarray[j]*Xarray[j]);
              }
    
              det=(n*sumx2)-(sumx*sumx);
    
              if (det&gt;0)
              {
                     a=((sumy*sumx2)-(sumx*sumxy))/det;
                     b=((n*sumxy)-(sumy*sumx))/det;
    
                     cout&lt;&lt;&quot;For data points:\n\t x\t y&quot;&lt;&lt;endl;
    
                     for(int k=0;k&lt;n;k++)
                     {
                            cout&lt;&lt;Yarray[k]&lt;&lt;&quot;\t&quot;&lt;&lt;Xarray[k]&lt;&lt;endl;
                     }
    
                            cout&lt;&lt;&quot;\nThe best fitting straight line is:\n\n\ty = &quot;&lt;&lt;a&lt;&lt;&quot; + &quot;&lt;&lt;b&lt;&lt;&quot;x&quot;&lt;&lt;endl;
              }
    
              else
              {
                     cout&lt;&lt;&quot;No solution.&quot;&lt;&lt;endl;        
              }   
       }
    
      cout&lt;&lt;endl&lt;&lt;endl;
    
    system(&quot;pause&quot;);
    return 0;
    

    }

    and here is the compile log:

    Compiler: Default compiler
    Executing g++.exe...
    g++.exe "C:\Documents and Settings\Philip Jr\Desktop\LinearRegression.cpp" -o "C:\Documents and Settings\Philip Jr\Desktop\LinearRegression.exe" -g3 -I"C:\Program Files\Dev-Cpp\include\c++" -I"C:\Program Files\Dev-Cpp\include\c++\mingw32" -I"C:\Program Files\Dev-Cpp\include\c++\backward" -I"C:\Program Files\Dev-Cpp\include" -L"C:\Program Files\Dev-Cpp\lib"
    Execution terminated
    Compilation successful

    What should I do?
    I need help fast!

     
    • phnealjr

      phnealjr - 2008-09-11

      Wow!

      Thank you so much.
      Super helpful! Great tips.

      Such a fast response too!

      Thanks again

       
    • cpns

      cpns - 2008-09-09

      > everything was going fine until i tried to compile and run it..

      Which is really everything, so nothing went well apart from your typing perhaps! ;)

      It is not unusual for code to contain bugs and so fail on first time execution. I would be amazed if it did work first time, but you seem genuinely surprised. Get used to it. The first course of action should be to work on debugging your code, and that does not necessarily involve posting a messge to a forum at the first sign of trouble. That's the slow way.

      I built your code in VC++ 2008 and it does not actually compile. The reason is these:

      float Xarray [n]; 
      float Yarray [n];
      

      Now you can do that in GNU C++ because it supports variable length arrays, however that is unusual. It is allowed uncer ISO C99 which few compilers support, it is not currently part of standard C++ so you should probably avoid it.

      However it does point to the rather obvious problem in your code. The variable n is unitialised, so it is anyone's guess how large those two arrays are. They are likely either very short and your code is overrunning the buffer, or very large and you have exceeded the process stack limit.

      You could move the array declarations to after the value for n is entered, and hope that the user enters a sensible value and does not blow your stack. This stills relies on the variable array support extension, so I would not recommend it.

      You could set them to some reasonable maximum limit, and coerce n to that limit if the user enters a higher value or reject the user input and ask again.

      You could dynamically allocate the arrays thus:

      cout&lt;&lt;&quot;How many data values?&quot;&lt;&lt;endl; 
      cin&gt;&gt;n;  
      float* Xarray = new float[n]; 
      float* Yarray = new float[n];
      

      and then when done with the arrays:

      delete [] Xarray ;
      delete [] Yarray ;
      

      Since you are expecting your users to enter the data manually, I suspect that the maximum number of values will be low, so the fixed array size and range check approach would be reasonable.

      I modified the code using dynamic memory allocation and it runs fine. Note that the way cin works, any whitespece is a delimiter, so you don't need to press <enter> between each value, you can equally enter a space separated list.

      Your user input has no error checking, it is easy to mess up the program or even make it crash by entering something other than the expected input. This may or may not be important in this assignment, but should be considered in real applications.

      From a usability point of view, I would expect to enter the coordinates in x,y pairs, not all x's followed by all y's. People do not normally think like that, an their data is not usually provided like that. For example (omitting any error checking), replacing your two input loops with:

      cout&lt;&lt;&quot;Enter &quot;&lt;&lt;n&lt;&lt;&quot; X,Y value pairs:&quot;&lt;&lt;endl;
      
      for(int i=0;i&lt;n;i++) 
      { 
          cin&gt;&gt;Xarray[i]; 
          cin&gt;&gt;Yarray[i]; 
      }
      

      allows the data to be entered thus:


      Enter 3 X,Y value pairs:
      1 1
      2 2
      3 3


      Which is both simpler code and more user friendly. Note it does not matter whether <enter> or <space> (or even <tab>!) is used in the input. I could have entered all values on one line, or on six separate lines.

      Other issues:

      Do not place projects in "My Documents", or "Desktop". The user profile path "C:\Documents and Settings\Philip Jr\&quot; contains spaces, and this can cause problems with the toolchain.

      For the same reason, you should not have installed Dev-C++ in "Program Files". Let it install in its default location to avoid problems.

      Also your log suggests that you may be using an old version of Dev-C++. You did not state the version. Currently you should use 4.9.9.2. Follow the instructions in the "PLEASE READ BEFORE POSTING A QUESTION" thread to the letter for uninstalling and reinstalling. That thread also mentions the spaces in paths issue and thE request to include the version number in posts.

      Clifford

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.