Errors with array

This is a discussion on Errors with array within the C Programming forums, part of the General Programming Boards category; I am trying to run a C source code about Evolutionary Programming. I get these errors after the compilation. Anyone ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    6

    Errors with array

    I am trying to run a C source code about Evolutionary Programming.
    I get these errors after the compilation.
    Anyone can help me to fix it? I am using VC 2005.
    I need help urgently.


    Here is the error part:--

    Code:
    double function14(int n)
    {
    double value;
    int i,j;
    
    double sum1,sum2,prod;
    
    double a[n][25];
    
    for(j=0;j<25;j=j+5)
    {
    a[0][j]=-32.0;
    a[0][j+1]=-16.0;
    a[0][j+2]=0.0;
    a[0][j+3]=16.0;
    a[0][j+4]=32.0;
    }
    error C2057: expected constant expression
    error C2466: cannot allocate an array of constant size 0
    error C2133: 'a' : unknown size


    Thanks everyone

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    double a[n][25];
    You can't use a variable as an array size when you declare an array. (except in C99 which most compilers don't support yet).
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    6
    Quote Originally Posted by cpjust View Post
    Code:
    double a[n][25];
    You can't use a variable as an array size when you declare an array. (except in C99 which most compilers don't support yet).
    Thanks for your quick response.
    Actually i downloaded this program from internet.
    I am trying to run it in my pc for studying about the Evolutionary Programming.

    So i need to run it without changing the functions of this program.

    Do you have any suggestion for me to make it run in my pc?

    Thanks a lot.

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Use malloc to dynamically create this array.

  5. #5
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    You could initialize a[n][25] with a constant value for n that is bigger than you would expect to see (I don't know what the function is for so I can't suggest one). You can use n everywhere else, though.

    Or, to save space, you can make a[n] (where n is a large number) and have each index be a pointer to an array b[25].

    i.e.,
    Code:
    double *a[255]
    double b[25]
    a[0]=b
    I think this will work, correct me if I'm wrong.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    6
    Code:
    double function19(int n)
    {
      double value;
      int i,j;
      double sum1,sum2;
      double a[4][n],p[4][n];
      double c[4];
     
    
     
    
    /* assign the values of left and right boundary  */
      for(i=0;i<n;i++)
      {
        left_bound[i]=0;
        right_bound[i]=1;
      }
    
    
        
        a[0][0]=3.0;
        a[0][1]=10.0;
        a[0][2]=30.0;
    
        a[1][0]=0.1;
        a[1][1]=10.0;
        a[1][2]=35.0;
    
        a[2][0]=3.0;
        a[2][1]=10.0;
        a[2][2]=30.0;
    
        a[3][0]=0.1;
        a[3][1]=10.0;
        a[3][2]=35.0;
    
        c[0]=1;
        c[1]=1.2;
        c[2]=3;
        c[3]=3.2;
    
        p[0][0]=0.3689;
        p[0][1]=0.1170;
        p[0][2]=0.2673;
    
        p[1][0]=0.4699;
        p[1][1]=0.4387;
        p[1][2]=0.7470;
    
        p[2][0]=0.1091;
        p[2][1]=0.8732;
        p[2][2]=0.5547;
    
        p[3][0]=0.038150;
        p[3][1]=0.5743;
        p[3][2]=0.8828; 
      
     sum1=0.0;
     
     for(i=0;i<4;i++)
     {
       sum2=0.0;
       
       for(j=0;j<n;j++)
       {
         sum2=sum2+a[i][j]*pow(z[j]-p[i][j],2.0);
       }
       sum1=sum1+c[i]*exp(-sum2);
     }
     value=-sum1;
     return value;
    }
    I am having the same errors for this part.
    So sorry for the silly questions, i'm a beginner in C programming.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Quote Originally Posted by wingster View Post
    I am having the same errors for this part.
    So sorry for the silly questions, i'm a beginner in C programming.
    And the reason is the same:

    Code:
    double function19(int n)
    {
      double value;
      int i,j;
      double sum1,sum2;
      double a[4][n],p[4][n];
      double c[4];
    [edit]I don't see a declaration for the z array, is it a global? Plus, even if the array part worked, it would assume that n was always at least 3 (would that be checked in the calling func?).[/edit]
    Last edited by hk_mp5kpdw; 05-14-2009 at 01:36 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    6
    Thanks again to everyone.
    Now i am facing another errors in my program.

    Code:
    void initilization(int n)
    {
      int i,j;
      double segment;
     
      unsigned int start_time;
    
      time_t time1;
    
      (void) time(&time1);
      start_time=(int)time1;
      srand(start_time);
    
      for(j=0;j<max_n;j++)
      {
        z[j]=0.0;
      }
    
      for(i=0;i<population_size;i++)
      {
        individual_f[i]=function1(n);
      }
    
    /* assign the initial values of points */
    
      for(i=0;i<population_size;i++)
      {
        for(j=0;j<n;j++)
        {
          individual_x[i][j]=left_bound[j]+rand()%10*0.1*(right_bound[j]-left_bound[j]);
          new_individual_x[i][j]=individual_x[i][j];
        }
      }
    
    /* compute their fitness */
      for(i=0;i<population_size;i++)
      {
        for(j=0;j<n;j++)
        {
          z[j]=individual_x[i][j];
        }
        individual_f[i]=function(n);
        new_individual_f[i]=individual_f[i];
        //   printf("\n fitness[%d]=%10.5f",i,individual_f[i]);
      }
    
      /* assign the initial values of sigmas */
      
    
      
      for(i=0;i<population_size;i++)
      {
        
        for(j=0;j<n;j++)
        {
          individual_sigma[i][j]=3.0;//sqrt(0.1*(right_bound[j]-left_bound[j]));
          new_individual_sigma[i][j]=individual_sigma[i][j];
         }
      }
    
    /* assign the initial values of mixed distribution */
      for(i=0;i<population_size;i++)
      {
        individual_mp[i]=rand()%no_strategy;
        new_individual_mp[i]=individual_mp[i];
    
      }
      for(i=0;i<no_strategy;i++)
      {
        for(j=0;j<no_strategy;j++)
        {
            p_history[i][j]=1.0/(double)no_strategy;
        }
      }
      /* assign the performance of each strategy a non-zero tiny positive */
      for(i=0;i<no_strategy;i++)
      {
        performance_of_best_mp[i]= 1.0e-10;
      }
    }
    error LNK2019: unresolved external symbol _function referenced in function _initilization
    fatal error LNK1120: 1 unresolved externals

    How can i fix this error?

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,196
    Within your function initilization() you are doing this.
    Quote Originally Posted by wingster View Post
    Code:
    void initilization(int n)
    {
      /*   Some stuff removed as not relevant to the error message */
    
    
    /* compute their fitness */
      for(i=0;i<population_size;i++)
      {
        for(j=0;j<n;j++)
        {
          z[j]=individual_x[i][j];
        }
        individual_f[i]=function(n);
        new_individual_f[i]=individual_f[i];
        //   printf("\n fitness[%d]=%10.5f",i,individual_f[i]);
      }
    
      /*   Other stuff removed as not relevant to the error message */
    }
    and then you get
    Quote Originally Posted by wingster View Post
    error LNK2019: unresolved external symbol _function referenced in function _initilization
    fatal error LNK1120: 1 unresolved externals
    "unresolved external symbol XXXX" is the way the linker tells you it tried to find something named XXXX and couldn't find it.

    In this case, the reason is that you have not implemented function() anywhere.

    The fix is either to remove the call of function from the line I've highlighted in red, or to actually provide an implementation of function().

    It would help in future if you try to interpret the error messages rather than just giving up and asking for someone else to tell you what's wrong. Error messages from compilers and linkers may be a little geeky, but they are usually self-explanatory if you think things through.
    Right 98% of the time, and don't care about the other 3%.

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    6
    The function have implemented here

    Code:
    double function1(int n)
    {
      double value;
      int i,j;
    
    /* assign the values of left and right boundary  */
      for(j=0;j<n;j++)
      {
        left_bound[j]=-100.0;
        right_bound[j]=100.0;
      }
      value =0.0;
      for(j=0;j<n;j++)
      {
        value=z[j]*z[j]+value;
      }
      return value;
    }
    
    
    /* function  9 (5 in Lee)*/
    double function9(int n)
    {
      double value;
      int i,j;
    
    
    
    /* assign the values of left and right boundary  */
      for(j=0;j<n;j++)
      {
        left_bound[j]=-5.12;
        right_bound[j]=5.12;
      }
    
      value =0.0;
      for(i=0;i<n;i++)
      {
        value=z[i]*z[i]-10.0*cos(2*M_PI*z[i])+10.0+value;
      }
      return value;
    }
    but still not working. Thanks for your comments.

  11. #11
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    What errors are you getting now?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    6
    Quote Originally Posted by BEN10 View Post
    What errors are you getting now?
    The error is this:
    error LNK2019: unresolved external symbol _function referenced in function _initilization
    fatal error LNK1120: 1 unresolved externals

  13. #13
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    It seems you have function1 and function9 defined, but I don't function. You need to define function without any numbers, or you have a typo in your code
    Code:
        individual_f[i]=function(n);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  2. Code: An auto expanding array (or how to use gets() safely).
    By anonytmouse in forum Windows Programming
    Replies: 0
    Last Post: 08-10-2004, 12:13 AM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. mode of an array
    By Need Help in forum C Programming
    Replies: 15
    Last Post: 09-17-2001, 08:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21