Can anyone explain and trace the following multiple recursive C program? [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am trying to understand how recursion works. I seem to not understand how this multiple recursion works. Please help and thanks!
Here is the output
#include<stdio.h>
int R(int x);
int main()
{
R(5);
return 0;
}
int R(int x)
{
if(x > 0){
x--;
R(x);
R(x - 2);
printf("%d ", x);
}
}

R(5) calls R(4) which will display something and then R(2) which will also display something. After that R(5) displays 4. Which is why you see a 4 at the end of the output.
R(4)display - R(2)display - 4
Before the 4 you have the display of R(2). R(2) calls R(1) which displays somenthing and then R(0) which displays nothing. R(2) displays 1. Which is why you have a 1 at the end, just before 4.
R(4)display - R(1)display - R(0)display - 1 4
And so on...
R(4)display - R(1)display - 1 4
R(4)display - 0 1 4
You always process the R()display calls starting from the end (right side first).
As you can see recursivity will inverse the order of the numbers (4, the biggest number, is last). This is because you have the printf after the recursive calls to R.

Take a look at the trace tree I made
R(5) means the call of R function for 5, and as you know the output of it will be 4. The numbers show in which order the functions are executed till the end. The ones that have a tick besides them will make outputs.

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.

warning: no description found for function main using betty compliant syntax [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 9 months ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I'm learning the programming language C and using the Betty coding style of writing C github.com/holbertonschool/betty),
I have have been getting this syntax warning.
#include <stdio.h>
int main(void)
{
int a;
printf("\n Enter: ");
scanf("%d", &a);
return (0);
}
total: 0 errors, 1 warnings, 8 lines checked
c:2: warning: no description found for function main
After including the library, you ought to include a description of the program, like so:
#include <stdio.h>
/**
* main - Entry point
*
* Description: 'the program's description'
*
* Return: Always 0 (Success)
*/
int main(void)
{
Code goes here
}

Generating the same sequence of random number in a loop [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
A for loop to be exact. I've tried using srand() to seed the loop but it doesn't seem to be working. Could someone clarify the seeding part? Anyways, could someone point out my errors?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
int a,b,c,d,loop1=0,loop2=1,count=0;
srand(1);
while(loop1!=1)
{
a=rand()%10;
b=rand()%10;
c=rand()%10;
d=rand()%10;
printf("%d %d %d %d\n",a,b,c,d);
count+=1;
if (count>3)
{
break;
}
}
return 0;
}
The output should look like this:
1 2 3 4
1 2 3 4
1 2 3 4
You need to set the seed at the beginning of the loop if you want to have the same sequence in each iteration:
while(loop1!=1) {
srand(1);
a=rand()%10;
b=rand()%10;
c=rand()%10;
d=rand()%10;
printf("%d %d %d %d\n",a,b,c,d);
count+=1;
if (count>3) {
break;
}
}

Recursive values in 4th order RK method for projectile (quadratic drag) in C [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 4 years ago.
Improve this question
I am trying to code projectile motion of a shell using 4th order RK method. I am facing a problem in the following code.
for (i=1; i<=n; i=i+1)
{
ax[i]= (-kk[i]*v[i]*vx[i])/m;
ay[i]= ((-kk[i]*v[i]*vy[i])-(m*g))/m;
k1= ax[i];
m1= ay[i];
k2= (-kk[i]*v[i]*(vx[i]+(k1*delt/2)))/m;
m2= (-kk[i]*v[i]*(vy[i]+(m1*delt/2)))/m;
k3= (-kk[i]*v[i]*(vx[i]+(k2*delt/2)))/m;
m3= (-kk[i]*v[i]*(vy[i]+(m2*delt/2)))/m;
k4= (-kk[i]*v[i]*(vx[i]+(k3*delt)))/m;
m4= (-kk[i]*v[i]*(vy[i]+(m3*delt)))/m;
vx[i+1]= vx[i]+((1/6)*(k1+2*k2+2*k3+k4)*delt);
vy[i+1]= vy[i]+((1/6)*(m1+2*m2+2*m3+m4)*delt);
x[i+1]= vx[i+1]*delt;
y[i+1]= vy[i+1]*delt;
xx[i+1]= x[i+1] + xx[i];
yy[i+1]= y[i+1] + yy[i];
t[i+1]= t[i]+delt;
v[i+1]= sqrt((vx[i+1]*vx[i+1])+(vy[i+1]*vy[i+1]));
if ((yy[i+1]<0)&&(i!=1))
//if (i==100)
{
nn=i;
i=n;
}
printf ("%f\t%f\t%f\t%f\t%f\t%f\t\n", t[i], vx[i], vy[i], v[i], xx[i], yy[i]);
In the above code ax, ay, vx and vy are accelerations and velocity in x and y directions, delt represents the time step. xx and yy are the position of the corresponding time steps.
After using fourth order RK method I try to find the velocities in the next time step to obtain the corresponding values in the loop until the projectile hits the ground. But I am only getting the same values as of vx[1] and vy[1] for every corresponding iteration and repeats.I am a beginner in C programming and still at an initial stage of using loops.
The problem is here:
vx[i+1]= vx[i]+((1/6)*(k1+2*k2+2*k3+k4)*delt);
vy[i+1]= vy[i]+((1/6)*(m1+2*m2+2*m3+m4)*delt);
1/6 - both numbers are integers so this will result in 0 making your statements:
vx[i+1]= vx[i];
vy[i+1]= vy[i];
Make them floating point. And since you calculate often, make it a constant.
const double one_sixth = 1.0 / 6.0;
Then
vx[i+1]= vx[i]+(one_sixth * (k1+2*k2+2*k3+k4)*delt);
vy[i+1]= vy[i]+(one_sixth * (m1+2*m2+2*m3+m4)*delt);

Meltdown PoC Detailed Code Review [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I read all the week-end about Meltdown and Spectre
I also have already read the .pdfs for Spectre and Meltdown
which are Must Read for anyone seeking more knowledge about these exploits but unfortunately don't provide detailed explanations on the code.
I found various PoC on github, which were very interesting but I lack the knowledge to fully understand it. I would be thanksful about more explanation on specific parts:
From this link https://github.com/dendisuhubdy/meltdown/blob/master/src/poc.c , and other git repositories as well, there are many interesting parts in the conception of this exploit.
Time reads
/* Time reads. Order is lightly mixed up to prevent stride prediction */
for (i = 0; i < 256; i++) {
mix_i = ((i * 167) + 13) & 255;
addr = &array2[mix_i * 512];
time1 = __rdtscp(&junk); /* READ TIMER */
junk = *addr; /* MEMORY ACCESS TO TIME */
time2 = __rdtscp(&junk) - time1; /* READ TIMER & COMPUTE ELAPSED TIME */
if (time2 <= CACHE_HIT_THRESHOLD && mix_i != array1[tries % array1_size])
results[mix_i]++; /* cache hit - add +1 to score for this value */
}
why do we use prime numbers 167 and 13 ?
/* Locate highest & second-highest results results tallies in j/k */
Why do we care about getting the max value ?
Other parts explanations are very welcome as well !!

Resources