Ayuda con Recursiòn
Hola gente.
Tengo un pequeño problema que talvez parezca muy tonto pero no le encuetro soluciòn:
Tengo que hacer una funciòn que recursiva que devuelva un numero elevado a otro.
Tal como dice el libro la funciòn serìa power(base,exponente) y devuelve base (elevado a) exponente.
El problema es que no me puedo plantear ni siquiera el logaritmo de forma iterativa.
Aguien me podrìa dar una mano, no darme el còdigo o el programa echo porque realmente lo quiero hacer yo, pero si alguna pista porque estoy perdido. (con la parte iterativa)
Gracias y saludos
Tengo un pequeño problema que talvez parezca muy tonto pero no le encuetro soluciòn:
Tengo que hacer una funciòn que recursiva que devuelva un numero elevado a otro.
Tal como dice el libro la funciòn serìa power(base,exponente) y devuelve base (elevado a) exponente.
El problema es que no me puedo plantear ni siquiera el logaritmo de forma iterativa.
Aguien me podrìa dar una mano, no darme el còdigo o el programa echo porque realmente lo quiero hacer yo, pero si alguna pista porque estoy perdido. (con la parte iterativa)
Gracias y saludos
Te envio un programa que realiza operaciones aritmeticas con recursion.
Supongo que la funcion que te interesa es Power().
Probalo con valores bajos, para evitar overflow
Supongo que la funcion que te interesa es Power().
Probalo con valores bajos, para evitar overflow
// program k7a1.CPP - page 122
// recursion : a - sum(a,b)
// b - subtract(a,b)
// c - multiply(a,b)
// d - division(a,b)
// e - remainder(a,b)
// f - power(a,b)
// g - sum(a) = 1 + 2 + 3 + . . . a
// 7/9/2001
// written in Borland CPP ver 3.1
#include <conio.h>
#include <iostream.h>
#include <iomanip.h>
char *msg[] = {"Sum","Subtract","Multiply","Division","Remainder","Power"};
unsigned int GetNum(char x)
{
unsigned int num;
cout << setw(25) << x << " = ";
cin >> num;
return num;
} // GET NUM
void Swap(unsigned int &a,unsigned int &b)
{
unsigned int c = a;
a = b;
b = c;
} // SWAP INTEGERS
void Show(unsigned int op,unsigned int a,unsigned int b,unsigned int c)
{
if(op != 6)
cout << setw(20) << msg[op] << "(" << a << "," << b << ") = " << c;
else
cout << setw(12) << msg[0] << "(from 1 to " << a << ") = " << c;
cout << endl;
} // SHOW
unsigned int Sum(unsigned int a,unsigned int b)
{
if(!b)
return a;
return Sum(a,b-1) + 1;
} // SUM
unsigned int Sum(unsigned int a)
{
if(!a)
return a;
return Sum(a-1) + a;
} // SUM
unsigned int Subtract(unsigned int a,unsigned int b)
{
if(!b)
return a;
return Subtract(a,b-1) - 1;
} // SUBTRACT
unsigned int Multiply(unsigned int a,unsigned int b)
{
if(b == 1)
return a;
return Sum(Multiply(a,b-1),a);
} // MULTIPLY
unsigned int Division(unsigned int a,unsigned int b)
{
if(a < b)
return 0;
return Sum(Division(a-b,b),1);
} // DIVISION
unsigned int Remainder(unsigned int a,unsigned int b)
{
if(a < b)
return a;
return Remainder(a-b,b);
} // DIVISION
unsigned int Power(unsigned int a,unsigned int b)
{
if(!b)
return 1;
return Multiply(Power(a,b-1),a);
} // MULTIPLY
void main()
{
clrscr();
cout << "recursives functions." << endl << endl;
unsigned int a = GetNum('a');
unsigned int b = GetNum('b');
cout << endl;
if(a < b)
Swap(a,b);
Show(0,a,b,Sum(a,b));
Show(1,a,b,Subtract(a,b));
Show(2,a,b,Multiply(a,b));
Show(3,a,b,Division(a,b));
Show(4,a,b,Remainder(a,b));
Show(5,a,b,Power(a,b));
Show(6,a,a,Sum(a));
cout << endl << "end of program - good bye ! ! !" << endl;
getch();
} // MAIN
/*
recursives functions.
a = 6
b = 3
Sum(6,3) = 9
Subtract(6,3) = 3
Multiply(6,3) = 18
Division(6,3) = 2
Remainder(6,3) = 0
Power(6,3) = 216
Sum(from 1 to 6) = 21
end of program - good bye ! ! !
*/
// recursion : a - sum(a,b)
// b - subtract(a,b)
// c - multiply(a,b)
// d - division(a,b)
// e - remainder(a,b)
// f - power(a,b)
// g - sum(a) = 1 + 2 + 3 + . . . a
// 7/9/2001
// written in Borland CPP ver 3.1
#include <conio.h>
#include <iostream.h>
#include <iomanip.h>
char *msg[] = {"Sum","Subtract","Multiply","Division","Remainder","Power"};
unsigned int GetNum(char x)
{
unsigned int num;
cout << setw(25) << x << " = ";
cin >> num;
return num;
} // GET NUM
void Swap(unsigned int &a,unsigned int &b)
{
unsigned int c = a;
a = b;
b = c;
} // SWAP INTEGERS
void Show(unsigned int op,unsigned int a,unsigned int b,unsigned int c)
{
if(op != 6)
cout << setw(20) << msg[op] << "(" << a << "," << b << ") = " << c;
else
cout << setw(12) << msg[0] << "(from 1 to " << a << ") = " << c;
cout << endl;
} // SHOW
unsigned int Sum(unsigned int a,unsigned int b)
{
if(!b)
return a;
return Sum(a,b-1) + 1;
} // SUM
unsigned int Sum(unsigned int a)
{
if(!a)
return a;
return Sum(a-1) + a;
} // SUM
unsigned int Subtract(unsigned int a,unsigned int b)
{
if(!b)
return a;
return Subtract(a,b-1) - 1;
} // SUBTRACT
unsigned int Multiply(unsigned int a,unsigned int b)
{
if(b == 1)
return a;
return Sum(Multiply(a,b-1),a);
} // MULTIPLY
unsigned int Division(unsigned int a,unsigned int b)
{
if(a < b)
return 0;
return Sum(Division(a-b,b),1);
} // DIVISION
unsigned int Remainder(unsigned int a,unsigned int b)
{
if(a < b)
return a;
return Remainder(a-b,b);
} // DIVISION
unsigned int Power(unsigned int a,unsigned int b)
{
if(!b)
return 1;
return Multiply(Power(a,b-1),a);
} // MULTIPLY
void main()
{
clrscr();
cout << "recursives functions." << endl << endl;
unsigned int a = GetNum('a');
unsigned int b = GetNum('b');
cout << endl;
if(a < b)
Swap(a,b);
Show(0,a,b,Sum(a,b));
Show(1,a,b,Subtract(a,b));
Show(2,a,b,Multiply(a,b));
Show(3,a,b,Division(a,b));
Show(4,a,b,Remainder(a,b));
Show(5,a,b,Power(a,b));
Show(6,a,a,Sum(a));
cout << endl << "end of program - good bye ! ! !" << endl;
getch();
} // MAIN
/*
recursives functions.
a = 6
b = 3
Sum(6,3) = 9
Subtract(6,3) = 3
Multiply(6,3) = 18
Division(6,3) = 2
Remainder(6,3) = 0
Power(6,3) = 216
Sum(from 1 to 6) = 21
end of program - good bye ! ! !
*/