According to this program: #include \"stdafx.h\" #include <iostream> #include <i
ID: 3630443 • Letter: A
Question
According to this program:#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int reverse(int);
int main()
{
int number;
cout << "Enter a number between 1 and 9999: ";
cin >> number;
cout << "the number with its digits reversed is: ";
cout << reverse(number) << endl;
system ("pause");
}
int reverse(int n)
{
int reverse = 0;
int divisor = 1000;
int multiplier = 1;
while ( n > 9 )
{
if ( n >= divisor )
{
reverse += n / divisor * multiplier;
n %= divisor;
divisor/= 10;
multiplier*= 10;
}
else
divisor /= 10;
}
reverse += n * multiplier;
return reverse;
}
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?
Usually questions will call for a change of only one line of code, in this case provide the original line of code and how it should be changed to get the desired effect
Example:
How would you change code in main so that it reverses number twice (i.e., reverse it once, then reverse the result of the first function call). Do not store the value of number after it has been reversed once. Is the number that has been reversed twice equal to the original number?
Answer:
Change the reverse digits function call from
cout << reverseDigits( number ) << endl;
To
cout << reverseDigits( reverseDigits ( number ) ) << endl;
Explanation / Answer
Here are the line of code though it was written in the program u marked as need work.
void reverseDigits2(int *num)
{
int newNum = 0;
while(*num > 0)
{
newNum *=10;
newNum += *num%10;
*num /= 10;
}
*num = newNum;
}
int main()
{
int num;
cout << "Enter a number: ";
cin >> num;
reverseDigits2(&num);
cout << num;
return 0;
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.