Welcome! Log In Create A New Profile

Advanced

Assignment 1 Semester 2 Question 3

Posted by core2000 
Announcements Last Post
Announcement : Programming Students at UNISA School of Computing 06/19/2019 02:01PM
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Assignment 1 Semester 2 Question 3
July 18, 2010 10:01AM
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
avatar Re: Assignment 1 Semester 2 Question 3
July 18, 2010 11:04AM
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.
avatar Re: Assignment 1 Semester 2 Question 3
July 18, 2010 11:43AM
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
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
avatar Re: Assignment 1 Semester 2 Question 3
July 19, 2010 10:39AM
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.

Click here to login