Whai是这个计划的错误和纠正 [英] Whai is error of this programme and correction for it

查看:83
本文介绍了Whai是这个计划的错误和纠正的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<complex.h>
#define chrom_len 14
#define chromosome_number 20
typedefstructChrom             // creating the chrom structure
{
short int bit[chrom_len];
float fit;
float modulus;
int condition;
}chrom;
structchrom_ele
{
short int bit1[chrom_len/2];
int val1;
}chrom1;			   
void* init_pop(chrompopcurrent[chromosome_number]);
void *pickchroms(chrompopcurrent[chromosome_number]);
void *crossover(chrompopcurrent[chromosome_number]);
void *mutation(chrompopnext[chromosome_number]);
int x(chrom_elepopcurrent);
float y(int x1,int x2);
float modulus_calc(int x1,int x2);
void final_output(chrompopcurrent[chromosome_number]);
void final_top(chrompopcurrent[chromosome_number]);
float mod,min_vol;
void main()                                    // the main function
{
inti,j,pop_number,value,itr,z;
chrompopcurrent[chromosome_number];                   // we make 4 chromes of popcurrent
chrompopnext[chromosome_number];
float vol,SA,CF,SF;
clrscr();
printf("\n------------------****----Genetic Alogorithm----****----------------------------");                         // introduction to the program
printf("\n\t--------Fitness finction is f(x1,x2)=0.785*x1*x1*x2----------- ");
printf("\nEnter volume:");
scanf("%f",&vol);
printf("\nEnter Surface area:");
scanf("%f",&SA);
printf("\nEnter Safety Factor(SF Generaly 1.2)");
scanf("%f",&SF);
mod=SF*(vol/SA);
printf("\nMod is :%f",mod);
printf("\nEnter value of Contraction factor(CF in %) :");
scanf("%f",&CF);
min_vol=(vol*CF)/100;
printf("\nMinimum volume is %f",min_vol);
printf("\nPlease Enter the number of iteration :");
scanf("%d",&itr);
printf("\nStep 1: Initial Population:");
init_pop(popcurrent);
for(z=0;z<itr;z++)   //repeat the procedure itr times
   {
clrscr();
printf("\n\n--------------------Iteration Number is-->%d-----------------------------",(z+1));
for(i=0;i<chromosome_number;i++)
     {
popnext[i]=popcurrent[i];
     }
pickchroms(popnext);  // for picking best chrom
crossover(popnext);   // for making crossover
mutation(popnext);   // mutate the chromosome
for(i=0;i<chromosome_number;i++)
     {
popcurrent[i]=popnext[i];
     }
flushall();                                         // flush the input buffer
   }
printf("\nPress any key to end ! ");
final_output(popcurrent);
getche();                                        // wait for a character from the keyboard to end
}                                                      //end of main
int x(chrom_elepopcurrent)        //x function that evaluate the value of a given chrom
{
int z=0,k;
int temp=0;
for(k=0;k<(chrom_len/2);k++)
   {
if(k==0)
    {
temp=1;
    }
else
    {
temp=pow(2,k);
    }
    z+=(popcurrent.bit1[k]*temp);
   }
z=(popcurrent.bit[0]*1)+(popcurrent.bit[1]*2)+(popcurrent.bit[2]*4)+(popcurrent.bit[3]*8)+(popcurrent.bit[4]*16);
return(z);                     //return the value of z
 }                                   // end x function
float y(int x1,int x2)          // the y function that we look for it's maximum value takes x value
{
float y;
   y=(0.785*x1*x1*x2);            
return(y);
}                              // end of y function
float modulus_calc(int x1,int x2)
{
float mod;
mod=(x1*x2)/(2*x1+4*x2);
return (mod);
}
void *init_pop(chrompopcurrent[chromosome_number])
{
inti,j,random;
chrom_ele pop1[chromosome_number];
chrom_ele pop2[chromosome_number];
for(j=0;j<chromosome_number;j++)                       // loop of j to choose chromes from [0] to [3]
    {
	for(i=0;i<chrom_len;i++)          // loop of i to choose the gen of the chrom from  [0] to [5]
	{
	random=rand();               // creating random value
	random=(random%2);        // make the random value o or 1 only
	popcurrent[j].bit[i]=random;  // initialising the bit[i] of chrom[j] with random
	}   // end of for(i)
       }    // end of for(j)
for(i=0;i<chromosome_number;i++)
      {
for(j=chrom_len/2;j>=0;j--)
       {
	pop1[i].bit1[j]=popcurrent[i].bit[j];
       }
      }
int





我尝试了什么:



我尝试运行程序但由于错误而无法运行



What I have tried:

I try to run programme but it can't run due to error

推荐答案

有很多这个程序的错误。它不会按原样编译。此外,您的代码已丢失其格式,似乎被剪掉。粘贴它时选择C ++代码选项。它没有格式化时很难阅读。



我看到的第一个问题是:

There are LOTS of errors with this program. It will not compile as it is. Also, your code has lost its formatting and appears to be clipped off. When pasting it in select the C++ code option. It is very difficult to read when it has no formatting.

The first problem I see is this :
chrompopcurrent[chromosome_number];



问题是变量没有类型。它似乎是一个数组但是什么类型?这导致了许多其他问题。函数原型似乎将该变量作为参数,但这不是如何编写原型。



看来这里有一个缺少的空间而且它是一个巨大的区别。这可能是由于缺乏格式化但我不知道。



根据这个假设,它应该声明为:


The problem is that variable has no type. It appears to be an array but of what type? That leads to a bunch of other problems. The function prototypes appear to take that variable as an argument but that is not how to write a prototype.

It appears that there is a missing space there and it makes a HUGE difference. This could be a result of a lack of formatting but I don't know.

Under that assumption, it should be declared as :

chrom popcurrent[chromosome_number];



,原型应声明为:


and the prototypes should be declared as :

void* init_pop( chrom popcurrent[] );



请注意,原型或函数定义中不需要数组的大小。该函数是否真的返回指向未知类型的指针?我不知道因为我看不到那段代码。除非你不能这样做,否则最好返回指向已知类型的指针。


Note that the size of the array is not needed in the prototype or in the function definition. Does that function really return a pointer to an unknown type? I don't know because I can't see that code. It's always best to return a pointer to a known type unless you just can't do it.


这篇关于Whai是这个计划的错误和纠正的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