Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   function with structure (http://www.go4expert.com/forums/function-with-structure-t21907/)

troika 27Apr2010 12:59

function with structure
 
Hello,
I have the code below, and what I am trying to do is to make a function which will calculate the fitness of each old_chrome[i], form the old_chrome[i].genes[j] parameters. Particular the two print functions in the code should have the same result. The first prints the result by calculating them in the loop and the second prints the result from the function that calls. Clearly from the output that I have below this doesn't work. Any solution?

The first lines are the reading of input. Please check the numbers after the the fitness of chromosome and its not true shall be the same.


0 0 0.110000
0 1 3.100000
0 2 5.120000
1 0 2.530000
1 1 2.270000
1 2 4.130000
2 0 3.100000
2 1 1.240000
2 2 7.230000
3 0 6.780000
3 1 7.450000
3 2 3.390000
8.220000
the fitness of chromosome 0 8.220000
its not true 6.200000
the fitness of chromosome 1 6.400000
its not true 6.200000
the fitness of chromosome 2 8.470000
its not true 6.200000
the fitness of chromosome 3 10.840000
its not true 6.200000


Many thanks

Code:


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>






    struct chromosome {

 
      double fitness;
      double genes[3];       
                        };

struct chromosome old_chrome[4];


double fit(struct chromosome *p);




    main()

{


  /****DECLARE VARIABLES****/

  FILE *fpl_0;
  FILE *fpl_1;
  FILE *fpl_2;
  FILE *fpl_3;

  double n1;
  int i, j,n2;
  int k=0;
  int num_of_chromes;
  int num_of_genes;
  char line[100];
  /*****************************/





  /********READ INPUT DATA******/





  fpl_0=fopen("data_fit", "r");

  if (fpl_0==NULL)
    {
      printf("UNABLE TO OPEN INPUT FILE TO READ PROGRAM PARAMETERS\n");
    exit(1);
    }

  while (fgets(line,100,fpl_0)!=NULL)
    {

      sscanf(line,"%d %d", &num_of_chromes, &num_of_genes);
    }

  fclose(fpl_0);






 
  fpl_1=fopen("input_fit","r");


  if (fpl_1==NULL)
    {
      printf("UNABLE TO OPEN INPUT FILE TO READ GENES DATA\n");
      exit(2);
    }



  for (i=0;i<num_of_chromes;i++)

    {
      for (j=0;j<num_of_genes;j++)

    {
      fscanf(fpl_1,"%lf",&old_chrome[i].genes[j]);
        }
   
    }


  fclose(fpl_1);
 
  /********************CLOSE INPUT FILES*************/
 




  /****************PRINT INPUT DATA*****************/

  printf("*****************INPUT*************************\n");
 

  for(i=0;i<num_of_chromes;i++)
    {
      for (j=0;j<num_of_genes;j++)

    {
      printf("%d %d %lf\n",i,j,old_chrome[i].genes[j]);

    }     


 
    }

 printf("%lf\n",old_chrome[0].genes[1]+old_chrome[0].genes[2]);



 /* UNTILL HERE READ DATA TO STRUCTURE FROM FILE AND CHECK IF IS OK***/

 for (i=0;i<num_of_chromes;i++)

  {
    old_chrome[i].fitness=old_chrome[i].genes[1]+old_chrome[i].genes[2];

    printf("the fitness of chromosome %d %lf \n", i,old_chrome[i].fitness);
 
 
 
    n1=fit(old_chrome[i]);

    printf("its not true %lf\n",n1);
   
  }
}



/*******fitness function******/


double fit(struct chromosome *p)

{
 
  int i;
  double a;

  a = p->genes[1]+p->genes[2];
  return a;

}


troika 27Apr2010 13:39

Re: function with structure
 
I found it I need a &old_chrome when I call the function


All times are GMT +5.5. The time now is 13:31.