not willing to print the same number
1 2 3
|
hi
I am trying to print a 2_D array.But not willing to print the same number once at a time in each row.But program prints it.Ohhhh...[code]
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
#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<job;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];
}
}
for(p=0;p<pop;p++)
{
for(i=0;i<job;i++)
{
cout<<alpop[p][i]<<"\t";
outfile<<alpop[p][i]<<"\t";
}
cout<<endl;
outfile<<endl;
}
delete[] chrom;
delete[] alpop;
outfile.close();
return 0;
}
|
[/code]
Last edited on
Topic archived. No new replies allowed.