structure////very important

#include<iostream>
#include<ctime>
#include <cstdlib>
#include<fstream>
using namespace std;


struct mc2
{
public:
int *seq,go;
double fitnes;
};

int main(){
mc2 *chrom,temp;
int job;
cout<<"enter the no. of jobs";
cin>>job;
chrom=new mc2[job];
int pop,rand_index,p,i;
cout<<"enter population size";
cin>>pop;
cout<<"enter no of jobs";
ofstream outfile("C:\\fuad.OUT");
outfile<<"population size:"<<pop<<endl;
outfile<<"no of jobs:"<<job<<endl;
srand(unsigned (time(0)));
for(i=0;i<job;i++)
{
chrom[i].seq=chrom[i+1].seq;//problem creats here
}
for(i=0;i<job;i++){
cout<<chrom[i].seq<<"\t";
outfile<<chrom[i].seq<<"\t";
}
outfile<<endl;

cout<<endl;
for(p=1;p<=pop;p++){
for(i=0;i<job;i++)
{
rand_index=(rand()%(job-1))+1;//swapping operation
temp=chrom[rand_index];
chrom[rand_index].seq=chrom[i].seq;
chrom[i].seq=temp.seq;
}

for(i=0;i<job;i++)
{
cout<<chrom[i].go<<"\t";
outfile<<chrom[i].seq<<"\t";
}
outfile<<endl;
cout<<endl;
/*for(i=0;i<job;i++)
{
cout<<chrom[rand_index]<<"\t"<<endl;
}*/
cout<<endl;
}
outfile.close();

return 0;

}


















Last edited on
The problem with this line: chrom[i].seq=i.seq+1; is i.seq+1. i is just an integer variable, and not the struct mc2. Therefore it doesn't have .seq or any of mc2's variables. I'm not exactly sure what you want to do, but maybe chrom[i].seq=chrom[i].seq+1; or chrom[i].seq=chrom[i+1].seq; will fix your problem?
actually I want to get the similar output of the following program(as bellow) using struct and dynamic memory allocation .But the above program creates garbage value :(

#include <iostream>
#include<ctime>
#include <cstdlib>
#include<fstream>
using namespace std;

int main()
{
int i,pop,rand_index,p;
int*chrom,temp,job;
cout<<"enter population size";
cin>>pop;
cout<<"enter no of jobs";
cin>>job;//enter chromosom size
chrom=new int[job];//dynamic memory allocation
ofstream outfile("C:\\fuad.OUT");
outfile<<"population size:"<<pop<<endl;
outfile<<"no of jobs:"<<job<<endl;
srand(unsigned (time(0)));
for(i=0;i<job;i++)
{
chrom[i]=i+1;
}
for(i=0;i<job;i++){
cout<<chrom[i]<<"\t";
outfile<<chrom[i]<<"\t";
}
outfile<<endl;

cout<<endl;
for(p=1;p<=pop;p++){
for(i=0;i<job;i++)
{
rand_index=(rand()%(job-1))+1;//swapping operation
temp=chrom[rand_index];
chrom[rand_index]=chrom[i];
chrom[i]=temp;
}

for(i=0;i<job;i++)
{
cout<<chrom[i]<<"\t";
outfile<<chrom[i]<<"\t";
}
outfile<<endl;
cout<<endl;
/*for(i=0;i<job;i++)
{
cout<<chrom[rand_index]<<"\t"<<endl;
}*/
cout<<endl;
}
outfile.close();

return 0;

}




Topic archived. No new replies allowed.