hello to everyone,
I would welcome some help. As you may have understand from the code, I would like to make a function which calculate a value a for each old_chrome[i] by using the old_chrome[i].genes[j] parameters.
Do u have any idea how to do?
Many thanks on behalf
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
double fit(int i, double old_chrome[i].genes[j]);
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);
struct chromosome {
double fitness;
double genes[num_of_genes];
};
struct chromosome temp_chrome[num_of_chromes], old_chrome[num_of_chromes];
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]);
}
}
/*
if (fpl_2==NULL)
{
printf("UNABLE TO OPEN INPUT FILE TO READ RANGE DATA\n");
exit(3);
}
if (fpl_3==NULL)
{
printf("UNABLE TO OPEN INPUT FILE TO READ INIT_VALUE DATA\n");
exit(4);
}
*/
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(i);
printf("its not true %lf\n",n1);
}
}
/*******fitness function******/
double fit(int i, double old_chrome[i].genes)
{
double a;
a= old_chrome[i].genes[0]+old_chrome[i].genes[1];
return a;
}