Friends Pair Algorithm Recursive solution in C [closed] - c

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

Related

New to C and can't figure out these outputs [closed]

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 last year.
Improve this question
Hi I am new to c and trying to build just a simple print array however I just get what it wants to give me. Here is my code:
#include <stdio.h>
int main(){
int n[5]={5,10,15,20,25};
int i;
printf("displaying integers:");
for ( i=0; i<5; i++)
{
printf("%d \n", &n[i]);
}
return 0;
}
And the output is:
displaying integers:6422280
6422284
6422288
6422292
6422296
Any help would be great I tried creating it as an enter integers and get an output but regardless of input it gave me extremely large numbers. which is why I'll be happy if it just prints. Sorry if it's an obvious one but I've tried 5 different ways all with similar/basically identical results.
What you get printed are address locations of items stored in array.
If you want to print values of items in array, you should not use the address-of operator [ & ]. Try it this way:
printf("%d \n", n[i]);

When I execute the following code, there is no output, what could be the cause? [closed]

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 1 year ago.
Improve this question
Here is my C code. I am not getting any output. Please help. I also tried adding the initialization of inside the main function, then also I am not getting any output.
#include <stdio.h>
int x = 10;
int main()
{
if (x = 20)
{
x = -1;
}
else
{
printf("x not eqaul to 20\n");
}
if (x > 0)
{
printf("x not greather than 0\n");
}
else
{
/* notjing */
}
return 0;
}
So in the first if-statement you wrote if(x=20). This is not a conditunal argument, this is a mathematical operand.
So x will be set to 20; afterworths it will be set to -1. And no printf() will be called.
If think you wanted to use if(x==20).

C code to find 3 digit numbers in an interval which satisfies the criteria sum of first and last=middle [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
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.
Improve this question
I have to find out how many 3 digit numbers are there in an interval [a,b] which satisfies criteria sum of first and last digit = middle digit . For eg: 121,143 etc.
Below is the code of loop for the same
for(int i=a;i<=b;i++)
{
first=a;
last=a%10;
temp=a/10;
middle=temp%10;
while(first>10)
{
first=first/10;
}
sum=first+last;
if(sum == middle)
{
count=count+1;
}
}
printf("%d",count);
But I am not getting the correct answer. Eg: in interval [100,130] , output should be 2 whereas I am getting 0 itself.
Please help out. Thanks .
for(int i=a;i<=b;i++)
{
first=i;
last=i%10;
temp=i/10;
middle=temp%10;
while(first>10)
{
first=first/10;
}
sum=first+last;
if(sum == middle)
{
count=count+1;
}
}
printf("%d",count);
just replace a with i in your loop.
for(int i=a;i<=b;i++)
{
first=i;
last=i%10;
temp=i/10;
middle=temp%10;
while(first>10)
{
first=first/10;
}
sum=first+last;
if(sum == middle)
{
cnt=cnt+1;
}
}
printf("%d",cnt);
This will give you correct result :)

My c program is not giving any result.pls help everyone [closed]

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 6 years ago.
Improve this question
i have written a program which is not giving proper result.
main()
{
int i=1,n,s=1;
printf("enter the value of n");
scanf("%d",&n);
while(i<=n)
{
s=s*i;
i++;
if (i==n+1)
{
break;
}
}
printf("factorial of n=",s);
}
it is giving the result as shown in the picture below.
Your problem is in this line:
printf("factorial of n=",s);
This outputs factorial of n=, but it does not simply concatenate the value of s, and there is no placeholder for s, so you actually have too many parameters.
You need a placeholder for the int output:
printf("factorial of n=%d",s);
Without it, your program exits with an error (status 15, when 0 would be normal).
Also, (as Vlad pointed out in his answer) the if (i==n+1) { ... } block is redundant, as your while loop will already exit when i > n.
Write
printf("factorial of n=%d\n",s);
^^
And this code snippet
if (i==n+1)
{
break;
}
is redundant and may be removed.
You could write the loop simpler. For example
while ( n > 1 ) s *= n--;
without a need to use one more variable i.

Too many arguments to function 'rand'? [closed]

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 8 years ago.
Improve this question
I have only used the function twice and it displays the aforementioned error. Can someone explain as to why the compiler does that?
void printrandom()
{
int x = (rand(5)+1);
int y = (rand(5)+1);
printf("%d and %d - a total of %d", x, y, (x+y));
}
It is actually rand(void), which is why you are getting that error.
Try int x = (rand() % 5) + 1;
EDIT as Daniel points out, using % will actually affect the probability. See his link for how to address this issue.
Declaration for rand() function is
int rand(void);
This means that it takes no arguments. Remove 5 from rand. If you want to generate random numbers from 1 to 5, the you can do this as
int x = rand()%5 + 1;

Resources