Reputation: 159
I am getting a floating point exception in my code but unable to sort it out. I dnt know what the problem is. Not so good at C++ program and just going through the formulas given. Please help me:
Program:
#include<iostream.h>
#include<conio.h>
#include <stdlib.h>
void main()
{
clrscr();
int a,b,c,d,ctr,j,Q=1,K=1 ;
float q0=0.7, p = 0.5 ;
int phe[3][3];
double dist[3][3] , mem[3][3],exp[3][3],eplt[3][3], rnd;
cout<<"enter the iterations, cities , ants ";
cin>>a>>b>>c;
for (int i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
dist[i][j]=(double)rand()/(double)RAND_MAX;
if (i==j)
dist[i][j]=0;
}
}
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
cout<< dist[i][j]<<"\t";
}
cout<<"\n";
}
cout<<"pheromone matrix "<<endl;
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
if (i==j)
phe[i][j]=0;
else
phe[i][j]=1;
}
}
for ( i=0;i<3;i++)
{
for ( j=0;j<3;j++)
{
cout<< phe[i][j]<<"\t";
}
cout<<"\n";
}
cout<< "after iteration "<<endl;
for (i=0;i<3;i++)
{
ctr=0;
for (int k=0;k<3;k++)
{
// mem[i][k]=(rand()%b)+1;
// cout<<"memory"<<mem[i][k]<<"\n";
rnd= (double)rand()/(double)RAND_MAX;
cout<<"hhhhhhh"<<rnd;
if (rnd<=q0)
{
cout<<"Exploitation\n";
eplt[i][ctr] =(p*phe[i][k])+(Q/K);
}
else
{
cout<<"EXPLORATION\n";
eplt[i][ctr]= phe[i][k]/dist[i][k];
}
ctr++;
}
}
for (i=0;i<3;i++)
{
for (int k=0;k<3;k++)
{
cout <<eplt[i][k]<<"\t";
}
cout<<"\n";
}
getch();
}
I am getting the error after first loop after "after iteration" completes. This is the output:
enter the iterations, cities , ants 3 4 4
0 0.003967 0.335154 0.033265 0 0.2172 0.536973 0.195776 0
pheromone matrix 0 1 1 1 0 1 1 1 0
after iteration hhhhhhh0.949919EXPLORATION Floating point error: Domain. Abnormal program termination
Upvotes: 3
Views: 1369
Reputation: 234795
I think this line (right after cout<<"EXPLORATION\n";
:
eplt[i][ctr]= phe[i][k]/dist[i][k];
is the problem. When i == k, then dist[i][k] == 0.0.
Upvotes: 3
Reputation: 998
At first glance: are you calling the line
eplt[i][ctr]= phe[i][k]/dist[i][k];
with i = 0 and k = 0 ? From the initialization, dist[0][0] = 0, and you would have a division by zero exception.
Upvotes: 5