problem in debug

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

int main()
{
int **alpop;
int i,rand_index,p,pop;
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

alpop=new int *[job];
for(int lp=0;lp<pop;lp++)
alpop[lp]=new int[pop];

ofstream outfile("C:\\fuad2.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=0;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;
alpop[p][i]=chrom[i]; //I donn't understand why the program creats error in debug when coming to that line
}

}


for(p=1;p<=pop;p++)
{
for(i=0;i<job;i++)
{
cout<<alpop[p][i]<<"t";
outfile<<alpop[p][i]<<" ";
}
cout<<endl;
outfile<<endl;
}



delete[] chrom;
delete[] alpop;
outfile.close();

return 0;

}
[b]
[/b]
Last edited on
Your code is fine.... Is there something I should be looking for?

alpop[p][i]=chrom[i]; //I donn't understand why the program creats error in debug when coming to that line
This generates the first level: alpop=new int *[job];

And this the second:
1
2
for(int lp=0;lp<pop;lp++)
alpop[lp]=new int[pop];


This alpop[p][i]=chrom[i]; uses pop as the first level and job as the second
It's the wrong oder of levels.

By the way: use [code]Your code[/code]
Topic archived. No new replies allowed.