factorial using recursion+pointers [closed] - c

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
i am learning C programming, i was trying to write a recursive function by using this prototype:
void fact(int *n);
The parameter of this function should be passed by reference. Thanks for your help.

I don't feel to be helpful in giving a complete solution -- this is just to show there is an answer:
void fact(int *n)
{
if (*n > 1)
{
int tmp = *n - 1;
fact(&tmp);
*n *= tmp;
}
}
I would never write a factorial function this way.

Related

C to MIPS convert [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Can anyone help me convert the C code below to MIPS code? I'm supposed to use recursion and get 21 - 2 as the final answer. Thanks!
/*
x is a pointer to a linked list node and is not null.
Return the min value stored in the linked list.
Assume each node is stored in memory as an int (value) followed by a pointer to the next node (next), each a word wide.
You must write it with recursion. */
int findMin(node *x) {
if(x->next == NULL)
return x->value;
else {
int min = findMin(x->next);
if(min < x->value)
return min;
else
return x->value;
}
}
Here you are:
mips-linux-gnu-gcc -S -o foo.asm foo.c

How to find square root of a number using a recursive function? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Basically i want to create a recursive function to implement this program in C
#include <stdio.h>
main()
{
float guess=1,num,num1;
int i;
printf("enter any number:\n");
scanf("%f",&num);
num1=num;
for (i=1;num1>1;i++,num1/=10); //to calculate no of digits in input
i=i/2;
printf("i:%d\n",i); //to make a better guess
for (;i>0;i--,guess*=10);
printf("guess = %f\n",guess);
for (i=1;i<=10;i++) //evaluate square root improving accuracy with each loop
{
guess=(guess+num/guess)/2;
}
printf("sqrt: %f\n",guess);
}
Something like this:
#include <math.h>
#include <float.h>
float MySqrt(float num, float prev)
{
float next = (prev+num/prev)/2;
if (fabs(next-prev)<FLT_EPSILON*next)
return next;
return MySqrt(num, next);
}
To call it, pass 1.0 as your initial guess for the prev parameter.
You can easily make this fail with a stack overflow by passing in bad data, but you probably aren't going to be tested on that in this assignment.

Find number of leaf nodes in a binary tree having depth D using array implementation [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
C code to find the number of leaf nodes in a tree with depth d.
Hint is to use array implementation of binary tree.
ignoring the hint...
int FindNumLeafs(Tree t)
{
if(t == null)
{
return 0;
}
if(t.LeftSon == null && t.RightSon == null)
{
return 1;
}
return FindNumLeafs(t.LeftSon) + FindNumLeafs(t.RightSon);
}
Height of a balanced binary tree is given by log2(n). Therefore, leaf nodes = 2^d.

meaning of this code [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
int main()
{
int x = 2, y = 6, z = 6;
x = y == z;
printf("%d", x);
}
== has higher precedence than =, and y==z is 1.
I will end the answer there, because this looks like homework.
http://codepad.org/fp4ZYJX5
The output is:
1
Have a look at this, which explains a similar, yet more complex question and will answer yours as well.
Output is 1
If you want more help we can give you

Shortest C code to reverse a string [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
Not counting the function signature (just the body) can anybody produce C code shorter than this function that will reverse a string and return the result as a pointer to the reversed string.. (not using a string reverse library function either)?
char * reverse_str(char * s)
{
char c,*f=s,*p=s;while(*p)p++;while(--p>s){c=*p;*p=*s;*s++=c;}return f;
}
not much longer, but it works.
#include <string.h>
/* precondition: s!=const && s!=NULL && *s!='\0' */
char *mystrrev(char *s)
{
char *a=s,*e=s+strlen(s)-1;
while( a<e )
{
char c=*a;
*a++=*e;
*e--=c;
}
return s;
}

Resources