Hi, I need help trying to figure out how to recursively modify my program. The p
ID: 3550158 • Letter: H
Question
Hi, I need help trying to figure out how to recursively modify my program. The problem I'm hacing is the the program is not looping correctly and also not printing the correct number. I've calculated the payoff correctly, also I've only been able to print the first section of R3. I can't figure out how to loop it to get R2 to stay at 2 then go to 3 after all possiblities of R2 at 2. Enventually, R1 will change to 2 then 3; 3 being the highest number earned.
The result of the program should look like:
R1 R2 R3
1 1 1 payoff is 1
1 1 2 .............. 1
1 1 3 ............... 1
1 2 1 ................... 1
1 2 2 .................... 1
...
...
...
3 3 2 ................. 5
this is what I have so far:
#include <stdio.h>
#include <stdlib.h>
int payOff(int r1, int r2, int r3);
void loopR3(int R3, int upto);
void loopR2(int R2, int upto);
int main(void)
{
loopR3(1,3);
loopR2(1,3);
return 0;
}
/*DID NOT INCLUDE PAYOFF AS IT IS IRRELEVENT FOR THE QUESTION*/
void loopR3(int R3, int upto)
{
int R1 =1;
int R2 =1;
if (R3 > upto){
return loopR2(R2, upto);
}//end if
else{
printf("%d %d %d payoff is %d ", R1, R2, R3, payOff(R1,R2, R3));
loopR3(R3+1, upto);
}//end else
}
void loopR2(int R2, int upto)
{
if (R2<=upto){
loopR3(R2+1,upto);
printf("R2 is %d ", R2);
}
}
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
int payOff(int r1, int r2, int r3);
void loopR1(int R1, int upto);
void loopR2(int R1, int R2, int upto);
void loopR3(int R1,int R2, int R3, int upto);
int main(void)
{
loopR1(1,3);
return 0;
}
void loopR1(int R1, int upto)
{
while(R1<=upto)
{
loopR2(R1,1,upto);
R1++;
}
}
void loopR2(int R1, int R2, int upto)
{
while (R2<=upto)
{
loopR3(R1, R2, 1,upto);
R2++;
}
}
void loopR3(int R1,int R2, int R3, int upto)
{
while (R3 <= upto)
{
printf("%d %d %d payoff is %d ", R1, R2, R3, payOff(R1,R2, R3));
R3++;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.