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

I NEED ONLY PART 1 FOR NOW BUT FEEL FREE TO DO OTHER PARTS IF YOU WANT. PLEASE R

ID: 3596457 • Letter: I

Question

I NEED ONLY PART 1 FOR NOW BUT FEEL FREE TO DO OTHER PARTS IF YOU WANT. PLEASE READ THE DIRECTION AND PART 1 CAREFULLY BEFORE YOU START. IF YOU ARE NOT SURE WHAT TO DO, PLEASE SKIP MY QUESTION. I WILL GIVE NEGATIVE RATING FOR WRONG OR INCOMPLETE ANSWER.

THANK YOU.

NOTE:

Sorry for the messy template. Believe or not, this what I'm given by the instructor.

DIRECTION:

PLEASE IMPLEMENT TO PART 1 TO THE CODE TEMPLATE GIVEN BELOW. YOU ARE NOT ALLOWED TO CONVERT NUMBERS INTO STRINGS AND STRING PROCESSING PRIMITIVES.

PART 1

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.

PART 2

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).

PART 3

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.

// TEMPLATE STARTS HERE

#include <iostream>

#include <cmath>

using namespace std;

/* part 1: 1-th = 1, 2-th =2, 33-th=3....5-th=5;

part 2 : ( 1+2 = 3) or (2+3=5), (a+b) < 10

// easy way to do that

   : numberIndex(1) = 1

: numberIndex(2) = 2

part 3: int number = 8;

   number % 9 = 8;

   change the value of number, so number % 9 ==0;

*/

int number Index(int index) {

   // return the index-th digit

void part1and2() {

   // 1. ask to input a number;

   int number;

   int len;

   // a. get len of the number 12345, lend =5

   // b. cout 1-th, 2th... len-th

   // c. cout pair of a-th + b-th < 10

}

voud part(3) {

int number;

   // ask to input a number;

   r = number % 9;

   // a. change the value of number; so make it number % 9 == 0

   // number = 19, 19 % 0 = 1, chnge : number =18, cout 18

   cout << number << endl;

int option = 1;

   while (cin>>option) {

   // cin>> option;

if (option == 0) return 0;

if (optopn == 1) return1and2();

if (optopn == 2) part3();

}

Explanation / Answer

//functions for part1 are below

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;

}

----------------------------------------------

//driver program to test above functions

#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 main()

{

cout << "Sum of digits of number 234565 = " << sumDigits(234565) << endl;

cout << "Sum of digits of number 234 = " << sumDigits(234) << endl;

}

-----------------------------------------------------

//output

Sum of digits of number 234565 = 25
Sum of digits of number 234 = 9