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

How can the following function prints all the given numbers vertically? Based on

ID: 3760530 • Letter: H

Question

How can the following function prints all the given numbers vertically?

Based on my tracing of function it can only prints first digit and the last digit using recursion.

#include <iostream>

using namespace std;

void recursive_write_vertical(unsigned int number)
// Postcondition: The digits of number have been written, stacked vertically.
{
if (number < 10) // stopping case
cout << number << endl; // Write the one digit
else // including recursive calls
{
cout << "Before: " << number << endl;
recursive_write_vertical(number/10);// Write all but the last digit
cout << "After: "<< number << endl;
cout << number % 10 << endl; // Write the last digit
}
}

int main()
{
recursive_write_vertical(1234);
return 0;
}

Explanation / Answer

#include <iostream>

using namespace std;

void recursive_write_vertical(unsigned int number)

// Postcondition: The digits of number have been written, stacked vertically.

{

if(number>0)

{

recursive_write_vertical(number/10);

cout << number%10 << endl;

}

}

int main()

{

recursive_write_vertical(1234);

return 0;

}

OP:

1

2

3

4

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