Question
Identify the errors in the following C code and fix it so the program can run. #include #include int calculate(bool* genetic); int main() { int
Identify the errors in the following C code and fix it so the program can run.
#include
#include
int calculate(bool* genetic);
int main()
{
int i,j,g; //counters
int population=100;
bool genetic[10][7]; //population
//initializing population
for(i=0;i
{
for(j=0;j<7;j++)
{
//randomize the genetic
genetic[i][j]=rand()%2;
}
}
for(g=0;g<100;g++)
{
printf("generation %d ",g);
//Evaluation
int best=0;
for(i=1;i
{
if(calculate(genetic[best])
best=i;
}
//Reproduction
for(i=0;i
{
if(i!=best)
{
for(j=0;j<7;j++)
{
if(rand()%2)
genetic[i][j]=genetic[best][j];
else
genetic[i][j]=genetic[i][j];
//mutation
if(rand()%100<4)
genetic[i][j]=rand()%2;
}
}
}
printf("best calculate %d ",calculate(genetic[best]));
}
getchar();
return 0;
}
int calculate(bool* genetic)
{
return ( -genetic[0] + genetic[1] + genetic[2]
-genetic[3] + genetic[4] - genetic[5]
-genetic[6] );
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started