Question: # include using namespace std; #define My_DEBUG 1 int getNumberOfDigits (int n); // returns how many digits in positive number n int* int2DynamicArray (int n,

 # include using namespace std; #define My_DEBUG 1 int getNumberOfDigits (int

# include

using namespace std;

#define My_DEBUG 1

int getNumberOfDigits (int n); // returns how many digits in positive number n

int* int2DynamicArray (int n, int &numDigits); // convert n to an dynamic array

int dynamicArray2int (int* intPointer, int numDigits);

int sortDescending (int* intPointer, int numDigits);

int sortAscending ( int* intPointer, int numDigits);

void printArray (int* p, int size);

void test();

void test () {

cout

int k;

int* p;

p = int2DynamicArray (34574, k)

printArray(p,k);

cout

cout

}

int main () {

int n = 1234; //4 digits positive number

int *p = NULL;

int numDigits = 0, descending, ascending;

int count = 0, m, blackHo1e = -1;

const int MAXCOUNT=50;

cout

while (n >= 10) {

cout

cin>>n;

m = n;

cout

while( count

p int2DynamicArray (m, numDigits);

//add code according to the definition of number black hole

blackHo1e = descending - ascending;

cout

delete [] p;

if (blackHo1e) {

break;

}

else{

m = blackHo1e ;

count++;

}

}

if (count == MAXCOUNT) {

cout

}

else{

cout

}

}

return 0;

}

int getNumberOfDigits (int n) { // returns how many digits in positive number n

if (n

return -1;

}

int numDigits = 0;

while (n > 0){

n = n/10; //integer division

numDigits++;

}

return numDigits;

}

// precondition: n >0. n=3898, return an array containing {3,8, 9, 8}

int* int2DynamicArray(int n, int &k){ //convert n to an dynamic array

k = getNumberOfDigits(n);

int *p = new int[k];

int m = k-1;

while ( n > 0) {

p[m--] = n%10;

n = n/10; //integer division

}

return p;

}

//2145 ---{2,1, 4, 5}

int dynamicArray2int (int* p, int numDigits) {

int n = 0;

int base = 1;

for(int i =numDigits - 1; i >= 0; i-- ) {

n += base * p [i];

base *= 10;

}

return n;

}

int sortDescending (int* p, int numDigits) {

int tmp;

for (int i=0; i

for (int j=i+l; j

if(p[i]

tmp = p[i];

p[i] = p[j];

p[j] = tmp;

}

}

}

return dynamicArray2int(p, numDigits);

}

int sortAscending (int* p, int numDigits) {

int tmp;

for (int 1=0; i

for(int j=i+l; j

if(p[i]> p[j]){

tmp = p[i];

p[i] = p[i];

p[j] = tmp;

}

}

return dynamicArray2int (p, numDigits);

}

void printArray(int* p, int size) {

cout

for(int i=0; i

cout

}

cout

}

Make an edit on the source program to run and show the running result using the instructions above.

Lets play a magic game. Pick any 4 digits integer, for example n = 1965. Perform following operations. Sort the digits in the number in descending order you will get m = 9651. Sort the digits in the number in ascending order you will get k = 1569. Let n = m-k = 9651-15698082. . After repeating above three steps certain times, you will find n = 6174, and it will stay 6174 for ever! In other words if you start above three steps with initial n = 6174, you will find n is still 6174 at step 3! We call it number black hole. Write a C++ program to demonstrate this. Your program should let user to be able to test it as many times as the user wants as long as the user enters a 4-digits positive integer and your program should quits if the user enters anything else

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!