# Assignment 1 Semester 2 Question 3

Posted by core2000
Announcements Last Post
myUnisa availability 21 to 24 March 2019 03/17/2019 02:24PM
SoC Curricula 09/30/2017 01:08PM
Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
School of Computing Short Learning Programmes 11/24/2014 08:37AM
Unisa contact information 07/28/2011 01:28PM
 Assignment 1 Semester 2 Question 3 July 18, 2010 10:01AM Registered: 9 years ago Posts: 34 Rating: 0
I have completed the question and my code works but I just want to find out whether I interpreted it correctly.

The question say:

Write a recursive function reverseDigits that takes an integer as a parameter and returns the
number with the digits reversed. Also write a program to test your program.

I assume the number must be returned as an integer and cannot simply be converted to a string for aesy manipulation.

My problem though is that my recursive function takes 2 parameters. The number and the reverse of the number.

The prototype looks as follows:

`Language: C++int reverseDigits(int num, int rev);`

in the next step of my general case, it takes the number/10 and a temp variable which is calculated by reverse*10+the mod of the number.

my base case just returns the reverse if the number = 0.

I am a bit weary of posting the code here so if anyone can tell me whether my approach was correct, I can consider this problem solved.

Thanks
Chris

Chris Botha

=============================
Far and away the best prize that life has to offer is the chance to work hard at work worth doing.
Theodore Roosevelt
 Re: Assignment 1 Semester 2 Question 3 July 18, 2010 11:04AM Registered: 13 years ago Posts: 1,424 Rating: 0
I'm not registered for this course but if the question says you should have a parameter, then you should only have one. I don't understand why you want two though, there seems no reason for it. What do you need rev for?

If you use it to get the result of the calculation, it's redundant, you can simply return the result.
 Re: Assignment 1 Semester 2 Question 3 July 18, 2010 11:43AM Registered: 10 years ago Posts: 3,496 Rating: 1
Also, as long as it comes back as an integer, I very much doubt it matters that much what you do with it inside the function.

And if you handle it as a string you need not concern yourself with any of the peculiarities of computer arithmetic (even though they're unlikely to be troublesome in this particular case). I think I'll go for the string route when I get here.
 Re: Assignment 1 Semester 2 Question 3 July 19, 2010 03:03AM Registered: 9 years ago Posts: 34 Rating: 0
After getting some advice, I tried to fix the function to take only one parameter. I got the function to work, but I am still unsure about my base case.

robanaurochs kindly pointed out my previous base case

`Language: C++if (num != 0)`

was not quite testing the base case that the number only has one digit, which means numbers with one digit actually ran through twice.
I am unfortunately stuck. As it stands now, with only one parameter I have changed my base case to

`Language: C++if (num > 0)`

but I am still not convinced that it is a good base case. it seems to be the same as the previous one.

If I use
`Language: C++if (num/10 > 0)`
the program seems to work and it seems to test correctly. I just need some confirmation that this is in fact correct.

Thanks
Chris

Chris Botha

=============================
Far and away the best prize that life has to offer is the chance to work hard at work worth doing.
Theodore Roosevelt
 Re: Assignment 1 Semester 2 Question 3 July 19, 2010 10:39AM Registered: 13 years ago Posts: 1,424 Rating: 0
The function only needs to recurse if the input number has more than one digit. Since we use decimal numbers, the base case is simply:

```Language: C++if (num / 10 == 0)
return num; // no need to recurse a single-digit number```

This will handle all single-digit integers: negative, zero and positive.
Sorry, only registered users may post in this forum.