Small bug in code

hello all,

I believe there is a small bug in my code because of this the segA and segB sizes when I call from main() are incremented by 1 due to which my output is going wrong. Below is my code.

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

int total_cells,total_nets;
int cost,tempe;
int **conn;
int *segA,*segB;
int *cutset;
int seg();
int k=1;
int find_cutset();
int conn1();
int swap();
int flag =0;
int temperature();
int min_cutset();
int accept_move();

int main()
{
int i;
conn1();
cout << total_nets << "\t" << total_cells << "\n" ;
seg();
cout<<"in main"<<endl;
for(i=1;i<=total_cells/2+1;i++)
{
cout << segA[i] << "\t" << segB[i] << "\t"<<endl;
}

temperature();
min_cutset();
return 0;
}

int conn1()
{
int x,y;
int i=0,j=0;
ifstream inFile;
char inputFileName[] = "trial.txt";
inFile.open(inputFileName);

if (!inFile)
{
cout << "Can't open input file named " << inputFileName << endl;
return 0;
}

inFile >> x >> y;
total_cells = x;
total_nets = y;
cout << "total_cells =" << total_cells << "\t"<<"total_nets ="<<total_nets<<endl;
conn = new int*[total_cells];
for (int i = 1; i <= total_cells; ++i)
conn[i] = new int[total_cells];
for(i=1;i<=total_cells;i++)
{
for(j=1;j<=total_cells;j++)
{
conn[i][j] = 0;
}
}
while (inFile >> x >> y)
{
conn[x][y] = conn[x][y] + 1;
}
for(i=1;i<=total_cells;i++)
{
for(j=1;j<=total_cells;j++)
{
//cout << "conn[" << i << "][" << j << "]: ";
//cout << conn[i][j]<< endl;
}
}
inFile.close();
return 0;
}

int seg()
{
int i;
segA = new int[total_cells/2];
segB = new int[total_cells/2];
cout<<"in segmentation"<<endl;
for(i=1;i<total_cells/2 +1;i++)
{
segA[i] = i;
segB[i] = i + total_cells/2;
cout << segA[i] << "\t" << segB[i] << "\t"<<endl;
}
return 0;
}

int find_cutset()
{
int x , y , i , j;
cutset= new int[total_nets*total_nets];
cutset[0] = 0;
cutset[1] = 0;
for(i=1;i<total_cells/2 +1;i++)
{
x = segA[i];

for(j=1;j<total_cells/2 +1;j++)
{
y = segB[j];

//cout << "conn[" << x << "][" << y << "]: " << conn[x][y] << "\n";
cutset[k] = cutset[k] + conn[x][y] + conn[y][x];
}
}
cout << "\n cutset is : " << cutset[k] << endl;
k++;

return 0;
}

int swap()
{
int i,j;
int temp;
if(flag)
{
i=rand() % total_cells/2 + 1;
j=rand() % total_cells/2 + 1;
cout<< "values of i and j are" << i << "\t" << j <<endl;
temp=segA[i];
segA[i]=segB[j];
segB[j]=temp;
}
flag =1;
find_cutset();
return 0;
}

int temperature()
{
int t=0,i=0;
int next,curr=0;
tempe=100;
swap();
curr = cutset[1];
cout << "curr is " << curr << endl;
while(tempe>10)
{
for(i=0;i<=10;i++)
{
swap();
next = cutset[k-1];
cost= next - curr;
t=accept_move();
cout << "value of t" << "\t" <<t << endl;
if(t)
{
curr = next;
cout<<"curr is" << curr<< endl;
}
}
tempe= tempe-tempe*0.70;
}
return 0;
}

int accept_move()
{
float boltz =0, random=0, expo=0, h=0, z=0;
if(cost<0)
{
return 1;
}
else
{
int a=-23;
h= exp(a);
z=1.38*h;
expo= -cost/z*tempe;
boltz=exp(expo);
random=rand() % 1 + 0;
if(random < boltz)
{
return 1;
}
else
{
return 0;
}
}
}

int min_cutset()
{
int k1=0;int i=0;
int min_value = cutset[1];
for(k1=1;k1<=k;k1++)
{
if( cutset[k1] < min_value)
{
min_value = cutset[k1];
}
}
cout<<"minimum value of cutset is "<<min_value<<endl;
for(i=1;i<=total_cells/2+1;i++)
{
cout << segA[i] << "\t" << segB[i] << "\t"<<endl;
}
return 0;
}

output file:
total_cells =6 total_nets =8
8 6(I do not know from where this is getting printed)
in segmentation
1 4
2 5
3 6
in main
1 4
2 5
3 6
27062272 0








Topic archived. No new replies allowed.