Max heapify in c creating infinite recursion [closed] - c

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 4 years ago.
Improve this question
This is program for max heapify ,i have doubt int this algorithm,that if a already max heap is passed into this function MAX_HEAPIFY in that case largest will equal to i only and directly recursive call to MAX_HEAPIFY
will execute only in that recursion will go infinitely
i am assuming that array index is starting from 1 instead of zero
MAX_HEAPIFY(A,i,heapsize){
l=2i;
r=2i+1;
if(l<=heapsize&& A[l]>A[i])
largest=l;
else
largest=i;
if(r<=heapsize&&A[r]>A[largest])
largest=r;
if(largest!=i)
swap(A[i],A[largest])
MAX_HEAPIFY(A,largest)
}

There may be some problem with indentation when copying this algorithm. But if you put MAX_HEAPIFY(A,largest) under this if(largest!=i) condition, then algorithm is correct. See the Algorithm below for more clarification:
MAX_HEAPIFY(A,i,heapsize){
l=2i;
r=2i+1;
if(l<=heapsize&& A[l]>A[i])
largest=l;
else
largest=i;
if(r<=heapsize&&A[r]>A[largest])
largest=r;
if(largest!=i) {
swap(A[i],A[largest])
MAX_HEAPIFY(A,largest)
}
}

Related

Codeforces's problem 151A I've wrote this program in C language , I think it has some problem. Help me out please [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 1 year ago.
Improve this question
151A codeforces link
Here is my code written in C language. It has some problem
#include<stdio.h>
#include<conio.h>
int main ()
{
int n,k,l,c,d,p,nl,np,mm,per,tl,to,sum;
scanf("%d%d%d%d%d%d%d%d",&n,&k,&l,&c,&d,&p,&nl,&np);
mm = k*l;
per = mm/nl;
tl = c*d;
to = p/np;
sum=(per,tl,to)/n;
printf("%d",sum);
return 0;
}
You have to find the minimum value among per, tl, and to and divide that with n instead of just calculating to/n, which you are currently calculating with sum=(per,tl,to)/n;. (per and tl are ignored according to the definition of the comma operator)

Trying to understand the error in my code [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 2 years ago.
Improve this question
I'm creating a code where someone enters the amount of people eating a cut from and from there I can figure out how many pieces I can have in one pizza.I'm having trouble and don't know how to fix my error.
#include <stdio.h>
int Cuts(int n)
{
int max = n*2;
return m;
}
int main()
{
int m;
m = Cuts();
printf("P1:%d\n" , m);
}
Your CutYourPizza function is written to require one integer argument (called n), but when you invoked that function on the line max = CutYourPizza(); you did not supply any argument.
For example, if you wanted to supply the number 10 as an argument, then you could have written max = CutYourPizza(10); with the argument 10 inside the parentheses.

Friends Pair Algorithm Recursive solution in C [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
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.
Improve this question
Given n friends, each one can remain single or can be paired up with
some other friend. Each friend can be paired only once. Find out the
total number of ways in which friends can remain single or can be
paired up. Examples: Input : n = 3 Output : 4
Explanation: [{1}, {2}, {3} : all single ]
[{1}, {2,3}] : 2 and 3 paired but 1 is single]
similarly
[{1,2}, {3}]
[{1,3}, {2}]
finally answer 4
here I'm stuck to construct the recursion
int friends(int i)
{
if(i==0){
return 0;
}
if(i==1){
return 1;
}
friends(i)=friends(i-1)+(i-1)*friends(i-2);
}
For further reference : http://www.geeksforgeeks.org/friends-pairing-problem/
you are missing return statement
return friends(i-1)+(i-1)*friends(i-2);
and as code from link says, there is
if (i <= 2)
dp[i] = i;
and you are missing i == 2 in your code
for example this recursion works, I do not know what do you need to know more:
#include <stdio.h>
int friends(int i)
{
if(i<=2)
return i;
return friends(i-1)+(i-1)*friends(i-2);
}
int main()
{
printf("%d", friends(3));
return 0;
}

C Realloc Memory [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 6 years ago.
Improve this question
I need to memorize how much memory I allocated with realloc().
Help me
if(!array)
array=(Type*) calloc(1,sizeof(Type));
else
array=(Type*)realloc(array,(cont+1)*sizeof(Type));
array[cont].setName(....);
cont++;
It doesn't work: after firt insert, it say: Access violation
I initialized the cont = 0 in the constructor of my class and freed memory in the destructor.
See the comments added to your code:
int count=0;
if(!array)
array=(Type*) calloc(count,sizeof(Type*); // Problem:
// missing )
// use sizeof(Type)
// calling calloc with count being zero
// so you do not allocate any memory
// use 1 instead of count
array[c].setName(EditName->Text);
c++;
count++;
array=(Type*)realloc(array,count*sizeof(Type*)); // Problem:
// use sizeof(Type)
so it should look:
int count=0;
if(!array)
array=(Type*) calloc(1,sizeof(Type));
array[c].setName(EditName->Text);
c++;
count++;
array=(Type*)realloc(array,count*sizeof(Type));
The variable c must be initialized to zero before running this code
Likewise array must be nullptr before running this code
EDIT
There seem to be one more problem if you intend to run this code several times (which I assume you do).
This line:
array=(Type*)realloc(array,count*sizeof(Type));
^^^^^
Don't use count here as you always sets count to zero
The line shall be:
array=(Type*)realloc(array,c*sizeof(Type));
In general there seems to be no real use of count

Why is the result of this short C program "3 2"? [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 7 years ago.
Improve this question
Here is the source codeļ¼š
#include <stdio.h>
enum coordinate_type{ RECTANGULAR = 1,POLAR };
int main(void)
{
int RECTANGULAR;
printf("%d %d\n",RECTANGULAR,POLAR);
return 0;
}
Why is the result the following:
3 2
You are redefining what RECTANGULAR is in the main function. It gets initialized with a "random" value, in this case it is 3, but it could be anything else.
POLAR keps its value of 2 because of how the enum is defined.
Try redefining the RECTANGULAR variable in main to see different outputs.

Resources