While simulating the Colatz Conjecture problem i have made recursion when i want to print the count number in the recursion i get the result i need but when the function return the result it gives me weird numbers, why is that?
#include <stdio.h>
#include <stdlib.h>
int divide(int n,int count){
if(n<=1){printf("%d ",count);return count;}
if(n%2==1){divide(n=3*n+1, ++count);}
else{divide(n/=2, ++count);}
}
int main(void) {
printf("%d ",divide(10,1));
return 0;
}
You don't have any default return. So the return value is undefined.
You need to return the results of the recursive calls:
if (n % 2) { return divide(3 * n + 1, count + 1); }
// %%%%%%
else { return divide(n / 2, count + 1); }
Note that there's no point in assigning to the local variables, so I've changed that to simple computations.
A. you need to return the returned value from the recursion calls.
B. why do you assign a value to n in the calls to divide?
#include <stdio.h>
#include <stdlib.h>
int divide(int n,int count){
if(n<=1){printf("%d ",count);return count;}
if(n%2==1)
{
return divide(3*n+1, ++count);
}
else
{
return divide(n/2, ++count);
}
}
int main(void) {
printf("%d ",divide(10,1));
return 0;
}
Related
So I've been trying to do this but I just can't think of a solution for this. I've got this bit of code but it outputs it backwards(if the answer is 11110 I get 01111):
#include <stdio.h>
int base(int n)
{
if(n==0)
return 0;
else
{
printf("%d",n%2);
}
return base(n/2);
}
int main() {
int n;
scanf("%d",&n);
base(n);
return 0;
}
Is there any trick for this problem or do I need to analyze this deeper?
As #rici stated, a very simple fix is to print after the recursive call:
#include <stdio.h>
void base(int n){
if(n==0)
return;
base(n/2);
printf("%d",n%2);
}
int main() {
int n;
scanf("%d",&n);
base(n);
return 0;
}
I would use a mask:
#include <stdio.h>
int base(int n, int mask){
if(!mask) {
printf("\n"); // we reach the end, print a line return
return 0;
}
printf("%d", !!(n & mask)); // if mask and n match, print '1', else print '0'. !! convert any value into 1, and 0 remains 0.
return base(n, mask >> 1); // divide mask by 2, check the next bit on the right
}
int main() {
int n;
scanf("%d",&n);
base(n, 1 << (31 - __builtin_clz(n))); // call the function with a mask initialized at the same level than the most important bit of n.
return 0;
}
I want to calculate a number to the power p, got Segmentation fault as a result.
This code is supposed to work:
#include <stdio.h>
int my_power(int nb, int p)
{
if (nb != 0){
return nb*my_power(nb, p-1);
}
return 1;
}
int main(int argc, char argv[]){
printf("%d\n", my_power(5, 3));
return 0;
}
In your code, your recursion never ends.
Change the base case to pb<=0 and it will work.
I have come to a solution by checking for p instead of nb and it worked.
I've added conditions for the negative valeus and the 0^0 case.
int power(int base, int a) {
if ( base=0 && a=0){
return 1;
}
if (a!=0){
return (base * power(base, a-1));
}else{
return 1;
}
}
Thank you for the hints.
I would like to return the minimum of three given numbers like this.
I don't know why it doesn't return anything
#include <stdio.h>
int minimum3(int un, int deux , int trois)
{
int minimum;
if (un<deux && un <trois)
minimum= un;
else if (deux<trois && deux<un)
minimum= deux;
else if (trois<deux && trois<un )
minimum= trois;
return minimum;
}
int main(void) {
minimum3(4,88,8999);
return 0;
}
As others mentioned in the comments, you ignore the returned value, a quick fix for that would be :
int main(void) {
int min = minimum3(4,88,8999);
printf("min: %d\n",min);
return 0;
}
Despite that, your algorithm isn't that effective, as Jonathan mentioned, if 2 of the numbers you process are equal, there is no way to calculate the minimum. The better would,imo, would be to have another function that calculates the minimum of 2 numbers and then use that to compare to the third number. Much cleaner this way.
#include <stdio.h>
int min2(int a,int b)
{
return ((a <= b) ? a : b);
}
int min3(int a,int b,int c)
{
int mintmp = 0;
mintmp = min2(a,b);
return ((mintmp <= min2(mintmp,c)) ? mintmp : min2(mintmp,c));
}
int main(void)
{
printf("%d\n",min2(5,10));
printf("%d\n",min3(5,-1,1));
return 0;
}
You can of course replace the conditional expressions with simpler if else .
I'm trying to recursive function in C that calculates the sum of the numbers from x to max (inclusive). For example, sum (4, 7) would compute 4 + 5 + 6 + 7 and return the value 22. The function code a must be recursive so you are not allowed to use any conventional loop constructs.
I have this and i think it should work but i'm not entirely sure why its not
#include <stdio.h>
int main()
{
int sum (x, max);
int total, y, x, max;
if (x<max){
y=x+1;
total = x+sum(y,max);
return total;
return x;
}
return 0;
}
Thanks for any help with this in advance!
Here is one possible solution:
#include <stdio.h>
int sum_in_range(int a, int b){
if(a != b){
return sum_in_range(a+1,b)+a;
}
else{
return b;
}
}
int main(void) {
// your code goes here
printf("%d",sum_in_range(2,4));
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int sum(int s,int max)
{
if(s==max)
{
return s;
}
else
{
return(s+sum(s+1,max));
}
}
int main()
{
int r,s,max;
printf("\n enter s and max");
scanf("%d%d",&s,&max);
r=sum(s,max);
printf("%d",r);
}
I spotted some errors on your code. I'm not a pro yet but here's what I think
I just edit your code. removed, added and rearranged some stuff*/
/*First, let's look at your code*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int sum(x, max);//I think what you want to do here is declare a function but instead declaring, you define it here because you added semicolon (;)
int total, x, y, max;
if(x < max)
{
y = x + 1;
total = x + sum(y, max); //you don't have a function declaration for "sum"
return total;
return x; //this will never return since you already "return the total before this"
}
return 0;
}
//////////////////////////////////////////////////////////////
/*And I think this is what you want to do*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x = 4, max = 6; //We declare this here for the "sum" function. This way "sum" function can recognize these variables
int total = x; //be sure to set the total to x.
//you can make a void function for this instead of "int". but either way, it can do the job.
void sum(int y) //no need to pass a "max" because "max" is already recognized by the "sum" function since we declare the variables at the top of "main" function
{
if(x < max)//don't make it x <= max. Because of the argument "total = total + (x + 1)" on this function. If you do, the total will exceed.
{
//You can see here why we set the value of "total" to x.
total = total + (x + 1);//And also, you can see why we didn't make the argument in if() statement like this: if(x <= max).
x++;//increment "x" every loop
//call the function again and pass the total until x == max.
sum(total);
}
}
//pass the x
sum(x);
//check the answer
printf("The total is %d\n\n", total);
return 0;
}
//////////////////////////////////////////////////////////////
/*It looks messy with comments*/
/*Here's the code looks like without a comment.It's pretty short code if you look remove the comments.. LOL..*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x = 4, max = 6;
int total = x;
void sum(int y)
{
if(x < max)
{
total = total + (x + 1);
x++;
sum(total);
}
}
sum(x);
//check the answer
printf("The total is %d\n\n", total);
return 0;
}
#include <stdio.h>
int main (void)
{
int n, x;
int factorial (int n)
{
if (x<=0)
{
printf("x equals: ");
return 1;
}
else
{
return n * factorial (n-1);
}
f(x)=f(x-1)+2; //says error is here
}
return 0;
}
I've tried some things and can't get it to work. I could just be overtired and looking past the smallest thing but help would be much appreciated! Thanks :)
You cannot declare a function definition inside of main() or any other function ... function definitions have to be stand-alone and cannot have embedded function definitions inside of them.
Also I'm not sure what you're doing on the line that you've marked as an error since f() is not a defined function, so you can't call it. Furthermore, it would need to return some type of l-value, such as a pointer to a static variable declared inside the function, or a pointer passed by reference to the function and even then the syntax is not right since there would be a required dereference ... so basically you can't do what you're doing on that line.
To get something that compiles, try
#include <stdio.h>
int factorial (int n)
{
if (n <= 0)
{
return 1;
}
else
{
return n * factorial (n-1);
}
}
int main (void)
{
int x;
x = factorial(5);
printf("Factorial of 5 is equal to %d", x);
return 0;
}
Use indentation to see possible problems with scope:
#include <stdio.h>
int main (void)
{
int n, x;
int factorial (int n)
{
if (x<=0)
{
printf("x equals: ");
return 1;
}
else
{
return n * factorial (n-1);
}
f(x)=f(x-1)+2; //says error is here
}
return 0;
}
As far as I can remember, C doesn't have closures.
A function cannot be defined inside another function. However gcc allows it as an extension. You have defined a function named factorial but are trying to use f which hasn't been declared anywhere.