user2019482
user2019482

Reputation: 21

Struct variables printing

I am writing a genetic algortihm and i have a problem in printing some struct variables. I know that the code reads them properly because i get a result but when i try to print them on screen or in a file a get zeros..

This is the code i am using can you please help me out...??

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

#define POPSIZE 50
#define MAXGENS 1000
#define NVARS 3
#define PXOVER 0.8
#define PMUTATION 0.15
#define B 2
#define TOURNAMENT_SIZE 2
#define TRUE 1
#define FALSE 0

int generation ;
int cur_best;
FILE *galog_tournament;

struct genotype
{
    double gene[NVARS];
    double fitness;
    double upper[NVARS];
    double lower[NVARS];
    double cfitness;
    double rfitness;
};

struct genotype population[POPSIZE+1];
struct genotype newpopulation[POPSIZE+1];


void initialize(void);
double randval(double,double);
void evaluate(void);
void keep_the_best(void);
void elitist(void);
void tournament_selection(void);
void crossover(void);
void random_Xover(int,int);
void swap(double *,double *);
void non_uniform_mutation(void);
void report(void);
double delta2(int,int);


void initialize(void)
{
    FILE *infile;
    FILE *bounds;
    FILE *popu;
    int i,j;
    double lbound,ubound;

    bounds = fopen("bounds.txt","w");
    popu = fopen("population.txt","w");


    if((infile = fopen("gadata.txt","r"))== NULL)
    {
        fprintf(galog_tournament,"\n Cannot open input file\n");
        exit(1);
    }


    for(i = 0; i < NVARS; i++)
    {
        fscanf(infile, "%lf",&lbound);
        fscanf(infile, "%lf",&ubound);

        printf("%lf  %lf\n",lbound,ubound);

        for(j = 0; j < NVARS; j++)
            {
                population[j].fitness = 0;
                population[j].rfitness = 0;
                population[j].cfitness = 0;
                population[j].lower[i] = lbound;
                population[j].upper[i] = ubound;

                fprintf(bounds,"%lf  %lf\n",&population[j].lower[i],&population[j].upper[i]);

                population[j].gene[i] = randval(population[j].lower[i],population[j].upper[i]);

                fprintf(popu,"%lf\n",population[j].gene[i]);
            }

    }
    fclose(infile);
    fclose(bounds);
    fclose(popu);
}

Guys thank you very much for you help actually i tryied to debug the code and change it a bit so that is prints the values on screen.. Unfortunatelly i am still getting zeros...!! This is the code and my input file with lower and upper bounds

void initialize(void)
{
    FILE *infile;
    FILE *test;
    int i,j;
    double lbound,ubound;

    test = fopen("test.txt","w");



    if((infile = fopen("gadata.txt","r"))== NULL)
    {
        fprintf(galog_tournament,"\n Cannot open input file\n");
        exit(1);
    }


    for(i = 0; i < NVARS; i++)
    {
        fscanf(infile, "%lf",&lbound);
        fscanf(infile, "%lf",&ubound);

         printf("%lf  %lf\n",lbound,ubound);


    for (j=0; j < POPSIZE; j++) {
        population[j].fitness = 0;
        population[j].rfitness = 0;
        population[j].cfitness = 0;
        population[j].lower[i] = lbound;
        population[j].upper[i] = ubound;
        population[j].gene[i] = randval(population[j].lower[i],
                                        population[j].upper[i]);






     }


  }

    fclose(infile);
    fclose(test);

}






   0.000000 0.000000
    0.000514    0.010514
    0.011307    0.021307
    0.021876    0.031876
    0.033994    0.043994
    0.043272    0.053272
    0.050229    0.060229
    0.053976    0.063976
    0.053803    0.063803
    0.046640    0.056640
    0.029907    0.039907
    0.017619    0.027619
    0.002316    0.012316
    -0.005428   0.004572
    -0.00683    0.00317
    -0.009743   0.000257
    -0.10582    -0.09582
    -0.026304   -0.016304
    -0.027064   -0.017064
    -0.025243   -0.015243
    -0.022386   -0.012386
    -0.019108   -0.009108
    -0.015788   -0.005788
    -0.012185   -0.002185
    -0.009452   0.000548
    -0.052133   0.002867
    -0.006128   0.003872
    0.000000    0.000000

Upvotes: 0

Views: 176

Answers (2)

user2123629
user2123629

Reputation: 11

You Probably dont want your code to run if fScanf is not reading anything and returning 0. why dont you change it to somthing like

while( 0 != fscanf(infile, "%lf",&ubound))
{
    if(0 == fscanf(infile, "%lf",&lbound) || i >= NVARS)      
       {
       return 0; 
       }
    else
       {
       printf("%lf  %lf\n",lbound,ubound);
       i++
       }
}

Then if it dosnt print anything you know your not scanning anything in with fscanf. Im guessing you dont want to keep filling your array with 0s if your scanf isnt reading anything and it will proably point you in the direction of the error

Upvotes: 0

user1944441
user1944441

Reputation:

Remove the &s from

fprintf(bounds,"%lf  %lf\n",&population[j].lower[i],&population[j].upper[i]);

so it becomes:

fprintf(bounds,"%lf  %lf\n",population[j].lower[i],population[j].upper[i]);

You want to print the values of your variable not the address of that variable.

Set your compiler to display warnings, in this case it would tell you about the problem.

Upvotes: 4

Related Questions