Click to See Complete Forum and Search --> : Change into something more of a recursive code snip


Napaseredak
June 11th, 2004, 10:00 PM
for(int i=0;i<10;i++)
for(int j=0;j<10;j++){
if(i==j)
Dosomething();
else
Dosomethingelse();
}

Thanks in advance

-Yaskatze

Napaseredak
June 11th, 2004, 10:02 PM
I know this sounds quite dumb a question I myself made but please help, i am new to C++ programming, my background is actually from BASIC.
thanks

Kheun
June 13th, 2004, 10:09 PM
Rather than writing the code for you let me try to analyze the problem for you.

If you look at your source code closely, if you convert it into a single loop (let say using k for the loop variable), you may notice that your code is, in effect, looping from 00 to 99. Comparing the variables i and j to k, the digit in tenths position of k is equivalent to i while the digit in ones position is equivalent to j. In other words, i == k/10 and j == k%10. So when you need to do the comparsion (i == j), you can easily do (k/10 == k%10). As for the recursion part, you need to return when the 'k' variable is equal or more than 99.

Hope this helps.

mehdi62b
September 3rd, 2004, 06:55 AM
if you want to change this code and solve your problem through a recursive function do it like below


const int k=10;
bool doORnot(int i,int j)
{
if(i==j==0) return true;
if ((i==0)&&(j!=0) ||(i!=0)&&(j==0) ) return false;
if(doORnot(i-1,j-1)) return true;
return false;
}


HtH.
------------------
Mehdi.:)

DeepButi
September 3rd, 2004, 10:16 AM
Just a side question, but, why would you ever want to change into recursive?
Recursivity is perfect for theorical problems but very time consuming for real problems ... and people usually wants the opposite: how to change recursivity to iterations to get a dramatical gain in performance.

RoboTact
September 3rd, 2004, 04:07 PM
Rather than writing the code for you let me try to analyze the problem for you.

If you look at your source code closely, if you convert it into a single loop (let say using k for the loop variable), you may notice that your code is, in effect, looping from 00 to 99. Comparing the variables i and j to k, the digit in tenths position of k is equivalent to i while the digit in ones position is equivalent to j. In other words, i == k/10 and j == k%10. So when you need to do the comparsion (i == j), you can easily do (k/10 == k%10). As for the recursion part, you need to return when the 'k' variable is equal or more than 99.

Hope this helps.Do you really think that programms should be complicated as possible and forced to run 40x slower?

mehdi62b
September 3rd, 2004, 04:34 PM
Just a side question, but, why would you ever want to change into recursive?
Recursivity is perfect for theorical problems but very time consuming for real problems ... and people usually wants the opposite: how to change recursivity to iterations to get a dramatical gain in performance.
consider some of teachers they enjoy bothering thier students with such questions in final examinations,although they often cant solve themselves questions :D

-------------
Mehdi.:)

Kheun
September 5th, 2004, 09:02 PM
Do you really think that programms should be complicated as possible and forced to run 40x slower?
You are very correct. In most cases, recursive function should be avoided to improve the performance. We need recursive only in very rare situation.