Why am i getting segmentation fault in C code? - c

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int t,i,rem,l[i],b[i];
scanf("%d",&t);
for (i=0;i<t;i++)
{
scanf("%d %d",&l[i],&b[i]);
}
for (i=0;i<t;i++)
{
if (l[i] > b[i])
{
rem = l[i]/b[i];
rem +=1;
printf("%d \n",rem);
}
else if (l[i] > b[i])
{
rem = b[i]/l[i];
rem +=1;
printf("%d \n",rem);
}
else
{
printf("1 \n");
}
}
return 0;
}
Hi my code is getting compiled but not running due to a segmentation fault. Please help me in figuring out whether its become of some memory issue or scanf statements

Here:
int t,i,rem,l[i],b[i];
i is not initialized and you are creating arrays of size i (what's the value of i currently?). Arrays are of fixed size and it won't change when the value of i changes.
Fix the problem by changing
int t,i,rem,l[i],b[i];
to
int t, i, rem;
and add
int l[t], b[t]; /* You want arrays of `t` size */
after
scanf("%d",&t);
so that t gets initialized when the VLAs (Variable Length Arrays) are created.

Related

How do I receive elements into an array using scanf?

I'm learning C programming and I've encountered a problem using scanf for initializing values into the array. In this example, 10, 32 and 20 were input as values for the array; 20 should be in grades[2] but its value is 0.
Why doesn't the program register the last value that is input?
That is the relevant code.
I'll appreciate any help in understanding what went wrong with the program.
#include <math.h>
#include <stdio.h>
#include <stdbool.h>
#define N 50
#define MaximalSTD 10
int main() {
printf("Please enter the grades of the examinees");
printf(" followed by the expected mean\n");
double grades[N], ReqMean;
int numgrade = 0;
for (int i = 0; i < N; i++) {
if (scanf("%lf", &grades[i]) == 1) {
numgrade++;
} else
break;
}
ReqMean = grades[numgrade - 1];
printf("numgrade: %d\nReqMean: %d\n", numgrade, ReqMean);
return 0;
}
printf("numgrade: %d\nReqMean: %d\n" , numgrade,ReqMean);
You are using the wrong format specifier for a double value (ReqMean in this case).
Try Instead.
printf("numgrade: %d\nReqMean: %lf\n" , numgrade,ReqMean);

find sum of infinite series.Why does it issue a zero amount?

You need to find the sums of an infinite series with a given accuracy.(the picture with the task is given as a link)
the program constantly counts the zero amount. I don't understand what my mistake is
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int i;
int n =1;
double E,x,q;
double sum = 0;
scanf ("%f",&E) ;
scanf ("%f",&x) ;
q=pow(x,3)/6;
while(fabs(q)>=E){
if (n/2==0) {
sum=sum+q;
}
else {
sum=sum-q;
}
q=(q*pow(x,2))/(n+3);
n=n+1;
}
printf("%f",sum);
return 0;
}
It will never going to enter to that if statement. You are letting n=1 and it will be always 1, and u are doing n=n+1 in the else.

How to avoid segmentation fault in C when the array is defined?

I have an array A1[ ]={1,0,1,1,..'X',0,1,'X',.1,0...}, as big as 10 Megas of binary integers and some 'X' values (taken as the ASCII integer value), I am thinking to use malloc but I already have the array defined (I am just copy and paste the values before running my code). Any other suggestions on how to avoid segmentation fault? So far this the error that I am geting :(.
The code:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
//int A[]={'x','x',1,0,'x',0,'x',1,1,1,...};//up to 10M values
int A[]={0,'x',1,'x',1,0,'x',0,'x','x',1,'x',0,'x','x','x','x','x','x',0};
int j;
#define n (sizeof(A)/sizeof(*A)) // bits
printf("Patt size= %d\n",n);
/*Code will be added here to make some calculations with A*/
printf("A ="); // show input
for(j=0; j<n; j++)
printf(" %d",A[j]);
return 0;
}

Segmentation fault in C when using arrays

When this code is run, it shows a segmentation fault. But when address(LessThan)countarray is changed into address<=countarray, it works. I just want it to print one less array but it doesnt let me.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,check,divisor,countarray,address;
int pn[100];
for (n=2;n<100;n++){
for (divisor=2;divisor<n;divisor++){
if ((n/divisor)*divisor==n) //if (n is not a prime number)
check++;
}
if (check==0){ //if its a prime number,
pn[countarray]=n;
countarray++;
}
check=0;
}
for (address=0;address<countarray;address++)
printf("address for %d is %d and ",pn[address],address);
return 0;
}
there is no problem with condition address<countarray, you should initialize check & countarray variable.
int n,check=0,divisor,countarray=0,address;

Can't find the error in minimum difference between two numbers on array in C

So I'm really new to programming. I need to write a program that, if I give it any array of integers, it'll be able to find the two numbers closest to each other, and then give the difference between those two numbers. Also, the first number must be the number of integers that are going to be in the array.
So for example, I give it 3 1 4 8. The first 3 means that there will be three integers, so it must find the closest two numbers between those three. In this case, it's 4 - 1 = 3, so the output should be 3, but when I write it it gives me 16.
This is what I have, and I don't know what's wrong:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i;
printf("Write numbers here\n");
scanf("%d", &n);
int st[n];
for(i=0;i<n;i++)
scanf("%d",&st[i]);
int a, b, str[n*n], minimum, c;
/* here I'll make a new array, and its elements will be all the
differences between all the elements of the previous one */
for(a=0;a<n;a++)
for(b=0+a*n;b<n;b++) {
if(st[b-a*n]==st[a])
str[b]=32000;
else
str[b]=abs(st[b-a*n]-st[a]);
}
// here I'll find the smallest element on the last made array
minimum = str[0];
for(c=0;c<n*n;c++)
{
if(str[c]<minimum);
{
minimum=str[c];
}
}
printf("%d", minimum);
return 0;
}
Edit: I tried to fix it with your answers but it still doesn't work.
New code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i;
printf("Write numbers here\n");
scanf("%d", &n);
int st[n];
for(i=0;i<n;i++)
scanf("%d",&st[i]);
int a, b, minimum;
minimum = st[0];
for(a=0;a<n;a++)
for(b=0;b<n;b++) {
if((st[b] != st[a]) && (abs(st[b]-st[a]))<minimum)
minimum = abs(st[b]-st[a]);
}
printf("%d", minimum);
return 0;
}
Edit 2: Ok, I fixed it now. Thanks a lot ^^
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main() {
int n, i;
printf("write numbers here\n");
scanf("%d", &n);
int st[n];
for(i=0;i<n;i++)
scanf("%d",&st[i]);
int a, b, minimum;
minimum = INT_MAX;
for(a=0;a<n;a++)
for(b=a+1;b<n;b++) {
if((abs(st[b]-st[a]))<minimum)
minimum = abs(st[b]-st[a]);
}
printf("%d", minimum);
return 0;
}

Resources