When floating point exception happens??

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
hamidreza_buddy
New poster
Posts: 3
Joined: Mon Mar 14, 2005 10:46 pm

When floating point exception happens??

Post by hamidreza_buddy » Wed Mar 30, 2005 2:43 pm

Your program has died with signal 8 (SIGFPE). Meaning:

Floating point exception

Before crash, it ran during 0.002 seconds.
---------------------
the code executes with the sample inputs given.
but when i submit it it returns the above exception.
would someone tell me when this happens plz?
thnx

hamidreza_buddy
New poster
Posts: 3
Joined: Mon Mar 14, 2005 10:46 pm

code

Post by hamidreza_buddy » Wed Mar 30, 2005 2:46 pm

Code: Select all

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

class file_name
{
public:
	char name[60];
	void init();
	void check();
	int get_chk();
	int length();
private:	
	int chk;
};
void file_name::init(){chk=0;}	
int file_name::get_chk(){return chk;}
void file_name::check(){chk=1;}
int file_name::length()
{
	int i;
	for(i=0;name[i]!='\0';i++){}
	return i;
}

int longest(file_name *x,int n);
int column(int,int);
file_name* sort(file_name*,int n);
int str(char*,char*);
int row(int,int);

int main()
{
	int l,col,r;
	file_name *x,*back;
	int n;
	#ifndef ONLINE_JUDGE
  close (0); open ("myprog.in", O_RDONLY);
  close (1); open ("myprog.out", O_WRONLY | O_CREAT, 0600);
#endif

	while(scanf("%d",&n)==1)
	{
		x=(file_name*)malloc(sizeof(file_name)*n);
		for(int i=0;i<n;i++)
		{
			x[i].init();
			scanf("%s",x[i].name);
		}
		l=longest(x,n);
		col=column(l,n);
		for(int j=0;j<60;j++){printf("-");}
		printf("\n");
		back=sort(x,n);
		r=row(n,col);
		for(int j=0;j<r;j++)
		{
			for(int i=0;i<n;i+=r)
			{
				for(int t=0;t<l+2;t++)
				{
					if(i+j<n && t<back[i+j].length()){printf("%c",back[i+j].name[t]);}
					else{printf(" ");}
				}
			}
			printf("\n");
		}
		
		free(x);
	}
	return 0;
}

int longest(file_name *x,int n)
{
	int l=0;
	for(int i=0;i<n;i++)
	{
		if(x[i].length()>l){l=x[i].length();}
	}
	return l;
}

int column(int l,int n)
{
	int x;
	x=(60-l)/(l+2);
	if((60-l)%(l+2)==0){return x;}
	else
	{
		
		return x+1;
	}
}

file_name* sort(file_name *x,int n)
{
	file_name *back,tmp;
	int m;
	back=(file_name*)malloc(sizeof(file_name)*n);
	for(int t=0;t<60;t++){tmp.name[t]=120;}
	for(int j=0;j<n;j++)
	{
		for(int t=0;t<60;t++){tmp.name[t]=127;}
		m=-1;
		for(int i=0;i<n;i++)
		{	
			if(str(x[i].name,tmp.name)==1 && x[i].get_chk()==0)
			{
				x[i].check();
				tmp=x[i];
				if(m!=-1){x[m].init();}
				m=i;
			}
			
			else{continue;}
		}
		back[j]=tmp;
	}
return back;
}			
				


int str(char *c1,char *c2)
{
	for(int i=0;i<60;i++)
	{
		if((c1[i]==c2[i])&& (c1[i+1]=='\0' && c2[i+1]=='\0')){return 0;}
		if(c1[i]==c2[i]){continue;}
		if(c1[i]<c2[i]){return 1;}
		if(c1[i]>c2[i]){return -1;}
	}
}

int row(int n ,int col)
{
	if(n%col==0){return n/col;}
	else
	{
		return n/col+1;
	}
}






[/code]

User avatar
shamim
A great helper
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Bozeman, Montana, USA

Post by shamim » Sat Apr 02, 2005 8:37 am

very often you get this RTE due to divide by zero error.

Post Reply

Return to “C++”