Professional Documents
Culture Documents
Tema 6:
Programacin Modular:
Funciones y Procedimientos
Problemas
Profesor
John Ledgard Trujillo Trejo
#include<iostream>
#include<conio.h>
using namespace std;
double potencia (float b, int n);
int main()
{
float base;
int exponente;
cout<<"Elevar una numero a la potencia n\n";
cout<<"Ingrese la base: ";
cin>>base;
do
{
cout<<"Ingrese el exponente: ";
cin>>exponente;
}while(exponente<0);
Lenguaje de Programacin
John Ledgard Trujillo Trejo
06 de noviembre de 2017
Problema 1
if (n == 0)
return (resultado);
return(resultado);
}
Lenguaje de Programacin
John Ledgard Trujillo Trejo
06 de noviembre de 2017
Problema 2
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int posicion;
long int fibo;
do
{
cout<<"Ingrese la posicion del fibonacci: ";
cin>>posicion;
}while(posicion<=0);
Lenguaje de Programacin
John Ledgard Trujillo Trejo
06 de noviembre de 2017
Problema 2
fibo = numfibonacci(posicion);
cout<<"El numero Fibonacci en la posicion "<<posicion<<" es: "<<fibo;
getch();
return 0;
}
if ((pos == 1)||(pos==2))
return(1);
else
{
num1 = 1;
num2 = 1;
Lenguaje de Programacin
John Ledgard Trujillo Trejo
06 de noviembre de 2017
Problema 2
contador = 2;
return (suma);
}
Lenguaje de Programacin
John Ledgard Trujillo Trejo
06 de noviembre de 2017
Problema 3
#include<iostream>
#include<conio.h>
using namespace std;
int cuenta_cifras(int num);
int main()
{
int numero, numero_cifras;
cout<<"CUENTA CIFRAS DE UN NUMERO POSITIVO";
do
{
cout<<"\nEscribe un numero positivo: ";
cin>>numero;
}while(numero<=0);
Lenguaje de Programacin
John Ledgard Trujillo Trejo
06 de noviembre de 2017
Problema 3
numero_cifras = cuenta_cifras(numero);
cout<<"\nEl numero "<<numero<<" tiene "<<numero_cifras<<" cifras";
getch();
return 0;
}
while (num>0)
{
num = num/10;
cifras = cifras + 1;
}
return (cifras);
}
Lenguaje de Programacin
John Ledgard Trujillo Trejo
06 de noviembre de 2017
Problema 4
Algoritmo invertir_numero
var
entero: numero, numeroi
caracter: op
inicio // Principal
escribir ("INVIERTE UN NUMERO POSITIVO)
repetir
repetir
escribir ("Escribe un numero positivo: )
leer (numero)
hasta_que (numero>0)
numeroi numero_invertido(numero)
escribir (El numero invertido es , numeroi)
escribir ("Quiere seguir calculando: (s/n))
leer (op)
hasta que (op<>n')
fin
Lenguaje de Programacin
John Ledgard Trujillo Trejo
06 de noviembre de 2017
Problema 4
inicio
invertido 0
mientras (num>0)
resto num%10
num num/10
invertido invertido*10 + resto
fin_mientras
devolver (invertido)
fin_funcion
Lenguaje de Programacin
John Ledgard Trujillo Trejo
06 de noviembre de 2017
Problema 5
Lenguaje de Programacin
John Ledgard Trujillo Trejo
06 de noviembre de 2017
Problema 5
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
using namespace std;
main()
{
enum Estado { CONTINUAR, GANA, PIERDE };
int suma, puntaje;
Estado EstadoJuego;
Lenguaje de Programacin
John Ledgard Trujillo Trejo
06 de noviembre de 2017
Problema 5
switch ( suma )
{
case 7:
case 11: // gana al primer lanzamiento
EstadoJuego = GANA;
break;
case 2:
case 3:
case 12: // pierde al primer lanzamiento
EstadoJuego = PIERDE;
break;
default: //Continua el juego
EstadoJuego = CONTINUAR;
puntaje = suma;
cout << "El puntaje es " << puntaje << endl;
break; // opcional
}
Lenguaje de Programacin
John Ledgard Trujillo Trejo
06 de noviembre de 2017
Problema 5
if ( EstadoJuego == GANA )
cout << "El jugador gana" << endl;
else
cout << "El jugador pierde" << endl;
getch();
}
Lenguaje de Programacin
John Ledgard Trujillo Trejo
06 de noviembre de 2017
Problema 5
Lenguaje de Programacin
John Ledgard Trujillo Trejo
06 de noviembre de 2017
Problema 6
#include <iostream>
#include<conio.h>
using namespace std;
int bisiesto(int); //declaracin o prototipo de la funcin
int main()
{
int anio;
cout<<"Introduce a"<<(char)164<<"o: "; //164 ascii de
cin >> anio;
getch();
return();
}
Lenguaje de Programacin
John Ledgard Trujillo Trejo
06 de noviembre de 2017
Problema 6
Lenguaje de Programacin
John Ledgard Trujillo Trejo
06 de noviembre de 2017
Problema 7
Hacer un programa C++ que lee la fecha actual y la fecha de nacimiento de una
persona y calcule su edad.
#include <iostream>
#include<conio.h>
using namespace std;
int bisiesto(int);
int fecha_valida(int , int, int);
int calcular_edad(int, int, int, int, int, int);
int main()
{
int diaa, mesa, anioa, dian, mesn, anion, edad;
do
{
cout << "Introduce fecha actual: " << endl;
cout << "dia : "; cin >> diaa;
cout << "mes : "; cin >> mesa;
cout << "a" << (char)164 << "o: "; cin >> anioa;
}while(!(fecha_valida(diaa, mesa, anioa)));
Lenguaje de Programacin
John Ledgard Trujillo Trejo
06 de noviembre de 2017
Problema 7
do
{
cout << endl << "Introduce fecha de nacimiento: " << endl;
cout << "dia : "; cin >> dian;
cout << "mes : "; cin >> mesn;
cout << "a" << (char)164 << "o: "; ; cin >> anion;
}while(!(fecha_valida(dian, mesn, anion)));
int calcular_edad(int da, int ma, int aa, int dn, int mn, int an)
{
int edad = aa - an;
if(ma < mn)
edad--;
else if(ma == mn and da < dn)
edad--;
return edad;
}
Lenguaje de Programacin
John Ledgard Trujillo Trejo
06 de noviembre de 2017
Problema 6
switch(m)
{
case 4:
case 6:
case 9:
case 11: if(d > 30)
return 0;
break;
Lenguaje de Programacin
John Ledgard Trujillo Trejo
06 de noviembre de 2017
Problema 7
case 2: if(bisiesto(a))
{
if(d > 29)
return 0;
}
else if(d > 28)
return 0;
break;
return 1;
}
Lenguaje de Programacin
John Ledgard Trujillo Trejo
06 de noviembre de 2017
Problema 8
Lenguaje de Programacin
John Ledgard Trujillo Trejo
06 de noviembre de 2017
Bibliografa
1.22
Preguntas ?
1.23