Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Use recursion to complete the function oddDownEvenUp() so it first prints al

ID: 660939 • Letter: C

Question

C++

Use recursion to complete the function oddDownEvenUp() so it first prints all odd integers from largest odd number <= argument down to 1. The same function call must then print all the even integers in the same range of 2 through the largest even integers <= argument. The following function calls shown to the left must generate the output shown in comments in the right. Do not use any loop anywhere, use recursion.

oddDownEvenUp(1); // 1

oddDownEvenUp(2); // 1 2

oddDownEvenUp(4); // 3 1 2 4

oddDownEvenUp(9); // 9 7 5 3 1 2 4 6 8

Explanation / Answer

int oddDownEvenUp(int n){
   if (n > 0)
   {
   if (n % 2 == 1) cout << n << " ";
   oddDownEvenUp(n-1);
   if(n % 2 == 0) cout << n << " ";
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote