Why is the following code showing run-time error(SIGSEGV)? - c

This is my solution to the Prime Generator problem on SPOJ(Sphere Online Judge)(http://www.spoj.com/problems/PRIME1/). I have allocated memory using malloc(), but it is still showing a run-time error(SIGSEGV).
#include <stdio.h>
#include <stdlib.h>
int main(){
int n;
scanf("%d", &n);
while(n--){
int a,b;
scanf("%d %d", &a, &b);
int *z =malloc(sizeof(int)*(b-1));
// Filling the array
int i;
for(i=0;i<b-1;i++){
z[i]=i+2;
}
int k,p;
for(k=0;k<=b-2;k++){
if(z[k]){
if(z[k]>=a){
printf("%d\n", z[k]);
}
for(p=k+z[k];p<=b-2;p+=z[k]){
z[p]=0;
}
}
}
free(z);
}
return 0;
}

Related

can not run the simple function program

#include <stdio.h>
#include <conio.h>
int sum();
//find the sum of two numbers entered by the user.
int main(){
int n, m;
printf("Enter two numbers: \n");
scanf("%d", &m);
scanf("%d", &n);
int result = sum(m, n);
printf(result);
getch();
return 0;
}
int sum(m, n){
int c;
c = m+n;
return c;
}
i was just writing a simple program with function but i don't know why it is not running it tells me to debug can someone tell me what is the problem with it
Change int sum() ; to int sum(int, int) ;
Change printf(result) to printf("%d", result) ;
Change int sum(m, n) to int sum(int m, int n) ;(https://i.stack.imgur.com/Z07cx.jpg)
Another way of writing above program is
(https://i.stack.imgur.com/VOXRY.jpg)
#include <stdio.h>
#include <conio.h>
int sum(int n, int m);
//find the sum of two numbers entered by the user.
int main(){
int n, m;
printf("Enter two numbers: \n");
scanf("%d", &m);
scanf("%d", &n);
int result = sum(m, n);
printf("%d",result);
getch();
return 0;
}
int sum(int n, int m){
int c;
c = m+n;
return c;
}

The function is not returning in clang

I am trying to make a program to calculate the area of a rectangle
but the function doesn't return any value
This is the code as in cs50 sandbox
#include <stdio.h>
#include <cs50.h>
int calcrectarea(int len,int wid){
return len*wid;
}
int main(){
printf("enter length here:\n");
int x; scanf("%d",&x);
printf("enter width here:\n");
int y; scanf("%d", &y);
calcrectarea(x,y);
}
Actually, the function returns a value. Maybe you want to see it, so just print the result:
#include <stdio.h>
#include <cs50.h>
int calcrectarea(int len,int wid){
return len*wid;
}
int main(){
printf("enter length here:\n");
int x; scanf("%d",&x);
printf("enter width here:\n");
int y; scanf("%d", &y);
printf("area is: %d", calcrectarea(x,y));
}

reversing elements of an integer array

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int reverse(int a[20],int n);
int main()
{
int a[20];
int n;
printf("enter the number of elements \n");
scanf("%d",&n);
printf("enter the array elements\n");
for(int i=0;i<n;i++)
{
scanf("%d ",&a[i]);
}
reverse(a,n);
return 0;
}
int reverse(int a[20],int n)
{
for(int i=n-1;i>=0;i--)
{
printf("%d ",a[i]);
}
return 0;
}
here if I input n=4 then during runtime i have to take 5 elements and then it reverses.For eg if i take n=4 and then for no of elements i have to take 1,2,3,4,5 and then only output is coming as 4 3 2 1.Why? is my logic wrong? also in this code I am unable to take the number of elements of arrays in a straight line, like 1 2 3 4.When I am entering the number each number is entering in new line .I am a novice programmer in C and thus having these doubts.Please anyone explain...
The problem with your code is the extra space after %d in your scanf line where you accept array elements i.e.
for(int i=0;i<n;i++)
{
scanf("%d ",&a[i]); //should be scanf("%d",&a[i]);
}
Change that and you're good to go.
Here is your entire program refactored to work correctly:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int reverse(int a[20], int n)
int main()
{
int a[20];
int n;
printf("enter the number of elements \n");
scanf("%d",&n);
printf("enter the array elements\n");
for (int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
reverse(a,n);
return 0;
}
int reverse(int a[20], int n)
{
int mid = n/2;
for (int i=0; i < mid; ++i)
{
int temp = a[n-i-1];
a[n-i-1] = a[i];
a[i] = temp;
}
return 0;
}

Issues with pascal triangle and dynamic memory allocation, c

I'm writing a code for my C programming class and stumbled upon a problem. I'm supposed to write a program which will show as an output Pascal's triangle. I'm to use 1d arrays and in each iteration make the array bigger by using realloc. The trouble is that even though the code compiles and runs when I type eg '7' (as the height of the tringle) in the 7th column there will be trash number. I have no idea why it happens. I'm a beginner in dynamic memory allocation, so please by gentle.
Here's my code:
int i,n;
printf("Give the height:\n");
scanf("%d", &n);
int *tab = (int*)malloc(sizeof(int));
int *tab2, liczba=2;
for(i=0;i<n;i++)
{
tab2=(int *)realloc(tab,i+1);
tab2[i]=&liczba;
print(tab2, i+1);
printf("\n");
}
void print(int *tab, int size)
{
int i;
for(i=0;i<size;i++) printf("%d\t", tab[i]);
}
#include <stdio.h>
#include <stdlib.h>
void print(int *tab, int size);
int main(void){
int i, j, n;
printf("Give the height:\n");
scanf("%d", &n);
int *tab = NULL;
for(i=1;i<=n;++i){
int *temp = realloc(tab, i * sizeof(*tab));
if(temp)
tab = temp;
else {
perror("realloc");
free(tab);
exit(EXIT_FAILURE);
}
tab[i-1] = (i == 1) ? 1 : 0;
for(j=i-1;j>0;--j){
tab[j] += tab[j-1];
}
print(tab, i);
}
free(tab);
return 0;
}
void print(int *tab, int size){
int i;
for(i=0;i<size;i++){
if(i)
putchar('\t');
printf("%d", tab[i]);
}
putchar('\n');
}

Unexpectedly crash in program for finding prime numbers

I would like to write a program which can find all prime numbers between two numbers in t test cases. But my program had crashed when I run it.
Please, could anyone help me?
My code:
#include <stdio.h>
#include <malloc.h>
#include <math.h>
void print(int a,int b)
{
int *p,i;
int x;
p = (int *) malloc (sizeof(int)*(b-a));
for(i=0;i<(b-a);i++) p[i]=a+i;
for(i=0;i<(b-a)/2;i++)
{
if(p[i]!=0)
{
if(p[i]%i==0) p[i]=0;
}
}
for(i=0;i<=(b-a);i++) if(p[i]!=0) printf("%d ",p[i]);
free(p);
}
int main(void)
{
int t,i,m,n;
scanf("%d",&t);
for(i=0;i<t;i++)
{
scanf("%d %d",&m,&n);
print(m,n);
}
return 0;
}
The problem is stumbling on allocating memory in a range, allocating one element too few (should have been malloc (sizeof(int)*(b-a+1));) and then not sticking to indexing the memory allocated. This could be so much simpler: no arrays needed - if a number has a divisor, there is no need to check any other divisors.
Sometimes it is easier to side-step the problems than struggle with them.
#include <stdio.h>
#include <math.h>
int prime(int n)
{
int s, i;
if (n == 1 || n == 2)
return 1;
if (n % 2 == 0) // no even numbers
return 0;
s = (int)sqrt(n); // limit the loop
for (i=3; i<=s; i+=2) // odd numbers only
if (n % i == 0)
return 0;
return 1;
}
void print(int a, int b)
{
int n;
for (n=a; n<=b; n++)
if (prime (n))
printf("%d ", n);
printf("\n");
}
int main(void)
{
int t, i, m, n;
printf("Input number of ranges to test: ");
scanf("%d", &t);
for(i=0; i<t; i++)
{
printf("Input bottom and top of range: ");
scanf("%d %d", &m, &n);
print(m, n);
}
return 0;
}
You messed up the termination of the for-loops. You allocated b-a bytes but you are iterating over b-a+1 items...
for(i=0;i<=(b-a);i++) p[i]=a+i;
needs to be a i<(b-a) or else you have a segfault (in both loops).
Also as BLUEPIX pointed out:
for(i=0;i<b/2;i++)
needs to be i<(b-a)/2 for iterating over half the intervall.
p[i]%i
Division by zero in the first iteration i==0.
After this the programm should terminate without an error.

Resources