C++: For this project, you will practice decomposing a problem into functions. I
ID: 3595866 • Letter: C
Question
C++: For this project, you will practice decomposing a problem into functions. In the final part, you will also get some practice with pass-by-reference functions. As part of this lab is to also gain experience with integer operations, you are NOT allowed to convert numbers into strings and use string processing primitives. TASK 0 Consider the standard decimal representation of a non-negative integer num. You wish to write a function that repetitively adds up the digits of a number until it eventually results in a single-digit number. For example, given the number 234567, the first iteration results in 27 (=2+3+4+5+6+7), and the 2nd iteration results in 9 (=2+7). Since 9 is a single-digit number, no more iterations are needed and 9 is returned. Write a function with the following prototype to do this: // Precondition: num > 0 // Postcondition: the return value is the iterated sum of digits of num int sumDigits(int num); To do this, we first need to identify functions that are useful to doing this. In this case, a list of useful function prototypes might be: int getDigit(int num, int index); // return the index'th digit of num int numDigits(int num); // return the number of digits in num You should have at least these three functions, though you may have additional functions if you wish. Don't forget to write pre/post conditions for these functions. You may use library functions defined in cmath (such as pow) if you wish, though you don't need to.
TASK 1 You need a driver program to test your above program. To do this, write a main function that prompts for and input num, calls sumdigits and outputs the result. You may assume that the input is a positive number and do not need to do any error checking (but realize that you would have to for a real program).
TASK 2 A well-known theorem of math states that the above sum is 9 iff the number is divisible by 9. Write a function that modifies its argument number so that its divisible by 9. The function should add something to the right-most digit of the number if possible; otherwise it should subtract something from that digit. An example use of this function might be: transformNum(n); cout << n; // prints 234567 if n was originally 234565 Write the transformNum function. You should be able to reuse most of the code from earlier. Also modify the driver program to output the transformed number.
Explanation / Answer
#include<iostream>
using namespace std;
int sumDigits(int num);
int getDigit(int num, int index); // return the index'th digit of num
int numDigits(int num); // return the number of digits in num
int transformNum(int n);
int main()
{
cout << "Sum of digits of number 234565 = " << sumDigits(234565) << endl;
cout << "transormed number of 234565: " << transformNum(234565) << endl;
cout << "transormed number of 234560: " << transformNum(234560) << endl;
cout << "transormed number of 234570: " << transformNum(234570) << endl;
cout << "transormed number of 234555: " << transformNum(234555) << endl;
}
int sumDigits(int num)
{
int n = numDigits(num);
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += getDigit(num, i);
}
return sum;
}
int getDigit(int num, int index) // return the index'th digit of num
{
int rem;
do
{
rem = num % 10;
num /= 10;
--index;
} while (index >=0);
return rem;
}
int numDigits(int num) // return the number of digits in num
{
int count = 0;
do
{
num /= 10;
++count;
} while (num > 0);
return count;
}
int transformNum(int n)
{
int min1 = 0, min2, sum,rem,sum1;
sum = sumDigits(n);
rem = sum % 9;
if (rem != 0)
{
for (int i = 1; i < 10; i++)
{
sum1 = sum+i;
if (sum1 % 9 == 0)
{
min1 = i;
break;
}
}
for (int i = 1; i < 10; i++)
{
sum1 = sum - i;
if (sum1 % 9 == 0)
{
min2 = i*-1;
break;
}
}
}
//to find least divisile number minimum number is to be subtracted or added to make it divisible by 9
if (min1 < abs(min2))
return n + min1;
else
return n + min2;
}
--------------------------------------------------------------------------
/*output
Sum of digits of number 234565 = 25
transormed number of 234565: 234567
transormed number of 234560: 234558
transormed number of 234570: 234567
transormed number of 234555: 234558
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.