why run time error ! SIGFPE? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
4 of the samples shows ok.. Others are not running !
Problem Link: https://www.hackerearth.com/challenge/competitive/programming-indiahacks-2017/algorithm/hacker-with-prime-bebe28ac/
#include <iostream>
using namespace std;
int main()
{
long N,A[100000],Q,X[10000],R,p;
cin>>N>>Q;
for (int i=0;i<N;i++){
cin>>A[i];
}
for (int i=0;i<Q;i++){
cin>>X[i];
}
for (int i=0;i<Q;i++){
R=0;
bool isprime=false;
for (int j=0;j<N;j++){
R = (X[i]/A[j])*A[j];
if (R==X[i])
{
for (int k=2;k<=R/2;k++)
{
if(R%k==0){
R=0;
};
};
if(R==0){
cout<<"YES"<<endl;
isprime=true;
break;
}
else {
isprime=false;
}
}
}
if(isprime==false){
cout<<"NO"<<endl;
}
}
return 0;
}

My psychic powers suggest you have input that will trigger your code to attempt to divide by zero.
For example:
R = (X[i]/A[j])*A[j];
if(R%k==0){
If A[j] is zero in the first line, or if k is zero in the second line... that might be the cause....

Related

I'm getting Runtime Error in a problem from Code Forces [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Here is the problem: https://codeforces.com/contest/1256/problem/D .I'm getting runtime error("out of bound") in test case 15 here is my submission: https://codeforces.com/contest/1256/submission/84865113
Where am I doing wrong?
my approach:
#include <iostream>
using namespace std;
int main() {
int t;
cin>>t;
while (t--){
int n,k;
cin>>n>>k;
string num;
cin>>num;
int start = 0;
for(int i=0;i<n;i++){
if(num[i]=='0' && k>0){
if((k-abs(i-start))>=0){
swap(num[start],num[i]);
k = k - abs(i-start);
start++;
}
else{
swap(num[i],num[i-k]);
k = 0;
}
}
}
cout<<num<<"\n";
}
return 0;
}
the range of k is out of int. You should use long long.
9428683473 is out of 2147483647(max value for type int), so you should not use int. And the var i and start should change too.

Filling vector with primes in a certain way [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I was asked to fill a given vector with prime numbers, I can't use any other vectors, arrays or collections. I've come up with something like this, but it doesn't work properly and I can't figure out why.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool isPrime(int n){
if (n<=1){return false;}
for (int i = 2; i < n; i++){
if (n % i == 0){
return false;
}
}
return true;}
int fillWithPrimesVec(vector<int>& vec){
for (int i = 0; i < vec.size(); i++){
for (int j = 0; j<INT_MAX; j++){
if (isPrime(j)){
vec.push_back(j);}
else{continue;}
}
}
return vec[0];
}
int main(){
int c[15];
size_t szc = sizeof(c)/sizeof(*c);
vector<int> d(szc,0);
cout << fillWithPrimesVec(d);
return 0;
}
Could anyone please help me?

abs function is not giving right answer in c++? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am working on a complete pivoting method for Gaussian elimination. The only problem is with abs() function.
In code, the abs function is not giving right value 2nd time. It should give -38 while it is giving 4.4.
If anyone knows, please do tell me the error. Details: in the first loop i finds the maximum value and then divide the whole row by that value and then removes that row from all rows.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout.precision(3);
float a[4][5]={{4,-4,-3,7,1.3},{8,-3,-8,17,6.6,},{12,-12,-16,29,-2.1},{-8,33,-25,36,10.4}},temp[5];
float max=0;
int c,cl,no=4;
for(int ku=no;ku>=0;ku--)
{
for(int r=0;r<ku;r++)
{
for(int i=0;i<ku;i++)
{
for(int j=0;j<4;j++)
{
if(abs(a[i][j])>max)
{
max=a[i][j];
c=i;
cl=j;
}
}
}
cout<<"****max:"<<max<<"*****"<<endl;
for(int k=0;k<5;k++)
{
if(r==c)
{
a[r][k]=a[r][k]/max;
}
}
}
for(int r=0;r<ku;r++)
{
for(int p=0;p<5;p++)
{
if(r==c)
{
temp[p]=a[r][p];
}
else
{
a[r][p]=a[r][p]-(a[c][p]*a[r][cl]);
}
}
}
for(int i=0;i<ku;i++)
{
for(int pi=0;pi<5;pi++)
{
a[c][pi]=a[ku][pi];
a[ku][pi]=temp[pi];
}
}
for(int j=0;j<5;j++)
{
cout<<" temp:"<<temp[j]<<" ";
}
cout<<endl<<endl;
for(int i=0;i<ku-1;i++)
{
for(int j=0;j<5;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
}
Your condition is if(abs(a[i][j])>max), so it looks like you're comparing the magnitude. On the next line, you assign max=a[i][j], which will store a negative number. The next iteration will replace this.
What you want to store is
max = abs(a[i][j]);

Skipped condition and I don't know why [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I was debugging this, and debugger skipped the last 'if' even 'sum' was equal to 'n' and jump straight to 'else', I don't know why. Please help.
P/s: Can I use dynamic array to increase the mobility of my program?
#include <iostream>
#include <math.h>
using namespace std;
int exponent_of_10(); // set position for digits
int exponent_of_10(int a, int b){
for(int j = b; j>0;j--)
{
a *= 10;
}
return a;
}
main() //check if the number was palindromic
{
int n;
int a[6]={0,0,0,0,0,0};
int i = 0;
int temp;
int S;
cout<< "Input n (maximum of 6 digits): ";
cin>> n;
do
{
if(n<1)
{break;}
temp=n%10;
a[i]=temp;
n=(n-temp)/10;
i++;
}
while (n!=0);
int sum = 0;
for(int j=0; j<=5; j++)
{
exponent_of_10(a[j],j);
S = exponent_of_10(a[j],j);
if (S==0)
{break;}
sum +=S;
}
if(sum==n)
{
cout<< "Congratz, this is PALIDROMIC NUMBER !!";
}
else
cout<< "Sad life, this is NOT palidromic number";
return 0;
}
When the code exits the do ... while() loop, n is 0. For the test in the if to be meaningful, the code should save the original value of n somewhere and compare sum to that original value.

Factorial(code chef) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
please someone help me finding the error in this code. i've its python counterpart which is working fine, but when i code this in c++ , giving values(N) >=5 gives me an infinite loop.
[in this problem, you have to find the no. of trailing zeros in the factorial of a given number]
[here T is no. of test cases, N is the number, and z is output]
#include <iostream>
using namespace std;
int main(){
long int T,N,Z;
cin>>T;
for(int x=0;x<T;x++){
Z=0;
cin>>N;
for(int y=1;y<=N;y++){
if(y%5==0){
while(y/5!=0 && y%5==0){
Z+=1;
y/=5;
}
}
}
cout<<Z<<endl;
}
}
my python code is (which is working fine)
T=int(raw_input())
for x in range(1,T+1):
Z=0
N=int(raw_input())
for y in range(1,N+1):
if y%5==0:
while y/5!=0 and y%5==0:
Z=Z+1
y=y/5
print Z
change this:
for(int y=1;y<=N;y++){
int temp=y;
if(temp%5==0){
while(temp/5!=0 && temp%5==0){
Z+=1;
temp/=5;
}
}
}
your logic is not correct. Look at this, which i correctly submitted , hope it helps:-
#include<iostream>
using namespace std;
int tailing_zero(int n){
int ans=0;
int DIV=5;
while(n/DIV >0){
ans=ans+n/DIV;
DIV=DIV*5;
}
return ans;
}
int main(){
int run_count;
cin>>run_count;
int input[run_count];
for(int i=0;i<run_count;i++){
cin>>input[i];
}
for(int i=0;i<run_count;i++){
cout<<tailing_zero(input[i])<<endl;
}
return 0;
}