Thread: confused with structure in function

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    79

    confused with structure in function

    Hello to all,

    I posted again today. I have the code bellow where I am trying to get the same results in the two print outputs ( the fitness of chromosome SAME and its not true SAME ) for each old_chrome

    the one is the result without the function and the other with the function

    BUT MY OUTPUT IS AS BELOW


    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

    The first lines is the reading of the parameters.

    I would welcome some help about how it will work and some explanation if you also could


    Many thanks
    Chris


    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 DAT..........****/
    
    
    
    
    
      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 DAT..........***************/
    
      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);
    
         printf("its not true %lf\n",n1);
         
       } 
    }
    
    
    
    /*******fitness function******/
    
    
    double fit(struct chromosome *p)
    
    {
      
      int i;
      double a;
    
      a = p->genes[1]+p->genes[1];
      return a;
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > old_chrome[i].genes[1]+old_chrome[i].genes[2];
    vs
    > p->genes[1]+p->genes[1];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    79
    ok but if you look carefully I am taking the same number. This is not the problem !!!!!

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    79
    The problem is that clearly I miss the point with the function

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Chris_1980 View Post
    ok but if you look carefully I am taking the same number. This is not the problem !!!!!
    In addition to 2 being a different number from 1, you are always passing old_chrome[0] to the function, despite apparently desiring to pass old_chrome[i].

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM