You are on page 1of 16

punteros del libro

//YAMADA POR VALOR


#include <cstdlib>
#include <iostream>
using namespace std;
int cubebyvalue(int);
int main(int argc, char *argv[])
{
int number=5;
cout<<"el valor original del numero es "<< number;
number=cubebyvalue(number);
cout<<"el nuevo valor de number es"<<number<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
int cubebyvalue(int n)
{ return n*n*n;
}
________________________________________________________________________________
_____________
//YAMADA POR REFERENCIA
#include <cstdlib>
#include <iostream>
using namespace std;
void cubebyreference(int *);
int main(int argc, char *argv[])
{
int number=5;
cout<<"el valor original del numero es "<< number;
cubebyreference(&number);
cout<<"el nuevo valor de number es"<<number<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
void cubebyreference(int *nptr)
{ *nptr = *nptr * *nptr * *nptr;
}
-------------------------------------------------------------------------------------------------_____
arreglos

#include <cstdlib>
#include <iostream>
#include <iomanip.h>
using namespace std;
int main()
{

int max[10]={23,45,67,89,23,4,12,89,56,45} ;
cout<<"elementto"<<setw (13)<<"valor"<<endl; //no olvidar que cuando se usa
setttw la biblioteca iomanip
for(int i=0; i<10;i++)
cout<< setw( 7 )<<i<<setw(13)<< max[i]<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
________________________________________________________________________________
_____________________________
NUMEROS ALEATORIOS CHINGON

#include <cstdlib>
#include <iostream>
#include <iomanip.h>
using namespace std;
int main()
{ unsigned seed;
cout<< "tecle la semilla";
cin>>seed;
srand(seed);
for( int i=1;i<=10; i++)
{
cout<<setw(10)<<1+rand()%6;
if(i%5==0)
cout << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
________________________________________________________________________________
_______________________________
est control

#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
double total,gradecounter,grade,average;
total=0;
gradecounter =1;
while (gradecounter<=10)
{

cout<<"inserte la calificacion";
cin>>grade;
total =total+grade;
gradecounter=gradecounter + 1;
}
average=total/10;
cout<<"el promedio es"<<average<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
________________________________________________________________________________
___________________________________
valor centinela

#include <iostream>
#include<iomanip>
using namespace std;
int main()
{
int total ,
gradecounter,
grade;
float average;
total=0;
gradecounter=0;
cout<<"introdusca la calificacion oprima -1 para terminar";
cin >>grade;
while(grade != -1)
{
total=total+grade;
gradecounter=gradecounter+1;
cout<<"introdusca la calificacion oprima -1 para terminar";
cin >>grade;
}
if(gradecounter!=0)
{
average=static_cast<float>(total)/gradecounter;
cout<<"el promedio del grupo es "<<setprecision (2)
<<setiosflags(ios::fixed | ios::showpoint )
<<average<<endl;
}
else
cout<<"no se teclearon calificaciones"<<endl;
system ("PAUSE");
return 0;
}

---------------------------------------------------------------------------------------------------------------------#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int passes=0,
failures=0,
studentcounter=1,
result;
while (studentcounter<=10){cout<<"introdusca el resultado 1,2";
cin>>result;
if(result==1)
passes=passes+1;
else
failures=failures+1;
studentcounter=studentcounter+1;}
cout<<"acreditados"<<passes<<endl;
cout<<"reprobados"<<failures<<endl;
if(passes>=8)
cout<<"aumente la colegiatura"<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
--------------------------------------------------------------------------------------------------------------------------

include<iostream>
#include<iomanip>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<conio.h>
using namespace std;
int main()
{
int a,b,opcion;
float R;
cout << "ingrese dos numeros" << endl;
cout<<"ingrese primer numero"<<endl;
cin>>a;
cout<<"segundo numero"<<endl;
cin>>b;
cout<<endl<<"menu"<<endl<<endl;
cout<<"1. sumar"<<endl;

cout<<"2. restar"<<endl;
cout<<"3.multiplicar"<<endl;
cout<<"4.dividir"<<endl;
cout<<endl<<"elija una opcion"<<endl<<endl;
cin>>opcion;
cout<<endl;
switch(opcion)
{
case 1:R=a+b;
break;
case 2 : R=a-b;
break;
case 3 : R= a*b;
break;
case 4: R =a/b ;
break;
}
cout<<"el resultado es :"<<R<<endl;
system ("PAUSE");
return 0;
-------------------------------------------------------------------------------------------------------------------------#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int c;
c=5;
cout<<c<<endl;
cout<<c++<<endl;
cout<<c<<endl<<endl;
c=5;
cout<<c<<endl;
cout<<++c<<endl;
cout<<c<<endl<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
--------------------------------------------------------------------------------------------------------------------------------#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
for(int counter=100;counter>=1;counter--)
cout<<counter<<endl;

system("PAUSE");
return EXIT_SUCCESS;
}
-----------------------------------------------------------------------------------------------------------------------------------#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int suma=0;
for(int number=2 ;number<=100;number+=2)
suma+=number;
cout<<suma<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
----------------------------------------------------------------------------------------------------------------------------------using namespace std;
int main()
{
double amount,
principal=1000.0,
rate=.05;
cout<<"ao"<<setw(21)
<<"cantidad depositada"<<endl;
for(int year=1;year<=10;year++){
amount=principal*pow(1.0+rate,year);//calcula el valor de x eleva
do a la y en este caso 1+rateelevado a la year
cout<<setw(4)<<year
<<setiosflags(ios::fixed | ios::showpoint )
<<setw(21)<<setprecision(2)
<<amount<<endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
---------------------------------------------------------------------------------------------------------------------------------#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int x;
for(x=1;x<=10;x++){
if(x==5)
break;//rompe relacion con un conjunto de sentencias para come
nzar con otro
cout<<x<<endl;

}
cout<<"salida del ciclo con el valor de x en "<<x<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
-------------------------------------------------------------------------------------------------------------------------------------#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int x;
for(x=1;x<=10;x++){
if(x==5)
continue;//por defecto el valor 5 lo salta y no lo opera en el
csaso no lo imprime
cout<<x<<endl;
}
cout<<"salta el codigo faltante solo si x =5"<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#include <cstdlib>
#include <iostream>
int square(int);
using namespace std;
int square(int);
int main(int argc, char *argv[])
{
for(int x=1;x<=10;x++)
cout <<square(x)<<" ";
cout<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
int square(int x)
{return x*x;
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#include <cstdlib>
#include <iostream>
int maximum(int,int,int);
using namespace std;
int square(int);
int main(int argc, char *argv[])
{
int a,b,c;
cout<<"tecle 3 numeros enteros";
cin>>a>>b>>c;
cout <<"el mayor es"<<maximum(a,b,c)<<endl;

system("PAUSE");
return EXIT_SUCCESS;
}
int maximum(int x,int y, int z)
{int max =x;
if
(y>max)
max=y;
if
( z>max)
max= z;
return max;
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#include <cstdlib>
#include <iostream>
#include <iomanip.h>
#include <stdlib.h>
using namespace std;

int main(int argc, char *argv[])


{
for (int i=1;i<=20 ;i++)
{
cout<< setw(10)<<(1+rand()%6);
if(i%5==0)

cout <<endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#include <cstdlib>
#include <iostream>
#include <iomanip.h>
#include <stdlib.h>
using namespace std;
int main()
{
int frecuency1=0,frecuency2=0,
frecuency3 =0,frecuency4=0,
frecuency5=0,frecuency6=0,
face;
for (int roll=1;roll<=600 ;roll++)
{
face=1+rand()%6;
switch(face){
case 1:
++frecuency1;
break;
case 2:
++frecuency2;
break;
case 3:
++frecuency3;
break;
case 4:
++frecuency4;
break;
case 5:
++frecuency5;
break;
case 6:
++frecuency6;
break;
default:
cout<<"nunca debe llegar aqui";
}
}
cout<<"cara"<< setw(13)<<"frecuencia"
<<"\n1"<< setw(13)<<frecuency1
<<"\n2"<< setw(13)<<frecuency2
<<"\n3"<< setw(13)<<frecuency3
<<"\n4"<< setw(13)<<frecuency4
<<"\n5"<< setw(13)<<frecuency5

<<"\n6"<< setw(13)<<frecuency6<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#include
#include
#include
#include

<cstdlib>
<iostream>
<iomanip>
<stdlib.h>

using namespace std;


int main()
{
unsigned seed;
cout<<"tecle semilla";
cin>>seed;
srand(seed);
for (int i=1;i<=10 ;i++)
{
cout<<setw(10)<<1+rand()%6;
if(i%5==0)
cout<<endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#include
#include
#include
#include

<cstdlib>
<iostream>
<time.h>
<stdlib.h>

int rolldice(void);

using namespace std;


int main(int argc, char *argv[])
{
enum status {CONTINUE,WON,LOST} ;
int sum ,mypoint ;
status gamestatus;
srand(time(NULL));
sum=rolldice();
switch(sum)
{
case 7:
case 11:
gamestatus =WON;
break;
case 2:
case 3:
case 12:
gamestatus =LOST;
break;
default:
gamestatus=CONTINUE;
mypoint =sum;
cout<<"el puntaj es "<< mypoint <<endl;
break;
}
while (gamestatus==CONTINUE){
sum=rolldice();
if(sum==mypoint )
gamestatus=WON;
else
if(sum==7)
gamestatus =LOST;
}
if
(gamestatus==WON)
cout<<"el jugadorgana"<<endl;
else
cout<<"el jugador pierde"<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
int rolldice(void)
{ int die1,die2,worksum;
die1= 1+rand()%6;
die2= 1+rand()%6;
worksum=die1+die2;
cout<<"tirada del jugador"<<die1<<"+"<<die2
<<"="<<worksum<<endl;
return worksum;
}
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#include <cstdlib>
#include <iostream>
void a (void);
void b (void);
void c (void);
using namespace std;
int x=1;
int main(int argc, char *argv[])
{
int x= 5;
cout<<"la variable local x en alcance esterno de main es"<<x<<endl;
{
int x=7;
cout<<"la variable local x de alcance interno de main es"<<x<<endl;
}
cout <<"la variable local x en el alcance esterno de main es"<<x<<en
dl;
a();
b();
c();
a();
b();
c();
cout<<"la variable local x en main es "<<x<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
void a(void)
{
int x=25;
cout<<endl<<"la nueva variable local en funcion de a es"<<x
<<"despues de entrar a a"<<endl;
++x;
cout<<"la variable local x en la funcion a es"<<x
<<"antes de salir de a"<<endl;
}
void b(void)
{
static int x=50;
cout<<endl<<"la nueva variable local statik a es"<<x

<<"despues de entrar a b"<<endl;


++x;
cout<<"la variable local statik x es"<<x
<<"antes de salir de b"<<endl;
}
void c(void)
{
cout<<endl<<"la variable global x es "<<x
<<"al entrar a la funcion c"<<endl;
x*=10;
cout<<"la variable global x es"<<x
<<"antes de salir de c"<<endl;
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#include
#include
#include
unsigned

<cstdlib>
<iostream>
<iomanip>
long factorial(unsigned long);

using namespace std;


int main()
{
for(int i=0;i<=10;i++)
cout<<setw(2)<<i<<"!="<<factorial(i)<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
unsigned long factorial (unsigned long number)
{
if (number<=1)
return 1;
else
return number*factorial(number-1);
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#include <cstdlib>
#include <iostream>
long fibonacci(long);
using namespace std;

int main()
{
long result,number;
cout <<"tecle un numero entero";
cin>>number;
result= fibonacci(number);
cout<<"fibonacci("<<number<<")="<<result <<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
long fibonacci ( long n)
{
if(n==0||n==1 )
return n;
else
return fibonacci(n-1)+fibonacci(n-2);
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#include <cstdlib>
#include <iostream>
void funcion1();
void funcion2(void);
using namespace std;
int main(int argc, char *argv[])
{
funcion1();
funcion2();
system("PAUSE");
return EXIT_SUCCESS;
}
void funcion1()
{
cout<<"la funcion 1 no toma argumentos"<<endl;
}
void funcion2(void)
{
cout<<"la funcion 2 tampoco toma artgumentos"<<endl;
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#include <cstdlib>
#include <iostream>
inline float cube (const float s){return s*s*s;}

using namespace std;


int main(int argc, char *argv[])
{
cout<<"tecle el tamao de un ado del cubo";
float side;
cin >>side;
cout<<"el volumen de un lado del cubo es"
<<side<<"es"<<cube(side)<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#include <cstdlib>
#include <iostream>

using namespace std;


int main(int argc, char *argv[])
{
int x=3 , &y=x;
cout<<"x="<<x<<"y="<<y<<endl;
y=7;
cout<<"x="<<x<<"y="<<y<<endl;

system("PAUSE");
return EXIT_SUCCESS;

}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

You might also like