|
-
Jan 29th, 2002, 03:11 PM
#1
Is this possible using recursion?
URGENT!
I must hand this in tomorrow.
We got this exercise a few days ago:
Write a function that receives 3 parameters:
A 4-digit integer, and two digits.
The function checks if the two digits appear next to each other on the left part of the number or on the right part of the number, but not in the middle. The order doesn't matter.
If the digits appear next to each other the function will print the number with the order of the digits reversed, else it will print the number without any changes.
examples:
function(1234, 3, 4) ---> 1243
function(7997, 9, 7) ---> 9779
function(1234, 2, 3) ---> 1234
The function MUST be recursive.
I've thought about it all day and I can't figure out how to do it using recursion.
Help!
-
Jan 29th, 2002, 04:20 PM
#2
Member
Just an idea...
Okay, this may not be what you're looking for but it's all I could think of. You could make the function that you described that accepts the three arguments and then immediately check to see if the two digits appear next to each other. If not (here's the recursion), swap the numbers of the four-digit number and call the function again. This time they should match up if there ever going to. I know it's weak, but it might work.
Psuedocode:
Function Header
Are the digits next to each other?
If no then call function again
If yes then reverse the digits appropiately and return number
End the Function
Just watch out to make sure the function will not call itself forever.
-
Jan 29th, 2002, 04:33 PM
#3
That's the problem, it doesn't make any sense to use recursion in this situation.
I thought about your idea already, but I don't think that recursion with only 2 calls is considered recursion. 
I have no idea what to do.
-
Jan 29th, 2002, 05:16 PM
#4
I'm sorry, I tried, but it just doesn't make any sense at all. Maybe you could make a recursive function to reverse the integer...
There is absolutely no way this thing can be made recursive. If you want my opinion, your teacher must be very dumb (like the one that insists on writing void main(), or maybe a little bit more).
You might want to post the solution here when you get it tomorrow.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 29th, 2002, 05:18 PM
#5
transcendental analytic
hmm, well it's not a very usefull algoritm I see, but make the function call itself, and you have made a useless one, but you have still met all the criterias on the excercise.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jan 29th, 2002, 05:24 PM
#6
Something like this:
PHP Code:
void func(int i, int j, int k)
{
if(i == 0)
{
if(k != 0)
func(0, 0, k-1);
return;
}
func(0, 0, 8); // trick recursion
// real code here
}
This will recur 9 levels deep
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 29th, 2002, 05:27 PM
#7
transcendental analytic
I wouldn't spend much extra time on that exercise 
PHP Code:
void func(int i, int j, int k)
{
// real code here
func(i, j, k); // blows up the stack
}
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jan 29th, 2002, 05:28 PM
#8
I can do something like:
PHP Code:
int Function(int num, int dig1, int dig2)
{
if (1 + 1 == 3)
Function(num+1, dig1-1, dig2-1);
// real code here...
}
-
Jan 29th, 2002, 05:29 PM
#9
transcendental analytic
or maybe you could put something like
PHP Code:
cout << "do it again?"
cin >> i;
if (i)func(i, j, k);
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jan 29th, 2002, 05:42 PM
#10
I'll post the answer as soon as I get it.
BTW, he is the kind of teacher that insists on void main().
-
Jan 29th, 2002, 07:58 PM
#11
Tell him hes an idiot =).
Try this one:
Code:
int func(int x, int y, int z)
{
//code here
return <answer>
func(x, y, z);
}
=).
Z.
-
Jan 30th, 2002, 08:46 AM
#12
You know, I slowly get the feeling that programming teachers are those programmers that are too bad to get a real programming job...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 30th, 2002, 09:35 AM
#13
Yeah. Its pretty sad. I got lucky to get a good teacher.
Z.
-
Jan 30th, 2002, 02:11 PM
#14
Turns out that was the solution, although I think it would have been smarter not to use recursion here:
PHP Code:
#include <iostream.h>
int SwitchDigits(int, int, int);
void main()
{
int num, d1, d2;
cout << "Enter number and digits: ";
cin >> num >> d1 >> d2;
cout << "Switched: " << SwitchDigits(num, d1, d2) << endl;
}
int SwitchDigits(int num, int dig1, int dig2)
{
int left, right;
if (num > 100)
{
left = SwitchDigits(num / 100, dig1, dig2);
right = SwitchDigits(num % 100, dig1, dig2);
return (100 * left + right);
}
if ((num == 10 * dig1 + dig2) ||
(num == 10 * dig2 + dig1))
return (10 * (num % 10) + (num / 10));
else
return num;
}
-
Jan 31st, 2002, 10:09 AM
#15
No, definitely not. This isn't really recursion, this is two functions in one (if number > 100 it calls the other function twice, else it acts like the other function)
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 31st, 2002, 11:16 AM
#16
transcendental analytic
And, it says explicitely in the exercise that the function takes in a 4-digit integer
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|