Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Recursividad
Se dice que una funcin es recursiva cuando se define en funcin de si misma. No todas las funciones
pueden llamarse a s mismas, deben estar diseadas especialmente para que sean recursivas, de otro
modo podran conducir a bucles infinitos, o a que el programa termine inadecuadamente. C++ permite
la recursividad. Cuando se llama a una funcin, se crea un nuevo juego de variables locales, de este
modo, si la funcin hace una llamada a s misma, se guardan sus variables y parmetros en la pila, y
la nueva instancia de la funcin trabajar con su propia copia de las variables locales, cuando esta
segunda instancia de la funcin retorna, recupera las variables y los parmetros de la pila y continua la
ejecucin en el punto en que haba sido llamada.
CODIGO EN C++
package ejercicio13; }
import java.math.BigInteger; int temp;
class permutaciones { int j = a.length - 2;
private int[] a; while (a[j] > a[j + 1]) {
private BigInteger numLeft; j--;
private BigInteger total; }
public permutaciones(int n) { int k = a.length - 1;
if (n < 1) { while (a[j] > a[k]) {
throw new IllegalArgumentException("Min k--;
1"); }
} temp = a[k];
a = new int[n]; a[k] = a[j];
total = getFactorial(n); a[j] = temp;
reset(); int r = a.length - 1;
} int s = j + 1;
public void reset() { while (r > s) {
for (int i = 0; i < a.length; i++) { temp = a[s];
a[i] = i; a[s] = a[r];
} a[r] = temp;
numLeft = new BigInteger(total.toString()); r--;
} s++;
public BigInteger getNumLeft() { }
return numLeft; numLeft =
} numLeft.subtract(BigInteger.ONE);
public BigInteger getTotal() { return a;
return total; }
}
public boolean hasMore() { public static void main(String[] args) {
return int[] indices;
numLeft.compareTo(BigInteger.ZERO) == 1; String[] elements = { "A", "B", "C", "D","E" };
} permutaciones x = new
private static BigInteger getFactorial(int n) { permutaciones(elements.length);
BigInteger fact = BigInteger.ONE; StringBuffer permutation;
for (int i = n; i > 1; i--) { while (x.hasMore()) {
fact = fact.multiply(new permutation = new StringBuffer();
BigInteger(Integer.toString(i))); indices = x.getNext();
} for (int i = 0; i < indices.length; i++) {
return fact;
} permutation.append(elements[indices[i]]);
public int[] getNext() { }
if (numLeft.equals(total)) { System.out.println(permutation.toString());
numLeft = }
numLeft.subtract(BigInteger.ONE); }
return a; }
DERIVADA
Este programa resuelve la derivada del siguiente polinomio: ax4 +bx3 + cx2 + dx + e.
CODIGO EN C++
// 4 3 2
//Desarrollo de derivadas de la forma ax + bx + cx + dx + e
#include<iostream> }
#include<math.h> double primer( double a )
using namespace std; {
double primer( double ); double s=pow(l,3);
double segundo( double); double q=(a*s)*4;
double tercer(double); return q;
double cuarto(double ); }
double quinto(double ); double segundo( double b )
double a,b,c,d,j; {
double l; double t=pow(l,2);
double h,m; double r=(b*t)*3;
void main() return r;
{ }
cout<<" 4 3 2 "<<endl; double tercer( double c )
cout<<"Derivada de la forma ax + bx + {
cx + dx + e"<<endl; double u=pow(l,1);
cout<<"Ingrese valor a: "; double p=(c*u)*2;
cin>>a; return p;
cout<<"Ingrese valor b: "; }
cin>>b; double cuarto( double d )
cout<<"Ingrese valor c: "; {
cin>>c; double w=pow(l,0);
cout<<"Ingrese valor d: "; double v=(d*w)/1;
cin>>d; return v;
cout<<"Ingrese valor e: "; }
cin>>j; double quinto( double j )
cout<<"Ingrese el valor a derivar: "; {
cin>>l; double g=0;
h = primer(a)+ segundo(b)+ tercer(c)+ double h=(j*g);
cuarto(d)+quinto(j); return h;
cout<<"Valor de derivada: "<<h<<endl; }
CODIGO EN JAVA
}
private double cuarto(double d){
return d; java.util.logging.Logger.getLogger(Problema15
} .class.getName()).log(java.util.logging.Level.SE
private double quinto(double e){ VERE, null, ex);
}
return 0; //</editor-fold>
}
/* Create and display the form */
try { java.awt.EventQueue.invokeLater(new
for Runnable() {
(javax.swing.UIManager.LookAndFeelInfo info : public void run() {
javax.swing.UIManager.getInstalledLookAndFe new Problema15().setVisible(true);
els()) { }
if ("Nimbus".equals(info.getName())) { });
}
javax.swing.UIManager.setLookAndFeel(info.g
etClassName()); // Variables declaration - do not modify//GEN-
break; BEGIN:variables
} private javax.swing.JButton btnLimpiar;
} private javax.swing.JButton btnOperar;
} catch (ClassNotFoundException ex) { private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
java.util.logging.Logger.getLogger(Problema15 private javax.swing.JLabel jLabel2;
.class.getName()).log(java.util.logging.Level.SE private javax.swing.JLabel jLabel3;
VERE, null, ex); private javax.swing.JLabel jLabel4;
} catch (InstantiationException ex) { private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
java.util.logging.Logger.getLogger(Problema15 private javax.swing.JLabel jLabel7;
.class.getName()).log(java.util.logging.Level.SE private javax.swing.JLabel jLabel8;
VERE, null, ex); private javax.swing.JLabel jLabel9;
} catch (IllegalAccessException ex) { private javax.swing.JTextField txtA;
private javax.swing.JTextField txtB;
java.util.logging.Logger.getLogger(Problema15 private javax.swing.JTextField txtC;
.class.getName()).log(java.util.logging.Level.SE private javax.swing.JTextField txtD;
VERE, null, ex); private javax.swing.JTextField txtE;
} catch private javax.swing.JTextField txtResultado;
(javax.swing.UnsupportedLookAndFeelExcepti // End of variables declaration//GEN-END:
on ex) { variables
}
INTEGRAL
CODIGO EN C++
#include<iostream> cin>>b;
#include<math.h> cout<<endl<<endl;cout<<endl;
using namespace std;
main () delta = (b-a)/n;
{ s=0;
cout<<endl<<endl;
cout<<" INTEGRALES for (i=1;i<=n;i++)
DEFINIDAS"<<endl; {
s = s + sin(a +
cout<<endl<<endl; cout<<endl; delta * i) * cos(a + delta * i) * delta;
cout<<" }
___________________________________"<<
endl;
cout<<" | b |"<<endl;
cout<<" | ( |"<<endl; cout<<"la integral es :";
cout<<" | ) seno(x)coseno(x)dx | cout<<endl<<endl;
"<<endl; cout<<"
cout<<" | a | "<<endl; ___________________________________
cout<<" "<<endl;
|___________________________________|"< cout<<" | b |"<<endl;
<endl; cout<<" | ( |"<<endl;
cout<<endl; cout<<endl; cout<<" | ) seno(x)coseno(x)dx |
double s,a,b,n,i,delta; = "<<s<<endl;
cout <<"ingrese n el numero de cout<<" | a |"<<endl;
particiones = "; cout<<"
cin>>n; |___________________________________|"<
cout <<"ingrese a el limite inferior = "; <endl;
cin>>a; cout<<endl; cout<<endl;
cout <<"ingrese b el limite superior = "; }
CODIGO EN JAVA