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

Write a function that takes an integer value and returns the number with its dig

ID: 3629860 • Letter: W

Question

Write a function that takes an integer value and returns the number with its digits reversed. For example, given the number 7631, the function should return 1367.

see the template

1. How would you rewrite reverseDigits to take its arguments pass-by-reference. It should no longer return a value; it should just modify the number it is passed.

2. How would you extend the function to work for inputs of size 5 (i.e 1 to 99999)? How about 3?

Explanation / Answer

#include "stdafx.h"//Remove unless in MS Visual Studio #include using namespace std; //pass by value version int reverseDigits(int num) { int newNum = 0; while(num > 0) { newNum *=10; newNum += num%10; num /= 10; } return newNum; } //pass by reference version void reverseDigits2(int *num) { int newNum = 0; while(*num > 0) { newNum *=10; newNum += *num%10; *num /= 10; } *num = newNum; } int main() { int num; cout > num; reverseDigits2(&num); cout
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