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 28 days ago.
This post was edited and submitted for review 28 days ago.
Improve this question
I'm trying to create a code in using C that creates an array, swaps all the numbers in the array, and tests which sort method is faster, but for some reason it stops swapping after exactly 32768 numbers, I'm pretty new to coding and I don't know what i'm doing wrong
void swap(int *a, int *b){
int temp;
temp=*a;
*a=*b;
*b=temp;
}
for(k=0;k<nswap;k++){
r1=rand()%max;
r2=rand()%max;
swap(&vet[r1],&vet[r2]);
}
This is just a part of the code, every variable has been declared correctly.
Edit: The array "vet" is initialized as vet[max] where max is a "#define max 35000"
by "stop" i mean that r1 and r2 don't swap numbers that are higher then 32768.
"k" is just a variable i use in loops. "k" does get higher then 2^15 but r1 and r2 don't
here's a reproducible example:
#include <stdio.h>
#include <stdlib.h>
#define max 35000
#define nswap 35000
void swap(int *a, int *b){
int temp;
temp=*a;
*a=*b;
*b=temp;
}
int main(int argc, char *argv[]) {
srand(time(NULL));
int vet[max];
int vettemp[max];
int k, r1, r2, temp;
for(k=0;k<max;k++){
vet[k]=k+1;
}
for(k=0;k<nswap;k++){
r1=rand()%max;
r2=rand()%max;
swap(&vet[r1],&vet[r2]);
}
for(k=0;k<max;k++){
printf("%d ", vet[k]);
}
return 0;
This is where my problem comes
I've tried to eliminate some useless declaration but it didn't work
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Hello I am beginner in C and I am reading 1.7 functions in K&R book. Below code is taken as is from the book.
int power(int m, int n);
int main()
{
int i;
for (i=0;i<10;++i)
printf("%d %d %d \n",i,power(2,i),power(-3,i));
return 0;
}
int power(int base, int n)
{
int i, p;
p=1;
for(i=1;i<=n;++i)
p=p*base;
return p;
}
I can not understand how this code works, especially this part:
int power(int base, int n)
{
int i, p;
p=1;
for(i=1;i<=n;++i)
p=p*base;
return p;
}
Here, where is returned p?
How this whole code raises number in power? And, relationship between these two parts of the code?
Any help is appreciated.
The code multiplies the base n times, which is essentially the definition of an integer exponent. What's important to recognize is that the loop executes n times, and each time it is multiplying p by the base.
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 5 years ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
#define size 20000000
int prim[size];
int i, zahl, zaehler, erg;
int sieve(int zahl, int prim[], int zaehler) {
if(zahl == 2000000)
return 1;
for(i=0; i<=zaehler; i++) {
erg = zahl%prim[i];
if(erg==0) {
zahl++;
return sieve(zahl, prim, zaehler);
}
}
zaehler++;
prim[zaehler]=zahl;
zahl++;
printf("%d\n", prim[zaehler]);
return sieve(zahl, prim, zaehler);
}
int main(){
zaehler = 0;
zahl = 2;
for(i=0;i<size;i++)
prim[i]=2;
sieve(zahl, prim, zaehler);
}
When trying to calculate prime numbers, when i run this code, it always crushes at the number 64901.
What might be the problem?
Ironically, this is literally a stack overflow due to recursion. You can make your stack large (which will only delay the issue), or change from a recursive solution to an iterative one.
(and for what it's worth, some debuggers won't be able to help you in this situation. And it's very difficult to beginners in C to understand what is going wrong until the first time they hit this problem. So congrats! You're leveling up in C)
A cheap way to verify it's indeed a stack overflow is to create extra memory on your stack in the recursive function and see if the number it crashes on changes from 64901. My guess is if you put like char dummy[2048] in there, it will crash much sooner.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
today i tried to do something new,but i didn't do that correct.Would anyone be able to do that and explain why is it so like that? Thank you in advance
#include<stdio.h>
void function(int a[],int n)/*The definition of function with void type,with parameters
int a[],int n */
{
int i;// declared count,type integer//
for(i=0;i<n;i++)//count goes from 0,to <n,and increment all time while goes//
printf("%d",a[i++]);// printing on the screen integers (a[i],i=i+1)//
printf("\n");// printing the newline //
}
main()
{
int a[]={1,2,3,4,5,6,7}; // declaration of array with 7 elements //
int n=5;// declaration of variable n type integer value of 5 //
function(a,n) // calling the function with parametres a,n//
} // end of sequence //
In my case i got the result of the 1,2,3,4,because i tought that the count goes from 1,to the one number less than n=5,but the IDE show the result of 135 ,i think the problem in my way is with counter...but all advices are welcome,thanks
Please make sure you are posting properly formatted valid C code.
Note that what you get is not one hundred and thirty five, but one, three, and five. You get that because you are incrementing the loop counter twice.
Here's a working, more readable version:
#include <stdio.h>
void function(int a[],int n)
{
int i;
for(i = 0; i < n; i++)
printf("%d ",a[i]);
printf("\n");
}
int main(void)
{
int a[]={1,2,3,4,5,6,7};
int n=5;
function(a,n);
return 0;
}
replace
printf("%d",a[i++]);// printing on the screen integers (a[i],i=i+1)//
with
printf("%d",a[i]);// printing on the screen integers (a[i],i=i+1)//
in your code you were incrementing i twice. Once in the while and once in the a[i++]
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 6 years ago.
Improve this question
I tried to implement the recursive form of Selection sort. But error as "Segmentation fault " is being shown. Where is the error? What are the errors generally faced with recursive algorithms?
#include <stdio.h>
void ssort(int[],int,int);
void prints(int[],int);
void sorting(int[],int,int);
int a[]= {5,6,3,1,2,4};
int main()
{
ssort(a,0,6);
prints(a,6);
return 0;
}
void ssort(int a[],int s,int e)
{
int min=a[s];
int ch,p,j;
for(j=s+1; j<e; j++)
{
if(min>a[j])
{
min=a[j];
ch=j;
}
}
sorting(a,j,ch);
if(s+1<e)
{
ssort(a,s+1,e);
}
}
void prints(int a[],int n)
{
int i;
for(i=0; i<n; i++)
printf("%d",a[i]);
}
void sorting(int a[],int j,int ch)
{
int p=a[j];
a[j]=a[ch];
a[ch]=p;
}
Segmentation fault is thrown when illegal memory is accessed. There is a possibility that "sorting(a,j,ch);" may get called even when the code may not have gone through the if block inside the prededing for-loop. In that case value of "ch" may be unpredictable and then when u access a[ch] inside the sorting(..) function, it may be access some illegal memory which will cause segmentation fault
Comment by BLUEPIXY in the question pretty much solves the problem. Writing this to add why those changes are needed.
ISSUE 1: At each step of recursion you will store the minimum value in first index (for that level of reursion). To do this first you identify the index of the minimum value, which in your code you are stroing in ch. Then you swap the values in first index and the index with minimum value. That means your code should be swapping the values in indexes s and ch. But, you are swapping the values in last index and the index with minimum value. So you need to make the below change:
/* sorting(a,j,ch); */ // ISSUE: you are swapping with last index
sorting(a, s, ch); // CORRECT: you are swapping with first index
ISSUE 2: If the condition if(min>a[j]) is never true then ch = j; is never executed, and ch will have indeterminate value. And then when you call sorting(a, s, ch); your program will not have deterministic results, and it may even crash. So, you have to set s as the minimum index at the beggning, as:
/* int ch,p,j; */
int ch = s, p,j; // Initialize ch with starting index
These changes should resolve you issue. However, one change you can make is, swap the values only if need:
/*sorting(a,j,ch);*/
if (ch != s)
sorting(a, s, ch);
And finally, you may want to change the name of the function sorting to swap.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I use a function that displays a block of memory's content pointed by a pointer.
But didn't get the desired output, i am new to this, please correct my if i am wrong.
when I input size =3, element = 1,2,3 , I got output = 1 only.
Here's the code:
#include <stdio.h>
#include <stdlib.h>
void merge(int **arr1);
int main(void) {
int size1;
printf("Give me the size of first array\n");
scanf("%d", &size1);
int *arr1 = malloc(size1*sizeof(int));
int *p1=arr1;
printf("Give me the elements of first array\n");
int index1;
for(index1 = 0 ; index1<size1; index1++)
scanf("%d", p1++);
merge(&arr1);
return;
}
void merge(int **arr1) {
while(**arr1) //**arr1 is the content of the passed array, if there
// is an int in it, print that out and increment to next one
{
printf("%d", **arr1); // ** is the content and * is the address i think, right?
*arr1++;
}
}
Your merge() code expects the array to be terminated by zero. The calling code is not doing it, so the behavior is unspecified (I got segfault when I tried your code).
The other issue is that you should put parentheses around *arr1:
(*arr1)++;
When I run your code with this modification and enter zero for the last element, your code runs fine.