c-Segmentation Fault:11 - c

The code is to find all possible combination of numbers present in the array a that would add up to a given number n. I am getting segmentation fault in this piece of code.
The error is Segmentation Fault:11.
Please HELP.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
void comb(long long int n,int a[],int k);
long int ct=0;
int main() {
long long int n;
scanf("%lld",&n);
int k;
scanf("%d",&k);
int a[k];
for(int i=0;i<k;i++)
scanf("%d",&a[i]);
comb(n,a,k);
ct=ct*2;
printf("%ld",ct);
return 0;
}
void comb(long long int n,int a[],int k)
{
if(n==0)
{
ct++;
return;
}
else
if(n<0)
return;
else
{
for(int j=0;j<k;j++)
{
comb(n-a[j],a,k);
}
}
}

The problem is that scanf is not safe. Use fgets and add some error checking.
For example if user types a letter on scanf("%lld", &n); your program crashes with seg fault.

Related

Segmentation fault (core dumped) in C while working with Bucket Sort

I have written a program in C for bucket sort:
#include <stdio.h>
#include <stdlib.h>
#define M 100
void insertion_sort(int arr[], int n){
int i, j, k;
for(i=1;i<n;i++){
j=i-1;
k=arr[i];
while(j>=0 && arr[j]>k){
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=k;
}
}
int max(int arr[], int n){
int max=arr[0];
for(int i=0;i<n;i++){
if(arr[i]>max)
max=arr[i];
}
return max;
}
void bucketsort(int arr[], int n){
int i, j, idx=0, *ptr, **bsort;
int len=sizeof(int *)*n+sizeof(int)*n*n;
bsort=(int **)malloc(len);
int len1=n*max(arr, n);
ptr=(int *)malloc(len1*sizeof(int));
for(i=0;i<n;i++){
*(ptr+i)=0;
}
for(i=0;i<n;i++){
int bindex=n*arr[i];
bsort[bindex][*(ptr+bindex)]=arr[i];
*(ptr+bindex)+=1;
}
for(i=0;i<n;i++){
if(*(ptr+i)>0)
insertion_sort(bsort[i], *(ptr+i));
}
for(i=0;i<n;i++){
for(j=0;j<*(ptr+i);j++)
arr[idx++]=bsort[i][j];
}
printf("The sorted array:\n");
for(i=0;i<n;i++){
printf("%d ", arr[i]);
}
}
int main()
{
int arr[M],n,i;
char op[1];
do{
printf("Enter no of elements: ");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
bucketsort(arr,n);
printf("\nDo you want to go again?(Y/N):");
scanf("%s", op);
}while(op[0]=='Y');
}
However, when I run the code, and give the necessary input, it gives me:
Segmentation fault (core dumped)
Why is this so? What sort of changes do I have to make in my code?
I've tried debugging on GDB, and it shows an error at the line
bsort[bindex][*(ptr+bindex)]=arr[i];
I don't understand why it should be so, since I have already written a max function for the same.

Why am I getting "Abort signal from abort(3) (SIGABRT)" runtime error in c?

I was trying to submit a solution to a problem on Geek4geeks website. I've checked the solution it seems correct and does not show runtime errors on codeblocks, however it shows the Abort signal from abort(3) (SIGABRT) runtime error on the website. please help me understand what is going wrong here.
this is my code:-
#include <stdio.h>
#include <stdlib.h>
int greatestVol(int* volumes, int k);
int volumeCal(int* array, int N);
int main()
{
int T, test, N, array[100], i=0;
scanf("%d", &T);
for(test=1;test<=T;++test)
{
scanf("%d", &N);
for(i=0;i<N;++i)
{
scanf("%d", &array[i]);
}
printf("%d\n", volumeCal(array, N));
}
return 0;
}
int greatestVol(int* volumes, int k)
{
int i, sol=0;
for(i=0;i<k;++i)
{
if(volumes[i]>sol)
{
sol=volumes[i];
}
else
continue;
}
return sol;
}
int volumeCal(int* array, int N)
{
int i, j, k=0, volumes[100], tot=0;
for(i=0;i<N;++i)
{
for(j=i+1;j<N;++j)
{
if(array[i]<array[j])
{
volumes[k]=array[i]*(j-i);
++k;
}
else
{
volumes[k]=array[j]*(j-i);
++k;
}
}
}
return greatestVol(volumes, k);
}

Unable to shuffle cards randomly

I am doing a project that shuffle 10 cards (12) time randomly but it didn't work with my code.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void show(int[],int);
void shuffle(int[],int,int);
int main (void)
{
int karten[]={1,2,3,4,5,6,7,8,9,10};
int n = sizeof(karten)/sizeof(int);
//printf("%d",n);
int s=12;
srand(time(NULL));
printf("Karten vor dem Mischen: \n");
show(karten,n);
shuffle(karten,n,s);
printf("Karten nach dem Mischen:\n");
show(karten,n);
return 0;
}
void show(int karten[],int n)
{
for(int i=0;i<n;i++)
{
printf("%d,",karten[i]);
}
printf("\n");
}
void shuffle(int karten[],int n,int s)
{
int i=0;
int d=0;
int v[]={(int)malloc(sizeof(karten))};
for(int z=0;z<=s;z++)
{
i=rand()%10;
d=rand()%10;
//printf("%d-->%d\n",i,d);
v[i]=karten[i];
v[d]=karten[d];
karten[d]=v[i];
karten[i]=v[d];
}
printf("Es wurden %d Vertauschungen gemacht\n",s);
free(v);
}
The error is
(((process returned 255)))
and the program crashed.
void shuffle(int karten[],int n,int s) is equivalent to
void shuffle(int *karten,int n,int s)
so int v[]={(int)malloc(sizeof(karten))}; allocates just the size of karten
which is a pointer to an int, so 4 bytes(on most systems).
Because of that You are trying to access unallocated memory.
sizeof(karten) don't give you the length of the array, see sizeof array clarification. Just rewrite the code like this (don't need another array):
void shuffle(int karten[],int n,int s)
{
int i=0, vi;
int d=0, vd;
for(int z=0;z<=s;z++)
{
i=rand()%10;
d=rand()%10;
vi=karten[i];
vd=karten[d];
karten[d]=vi;
karten[i]=vd;
}
printf("Es wurden %d Vertauschungen gemacht\n",s);
}

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;
}

