Why does my code for SPOJ GCD2 error on SPOJ? - c++

Following is code for SPOJ GCD2. It's running well on my machine and Ideone, but getting runtime error (SIGFPE) on SPOJ. I have checked all the test cases also available at spojtoolkit.com.
I am unable to figure out why this code is showing runtime error (SIGFPE) on spoj. SIGFPE means Erroneous arithmetic operation.
Why is this code showing runtime error on SPOJ?
#include <bits/stdc++.h>
using namespace std;
int gcd(int x,int a)
{
if(a==0)
return x;
else
return gcd(a, x%a);
}
int getmod(string b,int a)
{
int n=b.size();
int d;
d= (b[0]-'0') % a;
for(int i=1; i!=n; i++)
{
d=d*10;
d=d + (b[i]-'0');
d= d % a;
}
return d;
}
int main()
{
int tc;
cin >> tc;
int a;
string b;
while(tc--)
{
cin >>a>>b;
int x=getmod(b,a);
cout << gcd(x,a)<<endl;
}
return 0;
}

int getmod(string b,int a)
{
int n=b.size();
int d;
d= (b[0]-'0') % a;
If a = 0, this will error, because % 0 is like dividing by zero. a can be zero according to the problem statement.
The error covers division by zero:
The SIGFPE signal reports a fatal arithmetic error. Although the name is derived from “floating-point exception”, this signal actually covers all arithmetic errors, including division by zero and overflow. If a program stores integer data in a location which is then used in a floating-point operation, this often causes an “invalid operation” exception, because the processor cannot recognize the data as a floating-point number.
You can fix it by checking if a == 0 and simply returning b as the answer in that case. Else call your current function as is.

Related

Illegal instruction in while loop c++

I've been assigned to write a program that simplifies a rational number. What I want to do is compute the gcd and then divide the numbers by gcd. But the program returns a very strange error.
The code:
void read_rational(int& num, int& den) {
char bar;
if (cin >> num >> bar >> den) {
cout << "hi";
int a = num;
int b = den;
while (b != 0) {
int r = a%b;
a = b;
b = r;
}
num /= b;
den /= b;
}
}
INPUT: 10/2 OUTPUT: Illegal instruction (core dumped)
INPUT: 90/8 OUTPUT: Illegal instruction (core dumped)
I've tried commenting out some bits of the script. The program seems to crash only when the while loop is present. But I can't see what's wrong with it.
Indeed, the issue is the while loop. After it finishes, b is actually 0, so the divisions after raise these errors.
I think what you want is a instead of b.

Recursive call getting stuck

#include <bits/stdc++.h>
using namespace std;
int gcd(long long int a, long long int b){
if(a||b==0){
return 0;
}
else if(b==a){
return a;
}
else if(a>b){
return gcd(a-b,b);
}
else{
return gcd(a,b-a);
}
}
int lcm(long long int a,long long int b){
return a*b/(gcd(a,b));
}
int main(){
long long int answer=1;
for (int i = 2; i<=20; i++) {
answer=lcm(i,answer);
cout<<answer;
}
cout<<answer;
return 0;
}
i wrote this code for problem 5 in project euler. however the output screen is showing nothing and is getting hanged. i put a few debugging cout statements and i understood that in the main function the it is entering the loop but it is not continuing the excution after the call for lcm.
the program is to find the lcm of numbers from 1 to 20. i used the formula llcm= a*b/gcd(a,b). where in gcd also i used the recursive euclidian algorithm. i am not able to trace out the reason for this bug . could anyone help pls.
also if there any suggestions regarding my coding style (indentation, type casting, variable names, algorithm or anything) please point it out. i am beginner so i do not know much regarding c++ and programming styles.
Your program is becoming stuck because of this line:
if (a || b == 0) {
The == operator has higher precedence than ||, so the condition is in fact the same as:
if (a || (b == 0)) {
Which in C(++) is the same as:
if ((a != 0) || (b == 0)) {
That is, if a is non-zero OR b is zero. a will be non-zero straight away, hence your program will always try to divide by zero, which causes problems. I am not sure where you found this version of the algorithm, a cursory search results in a much simpler variant:
int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, (a % b));
}
}
As for the second part of your question, there are many little (stylistic) issues in your code that I would change. Inconsistent spacing, unnecessary use of long long int (an int would do just fine here) … But for these, I recommend the codereview StackExchange.

Runtime Error - SIGFPE

I HAVE THE FOLLOWING CODE for hackerearth competetion and WRITTEN IN C++ (g++ 4.8.4)
it is giving SIGFPE on runtime
I am just done with it
plz tell how tofix it
#include<iostream>
using namespace std;
int factorial(int n);
int main()
{
int n , k ,totitem , totways=0 , har1,har2, ansh=1;
int res;
cin>>n>>k;
totitem = (n/k);
ansh=factorial(n);
if(totitem>0)
for(int i=0;i<=totitem*k;i+=k)
{
har1=factorial(i);
har2=factorial(n-i);
totways+=(ansh/(har1*har2));
}
cout<<totways;
return 0;
}
int factorial(int n)
{
if(n>1)
return n*factorial(n-1);
else
// if(n==0 || n==1)
return 1;
}
It is usually division by zero error.
There are two divide statements in your code.
1:
totitem = (n/k);
where you don't sanitise your input.
2:
har1=factorial(i);
har2=factorial(n-i);
totways+=(ansh/(har1*har2));
And this will fail if any of the har parameters equals to zero.
The most probable cause of the problem is that you are using 32-bit signed integers (int) to do factorial calculations, which are limited to factorial of 12! Trying to do factorial with larger numbers will cause overflow and thus incorrect result and eventually a zero value, that caused your runtime error.

Keep getting 1.#INF as my output

#include <iostream>
using namespace std;
double calc(int a, int b);
int main()
{
int n1, n2;
cout << "Enter a number for a: ";
cin >> n1;
cout << "Enter a number for b: ";
cin >> n2;
cout << calc(n1, n2) << endl;
system("PAUSE");
return 0;
}
double calc(int a, int b)
{
double s;
s = (a) / ((sqrt(a / b)));
return s;
}
This program is meant to check whether the two integers are greater than zero. If it is it will calcualte the formula. Otherwise if one of the integers is zero or less than zero it will not return anything and exit the program.
My question here is that no matter what I input for a and b, i keep getting 1.#INF as the output and I have no idea why. I've checked the formula in a seperate program and it worked fine.
Any ideas?
Here, you are operating with int numbers:
s = (a) / ((sqrt(a / b)));
If a is less then b, then a/b (both are integers, remember, so the fractional part of the result will simply be lost) will be equal to 0, which leads to division by 0. You need to cast one of the numbers to double:
s = (a) / ((sqrt(static_cast<double>(a) / b)));
sqrt takes and returns a double. When you call it with integer arguments it will be converted in a double, and will thus get the value of infinity.
change your function signature to:
double calc(double a, double b);
and declare n1 and n2 as double.
You say that the function will exit the program when one of the integers are 0 or less, but where?
Try to check them like this:
Additionally, you should have a check whether a is greater than b
double calc(int a, int b)
{
double s;
if(a <= 0) exit(-1);
if(b <= 0) exit(-1);
if(a < b) exit(-1);
s = (a) / ((sqrt(a / b)));
return s;
}
You are having problems with infinity. For it use isinf. Here is some sample usage:
#include <stdio.h> /* printf */
#include <math.h> /* isinf, sqrt */
int main()
{
printf ("isinf(-1.0/0.0) : %d\n",isinf(-1.0/0.0));
printf ("isinf(sqrt(-1.0)): %d\n",isinf(sqrt(-1.0)));
return 0;
}
output:
isinf(-1.0/0.0) : 1 isinf(sqrt(-1.0): 0

A strange runtime error in C++

#include <iostream>
#include <string.h>
using namespace std;
int testMul(int a, int b)
{
return !a || a * b / a == b;
}
int main()
{
int a, b;
while(cin >> a >> b) cout << testMul(a, b) << endl;
return 0;
}
When the input is
-1 -2147483648
, there is a runtime error.Divided by 0?
I think it is amazing.
By the way, has anyone concluded a way that can judge if there is a overflow in multiplication?
When you multiply -1 and -2147483648 you get signed overflow, which is an undefined behaviour in C++, the result can be any, up to your computer catching fire. So getting division by zero is actually not that bad of an outcome.