i have sorting program, it works well when coded in simple way but when used recursion it give the floating exception (core dumped), it have tries to debug and it show error in the division stage, it tires to do typecasting also, but does not work out for recursion

cin >>n;
cout <<"enter the number of elements:"<<endl;
for(i=0;i<n;i++)
cin >>a[i];
i=0;lrg=a[i];

while(i<n)
{
 if(a[i]>lrg)
   lrg=a[i];
   i++;
}

digcnt=0;
while(lrg>0)
{
 digcnt=digcnt+1;
lrg=(int)lrg/10;          //probably error is here, 
}

i=1;
divsr=1;


rad(divsr,digcnt,n,a);

my program takes input , when input is complete it gives floating exception (core dumped) error and program terminates, what is the error

No, line 18 is not your problem.

Perhaps you need to better identify where the program crashes - put some output statements that tell you when you start each section, step through it in a debugger, ....

Posting the whole code so someone could attempt to recreate the problem would help.

ok this is my whole program, i inserted a couple of comments cout statements but , the turboc displays that statements but the g++ compiler does not , so
i will post all the program

#include<iostream>
#include<cstdlib>

using namespace std;
void rad(int divsr,int digcnt,int n,int a[]);
int main()
{
int a[20],n,lrg,i;
int digcnt,divsr;

cout <<"*****************************************"<< endl;
cout <<"how many elements(MAX 20):"<<endl;
cin >>n;
cout <<"enter the number of elements:"<<endl;
for(i=0;i<n;i++)
cin >>a[i];
i=0;lrg=a[i];

while(i<n)
{
 if(a[i]>lrg)
   lrg=a[i];
   i++;
}
 cout <<"I AM HERE";
digcnt=0;
while(lrg>0)
{
 cout.flush();
digcnt=digcnt+1;
lrg=(int)lrg/10;
}

i=1;
divsr=1;


rad(divsr,digcnt,n,a);

cout <<"THE SORTED ELEMENTS ......:"<<endl;
for(i=0;i<n;i++)
 cout <<"\t" <<a[i];

}

void rad(int divsr,int digcnt, int n, int a[])
{
int j,i,r,k,p;
int bukt[10][10];
int buktcnt[10];

while(i<=digcnt)
{
 j=0;
 while(j<10)
 {
  buktcnt[j]=0;
  j++;
 }
j=0;
 while(j<n)
 {
  r=(a[j]/divsr)%10;
  bukt[r][buktcnt[r]]=a[j];
  buktcnt[r]++;
  j++;
 }

j=0;p=0;
while(j<10)
{
 k=0;
while(k<buktcnt[j])
  {
   a[p]=bukt[j][k];
   p++;
   k++;
  }
 j++;
}
i++;
divsr=divsr*10;
}
rad(divsr,digcnt,n,a);
}

the proogram generate the error after taking input in the array, the turobc dispaly the message, I AM HERE, but g++ does not, display, so what is the error , if this program is done without using function (recursion) no error is generated, output is obtained

Two problems come up quickly.

In rad( ), you use variable 'i' without initializing it, so results will be wrong.

For a recursive function to work, you must have a base case - at what condition does it stop calling itself? I don't see any such here, so it keeps going and going and...just like the Eveready Bunny. Doing so, the divsr keeps increasing and increasing, till it wraps and becomes 0 (zero), thus giving a divide by 0 error.

a[20] can give you array bound violation esspecially where you are using recursion.

that does not works out , variable i has already been initialize in both the function and the array size, increased but not sloved.

You need a smarter compiler. Here's the warning I get with VC++ 2005 Express

\dvlp\test12\test12\test12.cpp(89) : warning C4717: 'rad' : recursive on all control paths, function will cause runtime stack overflow

The reason is that the last line of the rad() function make a recursive call without bounds. That is, there is nothing to stop the recursion. And every recursive call gobbles up (110 * sizeof(int)) +8 (more or less) number of bytes on the stack, so eventually that will consume all the stack space.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.