int main()
{
int N,D,A,B;
int count=0;
scanf("%d\t%d",&N,&D);
// printf("\n");
scanf("%d\t%d",&A,&B);
// printf("\n");
int S[N];
for(int i=0;i<N;i++)
scanf("%d",&S[i]);
// printf("\n");
int t[N];
for(int i=0;i<N;i++)
t[i] = S[i] - D;
typedef struct{
int remA;
int remB;
}arr;
arr a[N];
for(int i=0;i<N;i++){
a[i].remA = t[i]%A;
a[i].remB = t[i]%B;
}
for(int i=0;i<N;i++){
int b = (a[i].remB)%A;
int c =(a[i].remA) % B;
int d =a[i].remA;
int e = a[i].remB;
if((!d || !c) || (!b ||!e))
count++;
}
printf("%d",count);
return 0;
I am not getting any seg fault with below code. And it is very unusual to have a \t switch inside scanf() and the typedef inside the main() function.
#include <stdio.h>
#include<stdlib.h>
int main() {
int N,D,A,B;
int count=0;
scanf("%d\t%d",&N,&D); // printf("\n");
scanf("%d\t%d",&A,&B); // printf("\n");
int S[N];
for(int i=0;i<N;i++) scanf("%d",&S[i]); // printf("\n");
int t[N];
for(int i=0;i<N;i++) t[i] = S[i] - D;
typedef struct{
int remA;
int remB;
}arr;
arr a[N];
for(int i=0;i<N;i++){
a[i].remA = t[i]%A;
a[i].remB = t[i]%B;
}
for(int i=0;i<N;i++){
int b = (a[i].remB)%A;
int c =(a[i].remA) % B;
int d =a[i].remA;
int e = a[i].remB;
if((!d || !c) || (!b ||!e))
count++;
}
printf("%d",count);
return 0;
}
SIGSEGV - seg. fault occurs mostly because you are trying to access a memory location which you are not supposed to. This commonly happens when you try to access a location which is out of range of the array declared.