Program in c with functions without pointers doesn't work [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 8 years ago.
Improve this question
I have written a code and it doesn't run. However, there are no mistakes in compiler. I am stuck, could you give me a hint please where am I wrong?
I am 95% sure it is about checktable function.
P.S. I know how to do it with pointers, but I try to understand how to make it without them.
Thank you!
#include <stdio.h>
#include "genlib.h"
void fillarray (int a[50])
{
int i;
for(i=0;i<50;i++)
a[i]=rand()%10;
}
void printarray (int a[50])
{
int i;
for(i=0;i<50;i++)
printf(" %d ", a[i]);
}
int number()
{
int num;
printf("Give the number!");
num=GetInteger();
return num;
}
void checktable (int a[50], int b[50], int ar,int count)
{
int i;
count=0;
for(i=0;i<50;i++)
if (a[i]==ar)
{
b[count]=i;
count++;
}
}
main()
{
int i,a[50], b[50], N,count;
fillarray(a);
printarray(a);
N=number();
checktable(a,b,N,count);
printf("the number is used %d times", count);
printf("in places:");
for(i=0;i<count;i++)
printf("%d ",b[i]);
getch();
}
Approach 1: The "pointer" way:
In your code, change
void checktable (int a[50], int b[50], int ar,int count)
to
void checktable (int a[50], int b[50], int ar,int *count)
and call it as checktable(a,b,N,&count);
Approach 2: The "return" way:
Otherwise, the count which is updated in checktable() won't be reflected in main().
However, Alternatively (without using pointers), IMO, its always easier to simply return the number of occurrences via return statement. In that case , code will look like
int checktable (int a[50], int b[50], int ar)
{
int i;
int count=0;
for(i=0;i<50;i++)
if (a[i]==ar)
{
b[count]=i;
count++;
}
return count;
}
and the calling will be
count = checktable(a,b,N);
Note: Its always a good practice to initialize your variables.
If you don't want to use pointers,then you can return count and assign it to the count variable in main. I've improved your code here with all of it explained in the comments inside the code:
#include <stdio.h>
#include <stdlib.h> //Don't forget this header for srand and rand
#include <time.h> //For the time in srand
#include <conio.h> //For using getch()
#include "genlib.h"
void fillarray (int a[]) //leave the brackets empty
{
int i;
for(i=0;i<50;i++)
a[i]=rand()%10;
}
void printarray (int a[]) //here too
{
int i;
for(i=0;i<50;i++)
printf(" %d ", a[i]);
}
int number()
{
int num;
printf("\n\nGive the number!");
num=GetInteger();
return num;
}
int checktable (int a[], int b[], int ar) //Use int to return an int
//No need for count parameter
{
int i,count;
count=0;
for(i=0;i<50;i++)
if (a[i]==ar)
{
b[count]=i;
count++;
}
return count; //returning count
}
int main() //Use int main
{
int i,a[50], b[50], N,count;
srand(time(NULL)); //For rand to generate random numbers
fillarray(a);
printarray(a);
N=number();
count=checktable(a,b,N); //set count to the return value of checktable
//Also,no need to pass count as a parameter
printf("The number is used %d times", count);
printf(" in places:");
for(i=0;i<count;i++)
printf("%d ",b[i]+1); //Don't forget +1 otherwise output might come as place 0
getch();
return 0; //main returns int
}

Resources