Hello

My program takes in a number & uses a recursive function to display that number backwards. eg input = 473, output= 374.

I have to submit the program for marking, but they always throw in some curve ball test to make sure that your program will work for all cases.

Can you take a look at my program and tell me if it will work for all numbers? Or can you see an exception when this algorithm wont work?

Our tutor actually gave us the answer :P but mine is different & I am trying to be sure my code works for all numbers/cases so I dont loose a mark again :P

// My way of doing it
void reverseDigits(int x)
// displays the int x with its digits in reverse order.
// recursive algorithm
{
    if (x>1) {        // will this work for all numbers??
        cout << x%10;
        reverseDigits(x/10);
    }
    
}

// My tutors example 
void reverseDigits2(int x) {
     
    cout << x%10;
    
    if (x>10) {
        reverseDigits2(x/10);
    }
}

whole program:

#include <iostream>
using namespace std;

void reverseDigits(int x);
void reverseDigits2(int x);

int main()
{
   int number;
   cout << "Enter a number : " << flush;
   if (cin >> number)
   {
       cout << number << " reversed is ";
       reverseDigits(number);
       cout << endl;
   }
   else
      cout << "Invalid input " << flush;
   system("pause");
   return 0;
}

// My way of doing it
void reverseDigits(int x)
// displays the int x with its digits in reverse order.
// recursive algorithm
{
    if (x>1) {        // will this work for all numbers??
        cout << x%10;
        reverseDigits(x/10);
    }
    
}

// My tutors example 
void reverseDigits2(int x) {
     
    cout << x%10;
    
    if (x>10) {
        reverseDigits2(x/10);
    }
}

Recommended Answers

All 3 Replies

if (x>1) { // will this work for all numbers??

cout << x%10;

reverseDigits(x/10);

}

put the x = 100 and do a dry run. This , then if (x >1 ) did not pass
when x=1 , for this you can say,
if ( x >0 ) , since there is 0 then there's nothing to print.


As far as I checked this is fine.You can also run a unit test if you
not sure. anyway one thing that I seen here is that /10 operation
costs more computing power. Typically a good performance algorithms are not implemented like this /2 is normally we using
as <<2 operation.and it's just an single clock cycle , but idiv is a
multiple sometimes 80 clock cycles. So avoid the division operations
and replace them with the right shift as everyplace you can.

What if x is 1 ?

Can you take a look at my program and tell me if it will work for all numbers? Or can you see an exception when this algorithm wont work?

If you want to be confident in your algorithm, you need to give it good tests before turning it in. The function reverses digits, so the testing is pretty easy. I would start with these test cases for 32 bit ints:

0
1
2
3
12
123
1234
12345
123456
1234567
12345678
123456789
1234567890
-1
-2
-3
-12
-123
-1234
-12345
-123456
-1234567
-12345678
-123456789
-1234567890 INT_MIN or numeric_limits<int>::min() INT_MAX or numeric_limits<int>::max() This covers all possible digit counts on both positive and negative numbers, it tests the edge cases of the largest and smallest possible value, and it tests values around your recursive base condition.

At the bare minimum, you should test average cases to make sure the algorithm works in general, and boundary cases to make sure that the algorithm is complete. If you can, write a function that exhaustively covers all cases, because that is a stronger test.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.