Getting a Floating point exception ?!?!?!

Here's my code :-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<cstdio>
int main() {
int n,i,ul,a,x,y,z,s=0;  //n is number of tiles, i is counter variable , ul is upper limit & a,x,y,z are temp variables while s is sum variable for counting all possibilites
scanf("%d",&n);
ul=(int)n/2;
for (i=1;i<=ul;i++) {
 x=1;y=1;z=1;
  for (a=(n-i);a>1;a--) {if ((n-i)==1) break;x=x*a;}   //calculating (n-i) factorial
  for (a=i;a>1;a--) {if ((i)==1) break;y=y*a;}  //calculating i factorial
  for (a=(n-(2*i));a>=1;a--) {if ((n-(2*i))==1) break;z=(int)z*a;}  //calculating (n-(2*i)) factorial
 s+=(int)x/(y*z);
}
printf("%d",(s+1));
}


I am again getting a Floating Point exception , i know what is this error , but cant remove it from my code, can you pls help ???

For further reference here's the Question to which it is solution :-
To help Lavanya learn all about binary numbers and binary sequences, her father has bought her a collection of square tiles, each of which has either a 0 or a 1 written on it. Her brother Nikhil has played a rather nasty prank. He has glued together pairs of tiles with 0 written on them. Lavanya now has square tiles with 1 on them and rectangular tiles with two 0's on them, made up of two square tiles with 0 stuck together). Thus, she can no longer make all possible binary sequences using these tiles.

To amuse herself, Lavanya has decided to pick a number N and try and construct as many binary sequences of length N as possible using her collection of tiles. For example if N = 1, she can only make the sequence 1. For N=2, she can make 11 and 00. For N=4, there are 5 possiblities: 0011, 0000, 1001, 1100 and 1111.

Lavanya would like you to write a program to compute the number of arrangements possible with N tiles so that she can verify that she has generated all of them. Since she cannot count beyond 15746, it is sufficient to report this number modulo 15746.

Input format

A single line with a single integer N.

Output format

A single integer indicating the number of binary sequences of length N, modulo 15746, that Lavanya can make using her tiles.

Test data

You may assume that N ≤ 1000000.

Example

Here is a sample input and output corresponding to the example discussed above.

Sample input

4
Sample output

5
On which line of code does it happen?

If I had to guess, I'd guess it's here:
s+=(int)x/(y*z);

with (y*z) set to zero (i.e. y or z or both are zero).

You cannot divide by zero.
It is not given on which line it happens .

Yeah you pointed out correct possibility and i edited my code accordingly but still it gives an "Floating Point Error" , here's edited code edited on Line 12 :-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<cstdio>
int main() {
int n,i,ul,a,x,y,z,s=0;
scanf("%d",&n);
ul=(int)n/2;
for (i=1;i<=ul;i++) {
 x=1;y=1;z=1;
  for (a=(n-i);a>1;a--) {if ((n-i)==1) break;x=x*a;}
  for (a=i;a>1;a--) {if ((i)==1) break;y=y*a;}
  for (a=(n-(2*i));a>=1;a--) {if ((n-(2*i))==1) break;z=(int)z*a;}
 if ((y*z)!=0) s+=(int)x/(y*z); //condition here is to avoid division by 0
}
printf("%d",(s+1));
}
With what input value? I tried 4, 5 and 6 and there was no FPE thrown. Did you definitely recompile the changed code?
Last edited on
Yes , i obviously did compile it .

Some Input values for which it gives FPE are :-
1)45757 (Ans should be 12593)
2)510741 (Ans should be 2837)
3)309458 (Ans should be 12820)

And so on .
Topic archived. No new replies allowed.