About tail recursion [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Recently I am reading Mastering Algorithms with c, and in this book I have 1 exercise that I am not able to implement with c.
Tn = 1 if n=1 ,
Tn = 2T(n/2) + n if n > 1
Anyone can help me? I'll appreciate it a lot.
I have tried.
#include <stdio.h>
int test(int n) {
if (n == 1)
return 1;
else if( n > 1 )
return test(n / 2) * 2 + n;
}
int testtail(int n, int running_result) {
if (n == 1)
return running_result;
else
**return testtail(n / 2, ???? );** // How can I implement the second param
}
I am sorry guys! I am not a native English speaker! Maybe I made some mistakes in grammer! I should apologize for this!

Related

Why am I getting an error in this C program? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
I am new to C programming but I stumbled on this code
int print(int nb)
{
if (nb < 0)
{
return (0);
}
printf("%d", nb + print(nb - 1));
nb --;
return (nb);
}
int main(void)
{
print(4);
return (0);
}
I ran the code and it gave me an output of 00246
why is that the output that, looking at it logically, the answer is not suppose to start with a 0
print(4) -> print(3) -> print(2) -> print(1) -> print(0) -> print(-1)
print(-1) stops the recursion returning 0, thus a call to printf() is emitted with 0 + 0, which is 0.
print(0) ends with -1 as value, and a call to printf() with 1 + -1 is emitted, which is 0.
etc.

When I run my code it shows some errors on terminal. Could any can tell me what is the error stands for? How can I debug it? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Errors:
Test isPower2(0[0x0]) failed.
Gives 1[0x1]. Should be 0[0x0]
Code:
int isPower2(int x) {
int nonNega = (x>>31);
int result = !((x & (x-1)) ^ nonNega);
return result;
}
Your isPower2(0) returns true (1) but 0 is not a power of 2. So the expected result would be false (0).

Basic C program does not display anything [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 7 years ago.
Improve this question
What does my program not output anything. I have tried other similar versions of solutions for project euler problem 1. I don't need the answer I would just like to know why their is no output. After I compile with gcc and execute the file it seems like is freezes with no output. I have to ctrl-z to kill the program.
#include <stdio.h>
/* Project Euler Problem 1 */
int main()
{
int sum = 0;
int i = 0;
while (i <= 1000);
{
if (i % 3 == 0 || i % 5 == 0);
{
sum += i;
}
i++;
}
printf("%d\n", sum);
return 0;
}
You are closing the while without doing any instruction by putting a semi-colon:
while (i <= 1000);
You should drop the semicolon.
The same for the if instruction:
if (i % 3 == 0 || i % 5 == 0);

what is the fault with c here? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Please excuse me for asking this, because I know the code I'm gonna give you is wrong. Being a newbie I am not able to find the fault. Please help me correct the question and give a solution as well. Again I'm sorry to bother with this simple problem. Tomorrow is my exm in C so i'm kinda desperate. :(
Q: What will be the output of the program?
First let me show you how I find the code first:
#include<stdio.h>
int funct l(int n){
if (n>3)
return int funct(n-3)));
}
main() {
int n= 10;
printf("%d", funct l (n));
}
Then I thought i'd correct it. Then I cleaned up the code as far as i can. Then the code came to this:
#include<stdio.h>
int funct(int n){
if (n>3){
return funct(n-3);
}
}
main() {
int n= 10;
printf("%d", funct(n));
}
still it doesn't give proper answer (though I don't know what it'll show). It is either 1 or 2 and process returned 1 (0*1) is showing at the last line.
Please help me out!
Your funct function doesn't always return a value. This means that it could return anything. Try this:
int funct(int n) {
if (n > 3)
return funct(n - 3);
return n;
}
Here is the call stack when n = 10
funct(n = 10)
funct(n = 7)
funct(n = 4)
funct(n = 1)
return 1
return 1
return 1
return 1
Here is the call stack when n = 11
funct(n = 11)
funct(n = 8)
funct(n = 5)
funct(n = 2)
return 2
return 2
return 2
return 2

Can anybody help me make sense of this code my prof posted? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
My professor posted this code for us to decipher it for kicks and giggles. It outputs '12 days of christmas'
#include <stdio.h>
int main(t,_,a)
char *a;
{return!0<t?t<3?main(-79,-13,a+main(-87,1-_,
main(-86, 0, a+1 )+a)):1,t<_?main(t+1, _, a ):3,main ( -94, -27+t, a
)&&t == 2 ?_<13 ?main ( 2, _+1, "%s %d %d\n" ):9:16:t<0?t<-72?main(_,
t,"#n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+\
,/+#n+,/#;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/\
+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# ){n\
l]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#\
n'wk nw' iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;\
#'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/")
:t<-50?_==*a ?putchar(a[31]):main(-65,_,a+1):main((*a == '/')+t,_,a\
+1 ):0<t?main ( 2, 2 , "%s"):*a=='/'||main(0,main(-61,*a, "!ek;dc \
i#bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);
}
That is exactly how he posted it. I have executed it and it works alright, but it's a jumble.
This is an ancient entry into the IOCCC. It has been reverse engineered.

Resources