Você está na página 1de 64

Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 1

Clases del 14 de junio del 2013


Ingresar Una matriz, visualizar y mostrar su transpuesta usando arreglos




#include <conio.h>
#include <stdio.h>
const int maxfilas=10, maxcol=10;
void ImprimirMatriz(int A[][maxcol], int nf , int nc);
void Transpuesta(int A[][maxcol], int TA[][maxcol],int nf , int nc);
void IngresarMatriz(int A[][maxcol], int &nfilas, int &ncol);
int main()
{ int A[maxfilas][maxcol],TA[maxfilas][maxcol], nfilas=3, ncol=3;
IngresarMatriz(A,nfilas,ncol);
printf("\n Matriz Original ");
ImprimirMatriz(A,nfilas,ncol);
Transpuesta(A,TA,nfilas,ncol);
printf("\n Matriz Transpuesta ");
ImprimirMatriz(TA,nfilas,ncol);
getch();
}
void ImprimirMatriz(int A[][maxcol], int nf , int nc)
{ int fila,col;
for (fila=0;fila<nf;fila++)
{ printf("\n");
for (col=0;col<nc;col++) printf(" %3d",A[fila][col]);
}
}
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 2
void IngresarMatriz(int A[][maxcol], int &nfilas, int &ncol)
{ int fila,col;
printf("\n ingrese cantidad de filas 1..%d " ,maxfilas); scanf("%d", &nfilas);
printf("\n ingrese cantidad de col 1..%d ",maxcol); scanf("%d", &ncol);
for (fila=0; fila<nfilas; fila++)
for (col=0; col<ncol;col++)
{ printf("\n ingrese elemento de fila %d col %d ", fila,col);
scanf("%d",&A[fila][col]);
}
}

void Transpuesta(int A[][maxcol], int TA[][maxcol],int nf , int nc)
{ int fila,col;
for (fila=0; fila<nf;fila++)
for (col=0; col<nc;col++)
TA[col][fila]=A[fila][col];
}

2. Ingresar Una matriz, visualizar y mostrar su transpuesta usando punteros



#include <conio.h>
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 3
#include <stdio.h>
const int maxfilas=10, maxcol=10;
void ImprimirMatriz(int *A[], int nf , int nc);
void Transpuesta(int *A[], int *TA[],int nf , int nc);
void IngresarMatriz(int *A[], int &nfilas, int &ncol);
int main()
{ int *A[maxfilas],*TA[maxfilas], nfilas=3, ncol=3;
IngresarMatriz(A,nfilas,ncol);
printf("\n Matriz Original ");
ImprimirMatriz(A,nfilas,ncol);
Transpuesta(A,TA,nfilas,ncol);
printf("\n Matriz Transpuesta ");
ImprimirMatriz(TA,nfilas,ncol);
getch();
}
void ImprimirMatriz(int *A[], int nf , int nc)
{ int fila,col;
for (fila=0;fila<nf;fila++)
{ printf("\n");
for (col=0;col<nc;col++) printf(" %3d",*(A[fila]+col));
}
}
void IngresarMatriz(int *A[], int &nfilas, int &ncol)
{ int fila,col,dato;
printf("\n ingrese cantidad de filas " ); scanf("%d", &nfilas);
printf("\n ingrese cantidad de col "); scanf("%d", &ncol);
for (fila=0; fila<nfilas; fila++)
{ A[fila] = new int [ncol];
for (col=0; col<ncol;col++)
{ printf("\n ingrese elemento de fila %d col %d ", fila,col);
scanf("%d",&dato);
*(A[fila]+ col)=dato;
}
}
}

void Transpuesta(int *A[], int *TA[],int nf , int nc)
{ int fila,col;
for (col=0; col<nc;col++)
{TA[col] = new int [nf];
for (fila=0; fila<nf;fila++)
*(TA[col]+fila) =*(A[fila]+col);
}
}


3. Usando doble puntero
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 4


#include <conio.h>
#include <stdio.h>
const int maxfilas=10, maxcol=10;
void ImprimirMatriz(int **A, int nf , int nc);
void Transpuesta(int **A, int **TA,int nf , int nc);
void IngresarMatriz(int **A, int nfilas, int ncol);
int main()
{ int **A,**TA, nfilas=3, ncol=3;
printf("\n ingrese cantidad de filas " ); scanf("%d", &nfilas);
printf("\n ingrese cantidad de col "); scanf("%d", &ncol);
A= new int * [nfilas];
TA= new int *[nfilas];
IngresarMatriz(A,nfilas,ncol);
printf("\n Matriz Original ");
ImprimirMatriz(A,nfilas,ncol);
Transpuesta(A,TA,nfilas,ncol);
printf("\n Matriz Transpuesta ");
ImprimirMatriz(TA,nfilas,ncol);
getch();
}
void ImprimirMatriz(int **A, int nf , int nc)
{ int fila,col;
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 5
for (fila=0;fila<nf;fila++)
{ printf("\n");
for (col=0;col<nc;col++) printf(" %3d",*(*(A+fila)+col));
}
}
void IngresarMatriz(int **A, int nfilas, int ncol)
{
int fila,col,dato;
for (fila=0; fila<nfilas; fila++)
{ *(A+fila)=new int [ncol];
for (col=0; col<ncol;col++)
{ printf("\n ingrese elemento de fila %d col %d ", fila,col);
scanf("%d",&dato);
*(*(A+fila) +col)=dato;
}
}
}

void Transpuesta(int **A, int **TA,int nf , int nc)
{ int fila,col;
for (col=0; col<nc;col++)
{ *(TA+col) = new int [nf];
for (fila=0; fila<nf;fila++)
*( *(TA+col)+fila) =*( *(A+fila) +col);
}
}


PRACTICAS DEL MIERCOLES 19 DE JUNIO DEL 2013
Se revisar el trabajo asignado por lo menos el avance 5 puntos



Se tiene la funcion f(x) = (x+2)(x-1)(x-2) encuentre los siguientes valores

1. Encuentre las raices
2. Calcule los siguientes valores en el rango de la primera raz a la ltima (u otro
rango)
3. El punto maximo y minimo, entre los puntos enter el primera raiz y la tercea raiz
4. Grafique la funcin con sus coordenadas y escalas adecuadas
5. La longitud de la funcin
6. La coordenadas del punto ms alto en ese intervalo
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 6
7. Las coordenadas del punto ms bajo en ese intervalo
8. El rea en ese intervalo
9. El volumen de solido de revolucin alrededor del eje X
10. El volumen de solido de revolucin alrededor del eje Y
11. rea lateral de revolucin en el intervalo
12. La distancia mnima de la funcin al punto (X,Y Ej3.2 )
13. Ubicar un punto (x,y) en el intervalo de tal manera que la distancia a los puntos
de la funcin sea mnima
14. Ubicar un rectngulo de mayor rea posible entre raiz1 y 2 (opcional)
15. Mostrar el rea de corte dado en un valor x y tambin en un valor de y ( sale la
circuenferencia)
16. Obtener la derivada en un puntos (x,y)
17. Obtener las interseccin caso en que hay varias funciones
18. Recorrer la funcion y indique en cada momento sus coordenadas x,y
19. Encontrar la distancia mnima de la raiz1 al punto indicado
20. Realizar los puntos 1 al 14 en modo visual
21. Realizar grafica en 3d
22. Realice ejercicios de recursividad

Elabore los mismos ejercicios pero para otras funciones
Otras preguntas de interes

TRABAJO 2 LO MISMO PERO CON ARREGLOS

Avance del trabajo



// **** calculos.h
float f( float x)
{ return (x+2)*(x-1)*(x-2);}
float distancia( float x1, float y1 ,float x2, float y2)
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 7
{ return sqrt(pow(x2-x1,2)+pow(y2-y1,2));}
void GraficarFuncion( float Cx, float Cy, float Ex, float Ey, float Li, float Ls);
float mayorvalor( float Li, float Ls)
{ float mayor=-1000,dx=0.001,xmayor=Li,x;
for (x=Li;x<Ls;x=x+dx)
if(f(x) >mayor) { mayor=f(x); xmayor=x;}
return xmayor;
}
float recorrido(float Li,float Ls)
{ float dt=0,dx=1.0,x,dp;
for (x=Li; x<=Ls;x=x+dx)
{dp=distancia(x,f(x), x+dx, f(x+dx));
dt= dt+ dp;
}
return dt;
}
float AreaTrapecial(float a, float b, int nd)
{ float dx=(b-a)/nd;
float at=0,x;
for(x=a; x<b;x=x+dx)
at=at+fabs( (f(x)+f(x+dx)/2.0)*dx);
return at;
}

void GraficarFuncion( float Cx, float Cy, float Ex, float Ey, float Li, float Ls)
{ float dx=0.01,x,y;
gotoxy(Cx,Cy);
// coordenadas
textcolor(0,12);
for (x=1;x<78;x++)
{ gotoxy(x,Cy); printf("*");
}
for (y=1;y<24;y++)
{ gotoxy(Cx,y); printf("*");
}
textcolor(0,9);
for( x=Li;x<Ls;x=x+dx)
{ y=f(x);
gotoxy(Cx+x*Ex,Cy+y*Ey); printf("*");
}
}

#include <conio.h>
#include <stdio.h>
#include <math.h>
#include "e:\datos\milib.h"
#include "calculos.h"

int main ()
{float cx=40, cy=12,ex=10,ey=-1.5,li=-2.5,ls=2.5,xmayor=0,R1=-2,R2=2;
GraficarFuncion(cx,cy,ex,ey,li, ls);
xmayor=mayorvalor(R1,R2);
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 8
textcolor(0,14);
gotoxy(1,21); printf("\n el punto mas alto esta en x =%f y=%f", xmayor, f(xmayor));
gotoxy(1,22); printf("\n el recorrido de la primera raiz a las ultima es =%f",
recorrido(R1,R2));
gotoxy(1,23);
printf("\n el Area de %f a %f es %f", R1,R2,AreaTrapecial(R1,R2,1000));

getch();
}

PRACTICA SOBRE VECTORES Y MATRICES

1.Ingresar n elementos y mostrarlo

#include <conio.h>
#include <stdio.h>
const int maxcol=10;
int main ()
{ int A[maxcol], n,col;
printf("\n ingrese cantidad de elementos"); scanf("%d",&n);
printf("\n ingrese %d elementos", n);
for (col=0;col<n;col++) scanf("%d", &A[col]);
printf("\n elementos ingresados");
for (col=0;col<n;col++) printf(" %3d", A[col]);
getch();
}



2. el mismo ejercicio pero para nmeros reales



#include <conio.h>
#include <stdio.h>
const int maxcol=50;
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 9
int main ()
{ float A[maxcol];
int n,col;
printf("\n ingrese cantidad de elementos"); scanf("%d",&n);
printf("\n ingrese %d elementos", n);
for (col=0;col<n;col++) scanf("%f", &A[col]);
printf("\n elementos ingresados");
for (col=0;col<n;col++) printf(" %8.2f", A[col]);
getch();
}

3. el mismo ejercicio pero con letras
#include <conio.h>
#include <stdio.h>
const int maxcol=50;
int main ()
{ char A[maxcol];
int n,col;
printf("\n ingrese cantidad de elementos"); scanf("%d",&n);
printf("\n ingrese %d elementos", n);
for (col=0;col<n;col++) scanf("%c", &A[col]);
printf("\n elementos ingresados");
for (col=0;col<n;col++) printf(" %c", A[col]);
getch();
}


4. Con funciones



Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 10

#include <conio.h>
#include <stdio.h>
const int maxcol=15;
void Ingresar( int A[],int n)
{ for ( int col=0;col<n;col++) scanf("%d", &A[col]);
}
void Mostrar( int A[],int n)
{ for ( int col=0;col<n;col++) printf("%4d", A[col]);
}
int main ()
{ int A[maxcol], n,col;
printf("\n ingrese cantidad de elementos"); scanf("%d",&n);
printf("\n ingrese %d elementos", n);
Ingresar(A,n);
printf("\n elementos ingresados");
Mostrar(A,n);
getch();
}

Ejercicio 5 modifique el ejercicio 1 para listar los elementos del vector , el mayor
elemento , el menor elemento , el promedio y la cantidad de pares



#include <conio.h>
#include <stdio.h>
int const maxcol=20;
int main()
{int A[maxcol]={1,2,45,6,7,-3,10,1,2,3};
int n=10,i,mayor=A[0],menor=A[0],suma=0,pares=0;
printf("\n los datos ingresado son");
for(i=0;i<n;i++) printf("%4d", A[i]);
// encontrar el mayor y el menor
for (i=0;i<n;i++)
{if (A[i]>mayor) mayor=A[i];
if (A[i]<menor) menor=A[i];
suma=suma+A[i];
if (A[i] %2==0)pares++;
}
printf("\n el mayor es %d", mayor);
printf("\n el menor es %d", menor);
printf("\n el promedio es %f",(float)suma/n);
printf("\n cantidad de pares %d",pares);
getch();
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 11
}
Ejercicio 6 modifique el ejercicio 1 para listar los elementos del vector , el mayor
elemento , el menor elemento , el promedio y la cantidad de pares y sus lugares




// **** calculos.h

const int maxcol =15;
int Mayor( int A[], int n)
{ int maximo=A[0],lugMayor=0;
for (int col=0; col<n;col++)
if (A[col] >maximo) {maximo=A[col]; lugMayor=col;}
return lugMayor;
}
int Menor( int A[], int n)
{ int minimo=A[0],lugMenor=0;
for (int col=0; col<n;col++)
if (A[col]<minimo) {minimo=A[col]; lugMenor=col;}
return lugMenor;
}
void LugaresPares(int A[], int pares[],int n, int &np)
{ int cont=0, i;
for (i=0;i<n;i++)
if (A[i] % 2==0) pares[cont++]= i;
np=cont;
}
void Mostrar( int A[], int n)
{ for (int col=0; col<n;col++) printf(" %4d", A[col]);
}


#include <conio.h>
#include <stdio.h>
#include "calculos.h"
int main ()
{ int A[maxcol]={12,14,-13,45,-30,4,7,8,9,3,5}, n=10;
int pares[maxcol], np=0,i ;
printf("\n elementos ingresados"); Mostrar(A,n);
int lugarMayor=Mayor(A,n);
int lugarMenor=Menor(A,n);
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 12
printf("\n elemento mayor es %d y esta en lugar %d",A[lugarMayor], lugarMayor);
printf("\n elemento menor es %d y esta en lugar %d ",A[lugarMenor],lugarMenor);
LugaresPares(A,pares,n,np);
printf("\n elementos pares");
for (i=0;i<np;i++)
printf("\n %d %d ", pares[i], A[pares[i] ]);
getch();
}

Etiquetar los puntos de un grfico en excel
1 . escoja la fecha programador e insertar controles






Copie el siguiente cdigo

Private Sub CommandButton1_Click()
Dim texto As String
ActiveSheet.ChartObjects("1 Grfico").Activate
ActiveChart.SeriesCollection(1).Select
ActiveChart.SeriesCollection(1).ApplyDataLabels
i = 2
para = 0
While para = 0
texto = Cells(i, 2)
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 13
punto = Cells(i, 1)
If texto <> "" Then
ActiveChart.SeriesCollection(1).Points(punto).DataLabel.Select
ActiveChart.SeriesCollection(1).Points(punto).DataLabel.Text = texto
Else
para = 1
End If
i = i + 1
Wend
End Sub

Sealice el grfico y ponga su nombre del grafico de acuerdo a la ficha presentacin



Se tiene los siguientes puntos se pide recuperar el archivo y graficarlo

Nro Punto X Y dx y1 y2 ap at
1 0 2 1 2 1 1 2 2
2 1 4 1 0 1 3 0 2
3 2 4 3 1 3 3 3 5
4 3 5 3 -2 3 6 -9 -4
5 4 3 6 -2 6 3 -9 -13
6 5 1 3 1 3 3 3 -10
7 6 2 3 0 3 1 0 -10
8 7 2 1





Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 14



//*** calculos
const int maxcol=15;
void Mostrar( float X[], float Y[],int n)
{ for ( int col=0;col<n;col++)
printf("\n %f %f", X[col],Y[col]);
}
void recuperar(float X[], float Y[], int n)
{ FILE *p;
p= fopen("E:\\datos\\figura.txt","r");
for (int i=0;i<n;i++)
{ fscanf(p,"%f",&X[i]);
fscanf(p,"%f",&Y[i]);
}
fclose(p);
}

int Mayor( int A[], int n)
{int Maximo=A[0],lugMax=0;
for (int i=0;i<n;i++) if(A[i]>Maximo){ Maximo=A[i]; lugMax=i;}
return lugMax;
}
int Menor( int A[], int n)
{int Minimo=A[0];
for (int i=0;i<n;i++) if(A[i]<Minimo) Minimo=A[i];
return Minimo; }

float AreaTrapecial(float X[], float Y[], int np)
{ float at=0,x,y1,y2,ap;
for (int i=0;i<np-1;i++)
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 15
{ y1=Y[i];
y2=Y[i+1];
ap=(y1+y2)/2.0*(X[i+1]-X[i]);
at=at+(ap);
}
return at;
}

float distancia(float x1,float y1, float x2, float y2)
{ return sqrt(pow(x2-x1,2)+pow(y2-y1,2));}
float perimetro( float X[], float Y[], int n)
{float dt=0;
for (int i=0;i<n-1; i++)
dt= dt+ distancia( X[i], Y[i], X[i+1], Y[i+1]);
return dt;
}
void linea(float x1,float y1,float x2,float y2)
{ int i,vel=10;
float mayor,ancho,alto,partex,partey,px,py,dx,dy;
ancho=fabs(x2-x1);
alto=fabs(y2-y1);
if (x1<=x2)dx=1;else dx=-1;
if (y1<=y2)dy=1;else dy=-1;
if (ancho>alto)mayor=ancho;else mayor=alto;
partex=ancho/mayor;
partey=alto/mayor;
for (i=0;i<=mayor;i++)
{ px=i*partex*dx;
py=i*partey*dy;
gotoxy(x1+px,y1+py);
printf("*");
// Sleep(vel);
}
}
void GraficarFuncion( float Cx, float Cy, float Ex, float Ey,
float X[], float Y[], int n)
{ float x,y,x1,y1,x2,y2;
gotoxy(Cx,Cy);
// coordenadas
textcolor(0,12);
for (x=1;x<78;x++)
{ gotoxy(x,Cy); printf("*");
}
for (y=1;y<24;y++)
{ gotoxy(Cx,y); printf("*");
}
textcolor(0,9);
for(int i=0; i<n-1;i++)
{ x1=Cx+X[i]*Ex;
y1=Cy+Y[i]*Ey;
x2=Cx+X[i+1]*Ex;
y2=Cy+Y[i+1]*Ey;
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 16
linea(x1,y1,x2,y2);
}
}


#include <conio.h>
#include <stdio.h>
#include <math.h>
#include "E:\datos\milib.h"
#include "calculos.h"
int main ()
{ float X[maxcol],Y[maxcol];
int n=8;
float Cx=40, Cy=12, Ex=4, Ey=-2;
recuperar(X,Y,n);
printf("\n datos del archivo recuperado");
Mostrar(X,Y,n);
printf("\n El area es %f", AreaTrapecial(X,Y, n));
printf("\n El perimetro es %f", perimetro(X,Y, n));
GraficarFuncion( Cx, Cy, Ex, Ey, X, Y, n);
getch();
}


Ejercicio propuesto 2 se tiene los siguientes puntos

nro Punto x y dx y1 y2 ap at d dt
1 0 0 0 0 0 4 0 0 4 4
2 1 0 4 4 4 6 20 20 4.47 8.47
3 2 4 6 4 6 4 20 40 4.47 12.94
4 3 8 4 0 4 0 0 40 4.00 16.94
5 4 8 0 -8 0 0 0 40 8.00 24.94
6 5 0 0



Grabamos como puntos6.txt

Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 17


Solucion con el programa anterior




Hacemos traslacin, escalado y rotacin
-5 -3 0.5 0.5 0.785
traslacion escalado
rotacion
EjeZ
PUNTO X Y x1 y1 x2 y2 x3 y3
0 0 0 -5 -3 0 0 0 0
1 0 4 -5 1 0 2 -2.83 2.828
2 4 6 -1 3 2 3 -1.41 7.071
3 8 4 3 1 4 2 2.828 8.485
4 8 0 3 -3 4 0 5.657 5.657
5 0 0 -5 -3 0 0 0 0

Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 18







2.3.1 Rotacin

Cada punto rota alrededor de un punto pivote. El pivote no necesariamente tiene que
estar en el centro del objeto
Si se tiene el punto original x1,y1,z1 y se quiere rotar un ngulo se debe usar las
siguientes frmulas [3]

- Rotacin alrededor del eje z

| | | | 1 cos 1 1 1 cos 1
1 0 0
0 cos
0 cos
1 1 1 z y sen x sen y x sen
sen
z y x o o o o o o
o o
+ =
(
(
(



- Rotacin alrededor del eje x:

-4
-2
0
2
4
6
8
10
-6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9
original
TRASLACION
ESCALADO
Rotacion Z
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 19
(
(
(

o o
o o
cos 0
cos 0
0 0 1
sen
sen

- Rotacin alrededor del eje y:

(
(
(


o o
o o
cos 0
0 1 0
0 cos
sen
sen


2.3.2Traslacion.- Es el proceso de mover el objetos en el eje x, y i z . Para ello se
debe modificar el centro del objeto Cx, Cy ,Cz (Cx=Cx+k, Cy=cy+k, Cz=cz+k)

2.3.3 Escalado.- es el proceso de aumentar o disminuir el tamao del objeto tanto en
el eje x ,eje y , o el eje z para ello se debe modificar las escalas por un factor de escala
el eje correspondiente (Ex=Ex*k, Ey=Ey*k, Ez=Ez*k)
Si las escalas son negativas ocurre la reflexin

Ver el ejercicio de la pagina 320
PRACTICAS DEL MIERCOLES 19 DE JUNIO DEL 2013
ARREGLOS
Ejemplo sobre vectores pagina 292
Ejemplo sobre matrices pagina 298
Explicar sobre trabajos Trabajo 2 mtodos numricos en modo discreto
Ejercicio de la pagina 302
Burbujas de asteriscos
Ejercicios mover un ser en forma aleatoria 345
Ejercicios de arreglos con recursividad permutaciones sin repiticiones y
con repeticiones

Recuperar matrices y vectores
Realizar los ejercicios del capitulo 5

PRACTICAS DEL VIERNES 21 DE JUNIO DEL 2013

Avance del trabajo

Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 20
// **** calculos.h
float f( float x)
{ return (x+2)*(x-1)*(x-2);}
float distancia( float x1, float y1 ,float x2, float y2)
{ return sqrt(pow(x2-x1,2)+pow(y2-y1,2));}
void GraficarFuncion( float Cx, float Cy, float Ex, float Ey, float Li, float Ls);
float mayorvalor( float Li, float Ls)
{ float mayor=-1000,dx=0.001,xmayor=Li,x;
for (x=Li;x<Ls;x=x+dx)
if(f(x) >mayor) { mayor=f(x); xmayor=x;}
return xmayor;
}
float menorvalor( float Li, float Ls)
{ float menor=1000,dx=0.001,xmenor=Li,x;
for (x=Li;x<Ls;x=x+dx)
if(f(x) <menor) { menor=f(x); xmenor=x;}
return xmenor;
}
float recorrido(float Li,float Ls)
{ float dt=0,dx=1.0,x,dp;
for (x=Li; x<=Ls;x=x+dx)
{dp=distancia(x,f(x), x+dx, f(x+dx));
dt= dt+ dp;
}
return dt;
}
float AreaTrapecial(float a, float b, int nd)
{ float dx=(b-a)/nd;
float at=0,x;
for(x=a; x<b;x=x+dx)
at=at+fabs( (f(x)+f(x+dx)/2.0)*dx);
return at;
}

void GraficarFuncion( float Cx, float Cy, float Ex, float Ey, float Li, float Ls)
{ float dx=0.01,x,y;
gotoxy(Cx,Cy);
// coordenadas
textcolor(0,12);
for (x=1;x<78;x++)
{ gotoxy(x,Cy); printf("*");
}
for (y=1;y<24;y++)
{ gotoxy(Cx,y); printf("*");
}
textcolor(0,9);
for( x=Li;x<Ls;x=x+dx)
{ y=f(x);
gotoxy(Cx+x*Ex,Cy+y*Ey); printf("*");
}
}

Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 21
#include <conio.h>
#include <stdio.h>
#include <math.h>
#include "e:\datos\milib.h"
#include "calculos.h"
int main ()
{float cx=40, cy=12,ex=10,ey=-1.5,li=-2.5,ls=2.5,xmayor=0,R1=-2,R2=2;
float xmenor=0;
GraficarFuncion(cx,cy,ex,ey,li, ls);
xmayor=mayorvalor(R1,R2);
textcolor(0,14);
gotoxy(cx+xmayor*ex,cy+f(xmayor)*ey); printf("ma");
gotoxy(1,21); printf("\n el punto mas alto esta en x =%f y=%f", xmayor, f(xmayor));
gotoxy(1,22);
xmenor=menorvalor(R1,R2);
printf("\n el punto mas bajo esta en x =%f y=%f", xmenor, f(xmenor));

gotoxy(1,23); printf("\n el recorrido de la primera raiz a las ultima es =%f",
recorrido(R1,R2));
gotoxy(1,24);
printf("\n el Area de %f a %f es %f", R1,R2,AreaTrapecial(R1,R2,1000));

getch();
}

Ejercicio 1. Ingresar n elementos a un vector y mostrarlo




#include <conio.h>
#include <stdio.h>
const int maxcol=10;
int main()
{ int n, i,A[maxcol];
printf("\n ingrese la cantidad de elemento");
scanf("%d",&n);
printf("\n ingrese %d la cantidad de elemento",n);
for (i=0;i<n;i++) scanf("%d",&A[i]);
printf("\n los elementos ingresados son",n);
for (i=0;i<n;i++) printf("%4d",A[i]);
getch();
}
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 22
2. Como ingresar si los elementos del vector son reales

3. Ingresar los elementos del vector usando punteros



#include <conio.h>
#include <stdio.h>
const int maxcol=100;
int main()
{ int n, i; int *A;
int dato;
printf("\n ingrese la cantidad de elemento");
scanf("%d",&n);
A=new int[n];
printf("\n ingrese %d la cantidad de elemento",n);
for (i=0;i<n;i++)
{ scanf("%d",&dato);
*(A+i) =dato;
}
printf("\n los elementos ingresados son");
for (i=0;i<n;i++)
printf("%d ",*(A+i));
getch();
}

Ejercicio 4. Usando funciones mostrar el elemento mayor y en que lugar est, el
elemento menor que valor tiene y en que lugar est




const int maxcol=20;
void Ingresar(int A[], int &ne)
{ printf("\n ingrese la cantidad de elementos"); scanf("%d",&ne);
for (int i=0;i<ne;i++) scanf("%d",&A[i]);
}
void Mostrar(int A[], int n) {for (int i=0;i<n;i++) printf("%4d",A[i]); }
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 23
int Mayor(int A[], int n)
{int maximo=A[0],i,lugmayor=0;
for ( i=0;i<n;i++)
if(A[i]>maximo)
{ maximo=A[i];lugmayor=i;}
return lugmayor;
}
int Menor(int A[], int n)
{int minimo=A[0],i,lugmenor=0;
for ( i=0;i<n;i++)
if(A[i]<minimo)
{ minimo=A[i];lugmenor=i;}
return lugmenor;
}
#include <conio.h>
#include <stdio.h>
#include "CALCULOS.h"
int main()
{ int n=10, A[maxcol]={1,23,45,-3,4,5,6,7,8,12};
int lugmayor=Mayor(A,n); int lugmenor=Menor(A,n); //Ingresar(A,n);
printf("\n los elementos ingresados son"); Mostrar(A,n);
printf("\n el elemento mayor es %d y esta en lugar %d", A[lugmayor],lugmayor);
printf("\n el elemento menor es %d y esta en lugar %d",
A[lugmenor],lugmenor);
getch();
}

Ejercicio 5. Modifique el programa para mostrar los nmeros pares y en que lugares
se encuentra



#include <conio.h>
#include <stdio.h>
#include "CALCULOS.h"
int main()
{ int n=10, np,A[maxcol]={1,24,45,-3,4,5,6,8,7,12};
int pares[maxcol];
int lugmayor=Mayor(A,n);
int lugmenor=Menor(A,n);
//Ingresar(A,n);
printf("\n los elementos ingresados son"); Mostrar(A,n);
printf("\n el elemento mayor es %d y esta en lugar %d",
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 24
A[lugmayor],lugmayor);
printf("\n el elemento menor es %d y esta en lugar %d",
A[lugmenor],lugmenor);
LugaresPares(A,pares,n,np);
for ( int i=0;i<np;i++)
printf("\n %d %d ", A[pares[i]], pares[i]);
getch();
}

Ejercicio 6. Se tiene los puntos x i y en Excel que dibuja una figura mostrada en el
grafico se desea cargar los puntos a dos vectores X i Y luego mostrarlo



Pasos Copiarlo los puntos a bloc de notas y grabarlo como casita6.txt



#include <conio.h>
#include <stdio.h>
const int maxcol=20;
int main()
{ FILE *p; int n=6,i;
float X[maxcol],Y[maxcol];
p=fopen("E:\\datos\\casita6.txt","r");
for (i=0;i<n;i++)
fscanf(p,"%f%f",&X[i],&Y[i]);
fclose(p);
printf("\n los valores leidos X y Y son");
for (i=0;i<n;i++)
printf("\n %8.2f %8.2f ", X[i], Y[i]);
getch();
}
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 25
Ejercicio 7 . graficar la funcin

/// ***************************


///************** calculos.h
const int maxcol=20;
void Recuperar(float X[], float Y[], int n)
{FILE *p; p=fopen("E:\\datos\\casita6.txt","r");
for (int i=0;i<n;i++) fscanf(p,"%f%f",&X[i],&Y[i]);
fclose(p);
}
void Mostrar(float X[], float Y[], int n)
{for (int i=0;i<n;i++) printf("\n %8.2f %8.2f ", X[i], Y[i]);
}
void linea(float x1,float y1,float x2,float y2)
{ int i,vel=10;
float mayor,ancho,alto,partex,partey,px,py,dx,dy;
ancho=fabs(x2-x1);
alto=fabs(y2-y1);
if (x1<=x2)dx=1;else dx=-1;
if (y1<=y2)dy=1;else dy=-1;
if (ancho>alto)mayor=ancho;else mayor=alto;
partex=ancho/mayor;
partey=alto/mayor;
for (i=0;i<=mayor;i++)
{ px=i*partex*dx;
py=i*partey*dy;
gotoxy(x1+px,y1+py);
printf("*");
// Sleep(vel);
}
}

Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 26
void GraficarFuncion( float Cx, float Cy, float Ex, float Ey,
float X[], float Y[], int n)
{// coordenadas
float x,y;
textcolor(0,12);
for (x=1;x<78;x++) { gotoxy(x,Cy); printf("*"); }
for (y=1;y<24;y++) { gotoxy(Cx,y); printf("*");}
textcolor(0,9);
for(int i=0;i<n-1;i++)
linea(Cx+X[i]*Ex,Cy+Y[i]*Ey, Cx+X[i+1]*Ex, Cy+Y[i+1]*Ey);
}


#include <conio.h>
#include <stdio.h>
#include <math.h>
#include "E:\datos\milib.h"
#include "calculos.h"
int main()
{ int n=6,i; float X[maxcol],Y[maxcol];
float Cx=40, Cy=12,Ex=4, Ey=-2;
Recuperar(X,Y,n);
printf("\n los valores leidos X y Y son"); Mostrar(X,Y,n);
GraficarFuncion(Cx,Cy,Ex,Ey,X,Y,n);
getch();
}

Girar la figura una cantidad de grados


///************** calculos.h
const int maxcol=20;
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 27
#define pi 3.1416
void Recuperar(float X[], float Y[], int n)
{FILE *p; p=fopen("E:\\datos\\casita6.txt","r");
for (int i=0;i<n;i++) fscanf(p,"%f%f",&X[i],&Y[i]);
fclose(p);
}
void Mostrar(float X[], float Y[], int n)
{for (int i=0;i<n;i++) printf("\n %8.2f %8.2f ", X[i], Y[i]);
}
void linea(float x1,float y1,float x2,float y2)
{ int i,vel=10;
float mayor,ancho,alto,partex,partey,px,py,dx,dy;
ancho=fabs(x2-x1);
alto=fabs(y2-y1);
if (x1<=x2)dx=1;else dx=-1;
if (y1<=y2)dy=1;else dy=-1;
if (ancho>alto)mayor=ancho;else mayor=alto;
partex=ancho/mayor;
partey=alto/mayor;
for (i=0;i<=mayor;i++)
{ px=i*partex*dx;
py=i*partey*dy;
gotoxy(x1+px,y1+py);
printf("*");
// Sleep(vel);
}
}
void GraficarCoordenadas (float Cx, float Cy )
{float x,y;
for (x=1;x<78;x++) { gotoxy(x,Cy); printf("*"); }
for (y=1;y<24;y++) { gotoxy(Cx,y); printf("*");}
}
void GraficarFuncion( float Cx, float Cy, float Ex, float Ey,
float X[], float Y[], int n)
{
for(int i=0;i<n-1;i++)
linea(Cx+X[i]*Ex,Cy+Y[i]*Ey, Cx+X[i+1]*Ex, Cy+Y[i+1]*Ey);
}
void RotacionZ(float X[], float Y[], float X1[], float Y1[] ,float grados,int n)
{ int i;
float alfa= grados*pi/180.0,x1,y1,x2,y2;
for (i=0; i<n;i++)
{x1= X[i];
y1=Y[i];
x2=x1*cos(alfa)-y1*sin(alfa);
y2=x1*sin(alfa)+ y1*cos(alfa);
X1[i]=x2;
Y1[i]=y2;
}
}


Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 28
PLAN DE PRACTICAS DEL DIA MIEROCLES 10 DE JUILIO DELO 2013

Trabajo de dezplazamiento de pantalla de una figura
1. Disee en excel la siguiente figura o puede ser cualquier figura

2. Grabe como archivo de texto 4x20 casita40x20.TXT","r");



// ************* calculos .h
void IniciarMatriz(int A[][maxcol], int nf , int nc)
{ int fila,col,dato;
for (fila=0;fila<nf;fila++)
for (col=0;col<nc;col++)
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 29
A[fila][col]=0;
}
void RecuperarMatriz(int A[][maxcol], int nf , int nc)
{ FILE *p; int fila,col,dato;
p=fopen("E:\\DATOS\\casita40x20.TXT","r");

for (fila=0;fila<nf;fila++)
for (col=0;col<nc;col++)
fscanf(p,"%d",&A[fila][col]);
fclose(p);
}
void RecuperarObjeto(int A[][3], int nf , int nc)
{ FILE *p; int fila,col,dato;
p=fopen("E:\\DATOS\\objeto3x3.TXT","r");
for (fila=0;fila<nf;fila++)
for (col=0;col<nc;col++)
fscanf(p,"%d",&A[fila][col]);
fclose(p);
}
void MostrarMatrizEnPantalla(int Cx, int Cy, int A[][maxcol], int nfilas, int ncol)
{ int fila,col;
for (fila=0;fila<nfilas;fila++)
for (col=0;col<ncol;col++)
{ gotoxy(Cx+col, Cy+fila);
textcolor(0, A[fila][col]);
printf("%c",219);
}
}
void MostrarObjetoEnPantalla(int Cx, int Cy, int A[][3], int nfilas, int ncol)
{ int fila,col;
for (fila=0;fila<nfilas;fila++)
for (col=0;col<ncol;col++)
{ gotoxy(Cx+col, Cy+fila);
textcolor(0, A[fila][col]);
if (A[fila][col]>=0)printf("%c",219);
}
}


void LeerCopiaObjetoDeMatriz(int Matriz[][maxcol], int cx, int cy, int
Copia[][maxcol], int nf1, int nc1)
{int fila , col;
for (fila=0;fila<nf1; fila++)
for (col=0;col<nc1;col++)
Copia[fila][col]=Matriz[cy+fila][cx+col];
}

#include <conio.h>
#include <stdio.h>
#include "E:\datos\milib.h"
const int maxfilas=100,maxcol=100;
int Matriz[maxfilas][maxcol];
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 30
int Muestra[maxfilas][maxcol];
int Objeto[3][3];

#include "calculos.h"

int main()
{ char ch, ch1;
int nf =20, nc=40,Cx=1,Cy=1,nf1=10,nc1=20,Cx1=50,Cy1=10;
RecuperarMatriz(Matriz,nf,nc);
RecuperarObjeto(Objeto,3,3);

//ImprimirMatriz(Matriz, nf,nc);
MostrarMatrizEnPantalla( Cx, Cy, Matriz,nf,nc);
LeerCopiaObjetoDeMatriz(Matriz, 0,0, Muestra, nf1, nc1);
MostrarMatrizEnPantalla( Cx1, Cy1, Muestra, nf1,nc1);
MostrarObjetoEnPantalla( Cx1+10, Cy1+5, Objeto, 3,3);

int x1=0,y1=0, x2=30, y2=10;
int x=x1,y=y1;

do
{ ch=getch();
switch (ch)
{ case 'x' : if (x<x2) x++;break; // flecha arriba
case 'X': if (x>x1) x--;break; //flecha izquierda
case 'y': if (y<y2)y++;break; // flecha derecha;
case 'Y': if (y>y1)y-- ;break; // flecha abajo
}
gotoxy(60,1);printf(" %d %d ",x,y);
LeerCopiaObjetoDeMatriz(Matriz, x,y, Muestra, nf1, nc1);
MostrarMatrizEnPantalla( Cx1, Cy1, Muestra, nf1,nc1);
// if (x < x2-3x)
MostrarObjetoEnPantalla( Cx1+10, Cy1+5, Objeto, 3,3);

}while(ch!=27);

}
Grabar los valores de una matriz para ser leido en excel ejemplo los valores de la
funcion
(x+2)(x-1)(x-3)
#include <conio.h>
#include <stdio.h>
#include <math.h>
#include "e:\datos\milib.h"
const int maxfilas=50,maxcol=3;
float f( float x)
{ return (x+2)*(x-1)*(x-2);}

void ImprimirMatriz(float A[][maxcol], int nf , int nc)
{ int fila,col;
for (fila=0;fila<nf;fila++)
{
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 31
for (col=0;col<nc;col++)
printf("%8.4f ",A[fila][col]) ;
printf("\n");
}

}

void GrabarMatriz(float A[][maxcol], int nf , int nc)
{ FILE *p; int fila,col;
p=fopen("E:\\DATOS\\funcion.TXT","w");
for (fila=0;fila<nf;fila++)
{
for (col=0;col<nc;col++)
fprintf(p,"%8.4f\t",A[fila][col]) ;
fprintf(p,"\n");
}
fclose(p);
}

int main ()
{float dx,x,y,li=-2.5,ls=3.5;
int i, nf=40, nc=3;
float Matriz[maxfilas][maxcol];
dx=(ls-li)/nf;
for (i=0,x=li;i<nf;i++,x=x+dx)
{ y=f(x);
Matriz[i][0]=(float) i;
Matriz[i][1]=x;
Matriz[i][2]=y;

}
ImprimirMatriz(Matriz,nf,nc);
GrabarMatriz(Matriz, nf,nc);
printf("\n el archivo ha sido grabado correctamente");

getch();
}


Ejercicio de rotacin escalado y traslacin y escalado en modo texto

Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 32




/// ***********calculos .h
static int ax1=0,ay1=0,az1=0;
static int tx1=0,ty1=0,tz1=0;
static float ex1=1,ey1=1,ez1=1;
float X[tam],Y[tam], Z[tam];
float Cx=40,Cy=12,Cz=0,np=17,largo=10,D=1000;
float CXX[2],CXY[2],CXZ[2];
float CYX[2],CYY[2],CYZ[2];
float CZX[2],CZY[2],CZZ[2];
void IniciarVectores()
{ CXX[0]=-largo; CXX[1]=largo;
CXY[0]=0; CXY[1]=0;
CXZ[0]=0; CXZ[1]=0;
CYX[0]=0; CYX[1]=0;
CYY[0]=-largo; CYY[1]=largo;
CYZ[0]=0; CYZ[1]=0;
CZX[0]=0; CZX[1]=0;
CZY[0]=0; CZY[1]=0;
CZZ[0]=-largo; CZZ[1]=largo;
}
void linea(float x1,float y1,float x2,float y2)
{ int i,vel=10;
float mayor,ancho,alto,partex,partey,px,py,dx,dy;
ancho=fabs(x2-x1);
alto=fabs(y2-y1);
if (x2==x1&& y2==y1) { gotoxy(x1,y1); printf("*"); return; }
if (x1<=x2)dx=1;else dx=-1;
if (y1<=y2)dy=1;else dy=-1;
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 33
if (ancho>alto)mayor=ancho;else mayor=alto;
partex=ancho/mayor;
partey=alto/mayor;
for (i=0;i<=mayor;i++)
{ px=i*partex*dx;
py=i*partey*dy;
gotoxy(x1+px,y1+py);
printf("*");
// Sleep(vel);
}
}
void Mostrar(float X[],float Y[],float Z[],int np)
{ for (int i =0;i<np; i++)
printf("\n %3d %8.2f %8.2f %8.2f", i,X[i], Y[i],Z[i]);
}

void graficar(float X[],float Y[],float Z[], int np,float Cx,float Cy, float Cz,float D)
{
float x1,y1,z1,x2,y2,z2,px1,py1,px2,py2;
for (int i =0;i<np-1; i++)
{ x1= Cx+X[i]; y1=Cy+Y[i]; z1=Cz+Y[i];
x2= Cx+X[i+1]; y2=Cy+Y[i+1]; z2=Cz+Y[i+1];
px1 = (x1 * D) / (D + z1);
py1 = (y1 * D) / (D + z1);
px2 = (x2 * D) / (D + z2);
py2 = (y2 * D) / (D + z2);
linea(px1,py1,px2,py2);
}
}
void recuperar ( float X[],float Y[], float Z[],int np)
{FILE *p; int fila;
float nx,ny,nz;
p=fopen("E:\\datos\\cubo.txt","r");
for (fila =0;fila<np; fila++)
{ fscanf(p,"%f%f%f",&nx,&ny,&nz);
X[fila]=nx;
Y[fila]=ny;
Z[fila]=nz;
}
fclose(p);
}

void Escalado ( float X[],float Y[], float Z[], int np,float ex,float ey,float ez)
{ // factor de escalado el ultimo valor dividido entre el anterior
float fex=ex/ex1, fey=ey/ey1, fez= ez/ez1;
for (int fila=0;fila<np;fila++)
{ X[fila]=X[fila]*fex;
Y[fila]=Y[fila]*fey;
Z[fila]=Z[fila]*fez;
}
ex1=ex;
ey1=ey;
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 34
ez1=ez;
}
void traslacion ( float X[],float Y[], float Z[],float tx,float ty,float tz,int np)
{ int fila, dx=tx-tx1, dy=ty-ty1,dz=tz-tz1;
for ( fila=0;fila<np;fila++)
{ X[fila]=X[fila]+dx;
Y[fila]=Y[fila]+dy;
Z[fila]=Z[fila]+dz;
}
tx1=tx; ty1=ty; tz1=tz;
}
void RotacionZ (int az)
{ float x1,y1,z1,azr;
int daz,fila ;
//rotamos todos los objetos que hay
daz=(az-az1); if (abs(daz)>paso) { az1=az; return;}
azr=(daz)*pi/180.0;
for (int fila=0;fila<2;fila++)
{// rotando alrededor del eje z el vector CX
x1=CXX[fila]; y1=CXY[fila]; z1=CXZ[fila];
CXX[fila]=x1*cos(azr)-y1*sin(azr);
CXY[fila]=x1*sin(azr)+ y1 *cos(azr);
CXZ[fila]=z1;
// rotando alrededor del eje z el vector CYX
x1=CYX[fila]; y1=CYY[fila]; z1=CYZ[fila];
CYX[fila]=x1*cos(azr)-y1*sin(azr);
CYY[fila]=x1*sin(azr)+ y1 *cos(azr);
CYZ[fila]=z1;
// rotando alrededor del eje z el vector CYX
x1=CZX[fila]; y1=CZY[fila]; z1=CZZ[fila];
CZX[fila]=x1*cos(azr)-y1*sin(azr);
CZY[fila]=x1*sin(azr)+ y1 *cos(azr);
CZZ[fila]=z1;
}
// rotando la figura alrededor del eje Z
for ( fila=0;fila<np;fila++)
{ x1=X[fila]; y1=Y[fila]; z1=Z[fila];
X[fila]=x1*cos(azr)-y1*sin(azr);
Y[fila]=x1*sin(azr)+ y1 *cos(azr);
Z[fila]=z1;
}
az1=az ;
}

void RotacionX (int ax)
{ float x1,y1,z1,axr;
int dax,fila ;
//rotamos todos los objetos que hay
dax=(ax-ax1); if (abs(dax)>paso) { ax1=ax; return;}
axr=(dax)*pi/180.0;
for (int fila=0;fila<2;fila++)
{// rotando alrededor del eje x el vector CX
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 35
x1=CXX[fila]; y1=CXY[fila]; z1=CXZ[fila];
CXX[fila]=x1;
CXY[fila]=y1*cos(axr)- z1 *sin(axr);
CXZ[fila]=y1*sin(axr)+z1*cos(axr);
// rotando alrededor del eje x el vector CX
x1=CYX[fila]; y1=CYY[fila]; z1=CYZ[fila];
CYX[fila]=x1;
CYY[fila]=y1*cos(axr)- z1 *sin(axr);
CYZ[fila]=y1*sin(axr)+z1*cos(axr);
// rotando alrededor del eje x el vector CX
x1=CZX[fila]; y1=CZY[fila]; z1=CZZ[fila];
CZX[fila]=x1;
CZY[fila]=y1*cos(axr)- z1 *sin(axr);
CZZ[fila]=y1*sin(axr)+z1*cos(axr);
}
// rotando la figura alrededor del eje x
for ( fila=0;fila<np;fila++)
{ x1=X[fila]; y1=Y[fila]; z1=Z[fila];
X[fila]=x1;
Y[fila]=y1*cos(axr)- z1 *sin(axr);
Z[fila]=y1*sin(axr)+z1*cos(axr);
}
ax1=ax ;
}

void RotacionY (int ay)
{ float x1,y1,z1,ayr;
int day,fila ;
//rotamos todos los objetos que hay
day=(ay-ay1); if (abs(day)>paso) { ay1=ay; return;}
ayr=(day)*pi/180.0;
for (int fila=0;fila<2;fila++)
{// rotando alrededor del eje y el vector CX
x1=CXX[fila]; y1=CXY[fila];
z1=CXZ[fila];
CXX[fila]=x1*cos(ayr)+z1*sin(ayr);
CXY[fila]=y1;
CXZ[fila]=-x1*sin(ayr)+z1*cos(ayr);
// rotando alrededor del eje y el vector CX
x1=CYX[fila]; y1=CYY[fila];
z1=CYZ[fila];
CYX[fila]=x1*cos(ayr)+z1*sin(ayr);
CYY[fila]=y1;
CYZ[fila]=-x1*sin(ayr)+z1*cos(ayr);
// rotando alrededor del eje yz el vector CX
x1=CZX[fila]; y1=CZY[fila];
z1=CZZ[fila];
CZX[fila]=x1*cos(ayr)+z1*sin(ayr);
CZY[fila]=y1;
CZZ[fila]=-x1*sin(ayr)+z1*cos(ayr);
}
// rotando la figura alrededor del eje y
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 36
for ( fila=0;fila<np;fila++)
{
// rotando alrededor del eje yz el vector CX
x1=X[fila]; y1=Y[fila];
z1=Z[fila];
X[fila]=x1*cos(ayr)+z1*sin(ayr);
Y[fila]=y1;
Z[fila]=-x1*sin(ayr)+z1*cos(ayr);
}
ay1=ay ;
}


#include <conio.h>
#include <stdio.h>
#include <math.h>
#include "e:\datos\milib.h"
const int tam=50,paso=8;
#define pi 3.1416
#include "calculos.h"
int main()
{ char ch;
int tx=0,ty=0,tz=0;
float ex=paso, ey=paso, ez=paso;
unsigned int AnguloX=0, AnguloY=0, AnguloZ=0;
IniciarVectores();
recuperar (X,Y,Z, np);
Escalado (X,Y,Z, np, ex,ey,ez);
gotoxy(1,1);printf(" %d %d %d | %d %d %d | %f %f %f" ,AnguloX,AnguloY,
AnguloZ,tx,ty,tz,ex,ey,ez);
textcolor(0,9);graficar(CXX,CXY, CXZ,2,Cx,Cy,Cz,D);
textcolor(0,12);graficar(CYX,CYY, CYZ,2,Cx,Cy,Cz,D);
textcolor(0,10);graficar(CZX,CZY, CZZ,2,Cx,Cy,Cz,D);
textcolor(0,14);graficar(X,Y, Z,np,Cx,Cy,Cz,D);
do
{ ch=getch();
clrscr();
// valor=0;
switch(ch)
{ case 'x': AnguloX=(AnguloX+paso) % 360; break;
case 'X': AnguloX=(AnguloX-paso) % 360; break;
case 'y': AnguloY=(AnguloY+paso) % 360; break;
case 'Y': AnguloY=(AnguloY-paso) % 360; break;
case 'z': AnguloZ=(AnguloZ+paso) % 360; break;
case 'Z': AnguloZ=(AnguloZ-paso) % 360; break;
case 'h': if (tx<paso*2)tx++;; break;
case 'H': if(tx>-paso*2)tx--;break;;
case 'v': if (ty<paso/2)ty++;; break;
case 'V': if(ty>-paso/2)ty--;break;;
case 'p': if (tz<paso*2)tz++;; break;
case 'P': if(tz>-paso*2)tz--;break;;
case 'a': if (ex<paso)ex++; break;
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 37
case 'A': if(ex>1)ex--;break;;
case 'b': if (ey<paso)ey++; break;
case 'B': if(ey>1)ey--;break;;
case 'c': if (ez<paso)ez++; break;
case 'C': if(ez>1)ez--;break;;
}
gotoxy(1,1);printf(" %d %d %d | %d %d %d | %f %f %f" ,AnguloX,AnguloY,
AnguloZ,tx,ty,tz,ex,ey,ez);
Escalado (X,Y,Z, np, ex,ey,ez);
traslacion ( X,Y,Z,tx,ty,tz, np);
RotacionX (AnguloX); // Rotar todos los objetos
RotacionY (AnguloY); // Rotar todos los objetos
RotacionZ (AnguloZ); // Rotar todos los objetos
textcolor(0,9);graficar(CXX,CXY, CXZ,2,Cx,Cy,Cz,D);
textcolor(0,12);graficar(CYX,CYY, CYZ,2,Cx,Cy,Cz,D);
textcolor(0,10);graficar(CZX,CZY, CZZ,2,Cx,Cy,Cz,D);
textcolor(0,14);graficar(X,Y, Z,np,Cx,Cy,Cz,D);
}
while(ch!=27);
}

Ver el mismo ejercicio en modo visual ver el documento LPGRAFICOS pag 714 cuyo
cdigo es el sigueinte




Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 38


Haga clic en cuadro de herramientas


Y arrastre el control d emenu strip y disee un menu con las sigueitnes opciones









Arrastre tres controles de grupo un picture box, y un datagriviw , a cada group
arrastre tres barras de dezplazamiento horizontal(hsroll Bar), 3 label y 3 cuadros de
texto se meustra en la figura cambien los nombres de los scroll bar como hSrx , hsRy,
hsRz y sus poriedad maximun y minimuin a 360 y 0
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 39


Y arregle los controles en el group bxo y copie y cambie para trsalcion los nombres de
las barras de dezplazamoiento con hsCx, hsCy, hsCz y hsD, el maximun y minimun de
los bar a 250 y minimun a 0 y el maximun y minimun del scroll bar a4000 y 1


Copie el segundo gruup y cambien sus porpiedades para el grupo de esfcalado como s
emuestra en la figura y cambie su nombres a hsEx, hs ey , hs ez y los valores
Maximun y ,minimun 10 y 1

Con la opcion Agregar nuevo elemento agregue un archivo de cabecerq llado calculos
que tenga el siguiente el codigo del programa anterior

Y agregue el sigueinte codigo

// **************calculos.h
#include <stdio.h>
#include <math.h>
#define Pi 3.1416

const int maxfilas=20, maxcol=30;
int Ncol=4, Nfilas =6;
int Cx=200, Cy=200, Cz=0;
float Ex=100,Ey=100,Ez=10;
int D=1000, Modo=0;
float Normales[maxfilas][maxcol];
float A[maxfilas][maxcol];
float GradozAnterior=0, GradosyAnterior=0,GradosxAnterior=0;
void RecuperarPuntos(float A[][maxcol], int nfilas, int ncol)
{ int fila, col; float dato;
FILE *p;
p= fopen("E:\\DATOS\\caras2.TXT","r");
for ( fila=0;fila<nfilas; fila++)
for (col=0;col<ncol;col++)
{ fscanf(p,"%f",&dato);
A[fila][col]=dato;
}
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 40
fclose(p);
}

void ObtenerNormales(float A[][maxcol], float Normales[][maxcol], int nf, float D)
{ float Pe, R, Ax,Ay,Az,Bx,By,Bz;
float Nx , Ny, Nz, Nx1, Ny1, Nz1;
float x1,y1,z1,x2,y2,z2,x3,y3,z3;
int fila;
for (fila=0;fila<nf; fila++)
{ x1 = A[fila][1],
y1 = A[fila][ 2];
z1 = A[fila][ 3];
x2 = A[fila][ 4];
y2 = A[fila][ 5];
z2 = A[fila][ 6];
x3 = A[fila][ 7];
y3 = A[fila][ 8];
z3 = A[fila][ 9];
Ax = x2 - x1;
Ay = y2 - y1;
Az = z2 - z1;
Bx = x3 - x2;
By = y3 - y2;
Bz = z3 - z2;
Nx = Ay * Bz - Az * By;
Ny = Ax * Bz - Az * Bx;
Nz = Ax * By - Ay * Bx;
//' producto cruz
R = sqrt(Nx * Nx + Ny * Ny + Nz * Nz);
if( R > 0) Nx1 = Nx / R; else Nx1 = 1000;
if( R > 0) Ny1 = Ny / R; else Ny1 = 1000;
if (R > 0) Nz1 = Nz / R;else Nz1 = 1000;
Pe = Nx1 * 0 + Ny1 * 0 + Nz1 * D;
Normales[fila][ 0] = Nx1;
Normales[fila][ 1] = Ny;
Normales[fila][ 2] = Nz1;
Normales[fila][ 3] = Pe;
}
}




Este archivo de cabcera incluya en la primera linea de codigo del archivo del formulario
#pragma once
#include "calculos.h"

Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 41

Haga doble clic en menu cargar puntos e inserte el siguiente codigo
int fila,col;
RecuperarPuntos(A,Nfilas,Ncol*3+1);


dataGridView1->ColumnCount=Ncol*3+1;
dataGridView1->RowCount =Nfilas+1;
for (col=0;col<Ncol*3+1;col++)
dataGridView1->Columns[col]->Width =20;
for (fila=0;fila<Nfilas;fila++)
for (col=0;col<Ncol*3+1;col++)
dataGridView1->Rows[fila]->Cells [col]->Value =A[fila][col];
MessageBox::Show("El archivo ha sido cargado satisfactoriamente","Mensaje",
MessageBoxButtons::OK, MessageBoxIcon::Exclamation);

Vea el ejercicio enviado



private: System::Void MnucargarPuntos_Click(System::Object^ sender,
System::EventArgs^ e) {
int fila,col;
RecuperarPuntos(A,Nfilas,Ncol*3+1);
dataGridView1->ColumnCount=Ncol*3+1;
dataGridView1->RowCount =Nfilas+1;
for (col=0;col<Ncol*3+1;col++)
dataGridView1->Columns[col]->Width =20;
for (fila=0;fila<Nfilas;fila++)
for (col=0;col<Ncol*3+1;col++)
dataGridView1->Rows[fila]->Cells [col]->Value =A[fila][col];
MessageBox::Show("El archivo ha sido cargado satisfactoriamente","Mensaje",
MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
}


Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 42
void graficar(float M[][maxcol], int nf , int nc , int cx, int cy , int cz, float ex, float ey,
float ez, int D, int modo , float Normales[][maxcol])
{ int fila, col,cara=0;
float x1 , y1 , z1, px1, py1;
System::Drawing::Graphics^ formGraphics = pictureBox1->CreateGraphics();
SolidBrush^ brocha = gcnew SolidBrush( Color::Green );
Pen^ pluma = gcnew Pen( Color::FromArgb(255,0,0),2.0f );
for (fila=0;fila<nf;fila++)
{ array <Point> ^Figura=gcnew array<Point>(nc);
cara=M[fila][0];
for (col=0;col<nc;col++)
{
x1 = cx + M[fila][ col * 3 + 1] * ex;
y1 = cy + M[fila][ col * 3 + 2] * ey;
z1 = cz + M[fila][ col * 3 + 3] * ez;
px1 = (x1 * D) / (D + z1);
py1 = (y1 * D) / (D + z1);
Figura[col].X = px1;
Figura[col].Y = py1;
}
switch(modo)
{case 0 :formGraphics->DrawPolygon(pluma, Figura );break;
case 1: if (Normales[fila][2] > 0 )// 'solo dibuja si el normal es mayor a 0
{ switch(cara)
{ case 0 : brocha->Color = Color::FromArgb (0,255,0);break;
case 1 : brocha->Color = Color::FromArgb (0,0,255);break;
case 2 : brocha->Color = Color::FromArgb (255,0,0);break;
case 3 : brocha->Color = Color::FromArgb (255,255,0);break;
case 4 : brocha->Color = Color::FromArgb (0,255,255);break;
case 5 : brocha->Color = Color::FromArgb (100,100,100);break;
} // del switch de cara
formGraphics->FillPolygon(brocha, Figura );
} // fin del if del case 1
break;
} // fin de switch del modo
} // fila
// }
} // ****************** fin

private: System::Void dibujar_Click(System::Object^ sender, System::EventArgs^ e) {
graficar(A, Nfilas , Ncol , Cx, Cy , Cz, Ex, Ey, Ez, D, Modo ,Normales);
}
void Borrar()
{
System::Drawing::Graphics^ grafico1 = pictureBox1-
>CreateGraphics();
grafico1->Clear (Color::FromArgb (255,255,255) );
delete grafico1;
}

private: System::Void MnuBorrar_Click(System::Object^ sender, System::EventArgs^
e) {
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 43
Borrar();
}
private: System::Void hSCX_Scroll(System::Object^ sender,
System::Windows::Forms::ScrollEventArgs^ e) {
Borrar();
Cx =hSCX->Value ;
txtCx->Text =""+Cx;
graficar(A, Nfilas,Ncol, Cx, Cy, Cz, Ex, Ey, Ez, D, Modo, Normales);



}
private: System::Void hSCy_Scroll(System::Object^ sender,
System::Windows::Forms::ScrollEventArgs^ e) {
Cy =hSCy->Value ;
txtCy->Text =""+Cy;
Borrar();

graficar(A, Nfilas,Ncol, Cx, Cy, Cz, Ex, Ey, Ez, D, Modo, Normales);

}
private: System::Void hSCz_Scroll(System::Object^ sender,
System::Windows::Forms::ScrollEventArgs^ e) {
Cz =hSCz->Value ;
txtCz->Text =""+Cz;
Borrar();

graficar(A, Nfilas,Ncol, Cx, Cy, Cz, Ex, Ey, Ez, D, Modo, Normales);

}
private: System::Void hSPerspectiva_Scroll(System::Object^ sender,
System::Windows::Forms::ScrollEventArgs^ e) {
D = hSPerspectiva->Value ;
txtD->Text =""+D;
Borrar();
graficar(A, Nfilas,Ncol, Cx, Cy, Cz, Ex, Ey, Ez, D, Modo, Normales);

}
private: System::Void hSRX_Scroll(System::Object^ sender,
System::Windows::Forms::ScrollEventArgs^ e) {
float angulox=0,x1,y1,z1,x2,y2,z2,dx,gradosx,vargrados;
int fila,col;
gradosx = hSRX->Value;
txtRX->Text = " "+gradosx;
vargrados=gradosx-GradosxAnterior;
angulox = vargrados * Pi / 180;
dx = 2 *Pi / 360.0;

for (fila=0;fila<Nfilas;fila++)
{
for (col=0; col<Ncol;col++)
{
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 44
x1 = A[fila][col * 3 + 1];
y1 = A[fila][ col * 3 + 2];
z1 = A[fila][ col * 3 + 3];
x2 = x1;
y2 = y1 * cos(angulox) - z1 *sin(angulox);
z2 = y1 *sin(angulox) + z1 * cos(angulox);
A[fila][col * 3 + 1] = x2;
A[fila][col * 3 + 2] = y2;
A[fila][ col * 3 + 3] = z2;
}
}
Borrar();
ObtenerNormales(A, Normales, Nfilas, D);
graficar(A, Nfilas, Ncol, Cx, Cy, Cz, Ex, Ey, Ez, D, Modo, Normales);
GradosxAnterior=gradosx;

}
private: System::Void hSRY_Scroll(System::Object^ sender,
System::Windows::Forms::ScrollEventArgs^ e) {
float x1,y1,z1,x2,y2,z2,gradosy,anguloy,dy,vargrados;
int fila,col;
gradosy = hSRY->Value;
txtRY->Text = ""+gradosy;
vargrados=gradosy-GradosyAnterior;
anguloy = vargrados * Pi / 180;
// dy = 2 * Pi / 360;
for (fila=0;fila<Nfilas;fila++)
for (col=0;col<Ncol;col++)
{
x1 = A[fila][ col * 3 + 1];
y1 = A[fila][ col * 3 + 2];
z1 = A[fila][ col * 3 + 3];
x2 = x1 * cos(anguloy) + z1 *sin(anguloy);
y2 = y1;
z2 = -x1 * sin(anguloy) + z1 * cos(anguloy);
A[fila][ col * 3 + 1] = x2;
A[fila][ col * 3 + 2] = y2;
A[fila][ col * 3 + 3] = z2;
}
Borrar();
ObtenerNormales(A, Normales, Nfilas, D);
graficar(A, Nfilas, Ncol, Cx, Cy, Cz, Ex, Ey, Ez, D, Modo, Normales);
GradosyAnterior=gradosy;

}
private: System::Void hSRZ_Scroll(System::Object^ sender,
System::Windows::Forms::ScrollEventArgs^ e) {
int fila,col;
float x1,y1,z1,x2,y2,z2,gradosz,anguloz,dz,vargrados;
gradosz = hSRZ->Value;
txtRz->Text =""+ gradosz;
vargrados=gradosz-GradozAnterior;
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 45
anguloz = vargrados * Pi / 180;
//dz = 2 *Pi / 360;
for(fila=0;fila<Nfilas; fila++)
for (col=0; col<Ncol;col++)
{
x1 = A[fila][ col * 3 + 1];
y1 = A[fila][ col * 3 + 2];
z1 = A[fila][ col * 3 + 3];
x2 = x1 * cos(anguloz) - y1 * sin(anguloz);
y2 = x1 * sin(anguloz) + y1 * cos(anguloz);
z2 = z1;
A[fila][ col * 3 + 1] = x2;
A[fila][ col * 3 + 2] = y2;
A[fila][ col * 3 + 3] = z2;
}
Borrar();
ObtenerNormales(A, Normales, Nfilas, D);
graficar(A, Nfilas, Ncol, Cx, Cy, Cz, Ex, Ey, Ez, D, Modo, Normales);
GradozAnterior=gradosz;
}
private: System::Void textBox2_TextChanged(System::Object^ sender,
System::EventArgs^ e) {
}
private: System::Void hSEx_Scroll(System::Object^ sender,
System::Windows::Forms::ScrollEventArgs^ e) {
Ex = hSEx->Value;
txtEx->Text =""+Ex;
Borrar();
graficar(A, Nfilas, Ncol, Cx, Cy, Cz, Ex, Ey, Ez, D, Modo, Normales);

}
private: System::Void hSEy_Scroll(System::Object^ sender,
System::Windows::Forms::ScrollEventArgs^ e) {

Ey = hSEy->Value;
txtEy->Text =""+Ey;
Borrar();
graficar(A, Nfilas, Ncol, Cx, Cy, Cz, Ex, Ey, Ez, D, Modo, Normales);

}
private: System::Void hSEz_Scroll(System::Object^ sender,
System::Windows::Forms::ScrollEventArgs^ e) {

Ez = hSEz->Value;
txtEz->Text =""+Ez;
Borrar();
graficar(A, Nfilas, Ncol, Cx, Cy, Cz, Ex, Ey, Ez, D, Modo, Normales);

}
private: System::Void modeloAlambre_Click(System::Object^ sender,
System::EventArgs^ e) {
Modo=0;
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 46
}
private: System::Void modeloSolido_Click(System::Object^ sender,
System::EventArgs^ e) {
Modo=1;
}
private: System::Void salirToolStripMenuItem_Click(System::Object^ sender,
System::EventArgs^ e) {
this->Close();
}
};
}




SOLUCION DE PRACTICAS DEL MIERCOLES 10 D EJULIO


#include <conio.h>
#include <stdio.h>
#include "E:\datos\milib.h"
const int maxfilas=100,maxcol=100;
int Matriz[maxfilas][maxcol];
void RecuperarMatriz(int A[][maxcol], int nf , int nc);
void MostrarMatrizEnPantalla(int Cx, int Cy, int A[][maxcol], int nfilas, int ncol);
int main()
{ int Cx=3,Cy=4,nf=23,nc=21;
RecuperarMatriz(Matriz,nf,nc);
MostrarMatrizEnPantalla( Cx, Cy, Matriz,nf,nc);
getch();
}
void RecuperarMatriz(int A[][maxcol], int nf , int nc)
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 47
{ FILE *p; int fila,col,dato;
p=fopen("E:\\DATOS\\casita21x23.TXT","r");
for (fila=0;fila<nf;fila++)
for (col=0;col<nc;col++)
fscanf(p,"%d",&A[fila][col]);
fclose(p);
}
void MostrarMatrizEnPantalla(int Cx, int Cy, int A[][maxcol], int nfilas, int ncol)
{ int fila,col;
for (fila=0;fila<nfilas;fila++)
for (col=0;col<ncol;col++)
{ gotoxy(Cx+col, Cy+fila);
textcolor(0, A[fila][col]);
printf("%c",219);
}
}






#include <conio.h>
#include <stdio.h>
#include <math.h>
#include "e:\datos\milib.h"
const int maxfilas=500,maxcol=4;
float f( float x,float y)
{ return sin(x*x+y*y)/(x*x+y*y);}

void ImprimirMatriz(float A[][maxcol], int nf , int nc)
{ int fila,col;
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 48
for (fila=0;fila<nf;fila++)
{ for (col=0;col<nc;col++)
printf("%8.4f ",A[fila][col]) ; printf("\n"); }
}
void GrabarMatriz(float A[][maxcol], int nf , int nc)
{ FILE *p; int fila,col;
p=fopen("E:\\DATOS\\funcion3D.TXT","w");
for (fila=0;fila<nf;fila++)
{ for (col=0;col<nc;col++)
fprintf(p,"%8.4f\t",A[fila][col]) ;
fprintf(p,"\n");
}
fclose(p);
}

int main ()
{float dx=0.5,x,y,lix=-5,lsx=5;
float dy=0.5,z,liy=-5,lsy=5;
int i=0, nf=20, nc=4;
float Matriz[maxfilas][maxcol];
for (y=liy;y<=lsy;y=y+dy)
for (x=lix;x<=lsx;x=x+dx)
{ z=f(x,y);
Matriz[i][0]=(float) i;
Matriz[i][1]=x;
Matriz[i][2]=y;
Matriz[i][3]=z;
i++;
}
nf=i;
ImprimirMatriz(Matriz,nf,nc);
GrabarMatriz(Matriz, nf,nc);
printf("\n el archivo ha sido grabado correctamente");

getch();
}


Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 49




// obtener un archivo de texto con delimitadores para
//ser leido por excel
#include <conio.h>
#include <stdio.h>
#include <math.h>
#include "e:\datos\milib.h"
const int maxfilas=500,maxcol=4;
float f( float x,float y)
{ return sin(x*x+y*y)/(x*x+y*y);}

void ImprimirMatriz(float A[][maxcol], int nf , int nc)
{ int fila,col;
for (fila=0;fila<nf;fila++)
{ for (col=0;col<nc;col++)
printf("%8.4f ",A[fila][col]) ; printf("\n"); }
}
void GrabarMatriz(float A[][maxcol], int nf , int nc)
{ FILE *p; int fila,col;
p=fopen("E:\\DATOS\\funcion3D.TXT","w");
for (fila=0;fila<nf;fila++)
{ for (col=0;col<nc;col++)
fprintf(p,"%8.4f\t",A[fila][col]) ;
fprintf(p,"\n");
}
fclose(p);
}

int main ()
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 50
{ FILE *p;
p= fopen("E:\\datos\\funcion3D.txt","w");
float dx=0.5,x,y,lix=-5,lsx=5;
float dy=0.5,z,liy=-5,lsy=5;
int i=0, nf=20, nc=4;
float Matriz[maxfilas][maxcol];
for (y=liy;y<=lsy;y=y+dy)
{ for (x=lix;x<=lsx;x=x+dx)
{ z=f(x,y);
printf("%6.2f",z);
fprintf(p,"%6.2f\t",z);
}
printf("\n");
fprintf(p,"\n");
}
fclose(p);
getch();
}

Pruebe el problema del buscador




//*** declaraciones
const int maxfilas=40,maxcol=40;
int Matriz[maxfilas][maxcol];
struct Objeto
{ int nro,dir[4];
int Cx,Cy;
int np; // nro de puntos
int valor, color,Direc;
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 51
};
Objeto Objeto1, Objetos[maxfilas*maxcol];


// ************* calculos .h
void RecuperarMatriz(int A[][maxcol], int nf , int nc)
{ FILE *p; int fila,col,dato;
p=fopen("E:\\DATOS\\Matriz40x20.txt","r");
for (fila=0;fila<nf;fila++)
for (col=0;col<nc;col++)
{
fscanf(p,"%d",&dato);
A[fila][col]=dato;;
}
fclose(p);
}

void MostrarMatrizEnPantalla(int Cx, int Cy, int A[][maxcol], int nfilas, int ncol)
{ int fila,col;
for (fila=0;fila<nfilas;fila++)
for (col=0;col<ncol;col++)
{ gotoxy(Cx+col, Cy+fila);
textcolor(0, A[fila][col]);
printf("%c",219);
}
}
void MostrarObjeto(Objeto Objeto1, int Cx, int Cy)
{ textcolor(0,Objeto1.color );
gotoxy(Cx+Objeto1.Cx ,Cy+Objeto1.Cy ); printf("%d",Objeto1.valor );
// if (modo==1)Nro++; else Nro--;
}
void ImprimirObjetoForma2 ( Objeto Objeto1, int Cx, int Cy)
{ gotoxy(Cx,Cy);
printf(" Objeto Nro %d Cx=%d,Cy=%d Np=%d Dir %d %d %d %d ",
Objeto1.nro,Objeto1.Cx ,Objeto1.Cy ,Objeto1.np , Objeto1.dir[0]
,Objeto1.dir[1],Objeto1.dir[2],Objeto1.dir[3]);

}
void ImprimerObjeto(Objeto Ob1, int Cx, int Cy)
{ gotoxy(Cx,Cy); printf("\n Ob1 Nro %d", Ob1.nro);
gotoxy(Cx,Cy+1); printf("\n Ob1 Cx %d", Ob1.Cx );
gotoxy(Cx,Cy+2); printf("\n Ob1 Cy %d", Ob1.Cy );
gotoxy(Cx,Cy+3); printf("\n Ob1 np %d", Ob1.np );
gotoxy(Cx,Cy+4); printf("\n Ob1 Valor %d", Ob1.valor );
gotoxy(Cx,Cy+5); printf("\n Ob1 Col %d", Ob1.color );
gotoxy(Cx,Cy+6); printf("\n Ob1 dir "); for (int i=0; i<4; i++)
printf("%3d",Ob1.dir[i]);
}

void ImprimirMatrizEnPantalla(int Cx, int Cy, int A[][maxcol], int nfilas, int ncol)
{ int fila,col;
for (fila=0;fila<nfilas;fila++)
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 52
for (col=0;col<ncol;col++)
{ gotoxy(Cx+col, Cy+fila);
if (A[fila][col]>1) textcolor(0,A[fila][col]); else textcolor(0,15);
printf("%d",A[fila][col]);
}
}

int obtenerDir( int A[])
{ for (int i=0;i<4;i++) if (A[i]==1) return i+1;
return 0;
}
int evaluar(Objeto &Objeto1, int Matriz[][maxcol],int camino, int meta)
{ int cont=0,Cx,Cy,Direc=0;
for (int i=0; i<4;i++) Objeto1.dir[i]=0;
Cx= Objeto1.Cx ;
Cy= Objeto1.Cy ;
// primero evalua si ya encontro la metas
if ( Matriz[Cy][Cx+1]==meta) { Objeto1.dir [0]=1;cont++; return 1;}
if ( Matriz[Cy-1][Cx]==meta){Objeto1.dir[1]=1; cont++;return 1; }
if ( Matriz[Cy][Cx-1]==meta){ Objeto1.dir[2]=1; cont++;return 1;}
if ( Matriz[Cy+1][Cx]==meta){Objeto1. dir[3]=1; cont++;return 1;}
// si no encuentra meta evalua si hay camino
if ( Matriz[Cy][Cx+1]==camino) { Objeto1.dir [0]=1;cont++;}
if ( Matriz[Cy-1][Cx]==camino){Objeto1.dir[1]=1; cont++;}
if ( Matriz[Cy][Cx-1]==camino){ Objeto1.dir[2]=1; cont++;}
if ( Matriz[Cy+1][Cx]==camino){Objeto1. dir[3]=1; cont++;}
Objeto1.np =cont;
Direc= obtenerDir(Objeto1.dir );
Objeto1.Direc =Direc;
return 0;
}
void InicializarObjeto(Objeto &Objeto1)
{
Objeto1.Cx =14; Objeto1.Cy=13;
Objeto1.color =12; Objeto1.valor =1;
Objeto1.dir[0]=0;Objeto1.dir[1]=0;Objeto1.dir[2]=0;Objeto1.dir[3]=0;
Objeto1.np=0; Objeto1.nro=0;
}


#include <conio.h>
#include <stdio.h>
#include "E:\datos\milib.h"
#include "declaraciones.h"
#include "calculos.h"
int main()
{ int Px2=0,Py2=0,valorbuscado=2,huella=4,
camino=0,terminado=0,i,retorno=5,meta=9,resultado=0;
int nf =20, nc=40,Cx=10,Cy=1,dir=0,valor=0,final=0,cont=0;
int NroObjeto=0; // posicion para imprimir el valor del objeto
InicializarObjeto(Objeto1);
RecuperarMatriz(Matriz,nf,nc);
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 53
final=0;
ImprimirMatrizEnPantalla( Cx, Cy, Matriz,nf,nc);
MostrarObjeto(Objeto1, Cx, Cy );
ImprimirObjetoForma2(Objeto1, 30,21);
NroObjeto=0;
Objetos[NroObjeto]=Objeto1;
Matriz[Objeto1.Cy ][Objeto1.Cx ]=huella;
getch();
do
{
do
{ ImprimirMatrizEnPantalla( Cx, Cy, Matriz,nf,nc);
MostrarObjeto(Objeto1, Cx, Cy );
resultado= evaluar(Objeto1, Matriz ,camino,meta);
ImprimirObjetoForma2(Objeto1, 30,21);
// getch();
if (resultado==1) goto fin;
dir=Objeto1.Direc ;
switch(dir)
{ case 1: Objeto1.Cx++; break;
case 2: Objeto1.Cy--; break;
case 3: Objeto1.Cx--; break;
case 4: Objeto1.Cy++; break;
default: terminado=1;
}
Matriz[Objeto1.Cy ][Objeto1.Cx ]=huella;
NroObjeto++;cont++;
Objeto1.nro=NroObjeto;
Objetos[NroObjeto]=Objeto1;
// Sleep(1);
}while(terminado==0);
// Retornno recorre en sentido inverso hasta cuando dir >1 pero evaluado de nuevo
terminado=0;
//getch();
do
{ Objeto1= Objetos[NroObjeto];
ImprimirObjetoForma2(Objeto1, 30,21);
ImprimirMatrizEnPantalla( Cx, Cy, Matriz,nf,nc);
MostrarObjeto(Objeto1, Cx, Cy );
resultado= evaluar(Objeto1, Matriz ,camino,meta);
dir=Objeto1.Direc;
if (NroObjeto<0) goto fin;
if (dir>0) terminado=1;
else
{
Matriz[Objeto1.Cy ][Objeto1.Cx ]=retorno;
NroObjeto--; cont++;
// getch();
// Sleep(1);
}
}while(terminado==0);
if (NroObjeto<0) final=1;
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 54
} while(final==0);
MostrarObjeto(Objeto1, Cx, Cy );
fin:
textcolor(0,14);
gotoxy(1,22);
if (NroObjeto>0)
{printf("\n El objeto %d encontro la meta esta ubicado en X %d Y=%d ",NroObjeto,
Objetos[NroObjeto].Cx ,Objetos[NroObjeto].Cy);
printf("\n y se hizo Con %d movimientos ",cont);
}
else
{ printf("\n No se encontro el objeto y se hizo %d movimientos", cont);
}
getch();
}



Vea el siguiente ejercicio de la pagina 370 arreglos

///******** calculos.h
const int limite=20,longitud=20;
const int maxfilas=24, maxcol=80;
struct punto
{ int nro,longitud, dx,dy;
int X[limite],Y[limite],color[limite];
float dmenor;
};
punto Ser;
int Matriz[maxfilas][maxcol];
float distancia( float x1,float y1, float x2, float y2)
{ return sqrt(pow(x2-x1,2)+pow(y2-y1,2));
}
void IniciarMatriz (int Matriz[][maxcol], int nf, int nc)
{ int fila ,col;
for (fila=0; fila<nf; fila++)
for (col=0;col<nc;col++)
Matriz[fila][col]=0;
}
void MostrarMatriz (int Matriz[][maxcol], int nf, int nc)
{ int fila ,col;
for (fila=1; fila<nf; fila++)
for (col=1;col<nc;col++)
{ gotoxy(col,fila);
if (Matriz[fila][col]>0) textcolor(0,12); else textcolor(0,10);
printf("%d",Matriz[fila][col]);
}
}
void IniciarSer(punto &Ser)
{// srand( (unsigned)time( NULL ) );
Ser.nro=1;
Ser.dx =50;
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 55
Ser.dy =20;
Ser.longitud =15;
Ser.dmenor=1000;
for ( int col=0;col<Ser.longitud ;col++)
{ Ser.X[col] =40-col;
Ser.Y[col] =12;
Ser.color[col]=col+1;
}
}
void ImprimirSer(punto Ser)
{ printf("\n ser %d i x1 y1 x2,y2, color ",Ser.nro);
for (int i=0;i<Ser.longitud ;i++)
printf("\n %d %d %d %d %d %d ",i,Ser.X[i], Ser.Y[i], Ser.dx, Ser.dy ,
Ser.color);
}

void MostrarSer(punto Ser, int modo )
{
for ( int col=0;col<Ser.longitud ; col++)
{ gotoxy(Ser.X[col],Ser.Y[col]);
if (modo>0) textcolor(0,Ser.color[col]); else textcolor(0,0);
printf("%c",219);
}
gotoxy(Ser.X[0],Ser.Y[0]); printf("%d",Ser.nro );
}

void MostrarSerMatriz(punto Ser, int modo , int Matriz[][maxcol] )
{ int valor;
if (modo>0) valor= 1; else valor =0;
for ( int col=0;col<Ser.longitud ; col++)
Matriz[Ser.Y[col]][Ser.X[col]] =valor;
}

int ModificarSer (punto &Ser, int Matriz[][maxcol])
{ ///1
int xmenor, ymenor, x,y,x1,y1,x2,y2, resultado=0,largo=0,col;
float d=1, dmenor;
x1=Ser.X[0]; // cabeza
y1=Ser.Y[0];
x2=Ser.dx ;
y2=Ser.dy;
largo=Ser.longitud;
dmenor=Ser.dmenor;
xmenor=x1; ymenor=y1;
for (x=x1-1; x<=x1+1;x++)
for (y=y1-1;y<=y1+1;y++)
{ // 2
if (x==y) continue;
if (Matriz[y][x]==1) continue;
d= distancia( x,y,x2,y2);
if (d<dmenor)
{ // 3
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 56
dmenor=d; xmenor = x, ymenor=y; resultado=1;
} // 3
} // 2
// pasar los datos a los demas puntos
for (col=largo; col>0;col--)
{ // 2
Ser.X[col]=Ser.X[col-1];
Ser.Y[col]=Ser.Y[col-1];
} // 2

Ser.X[0]=xmenor;
Ser.Y[0]=ymenor;
Ser.dmenor=dmenor;
return resultado;
}


#include <windows.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include "e:\datos\milib.h"
int xmin=3, ymin=3, xmax=78, ymax=23;
#include "calculos.h"
int main()
{ ///1
int nro=0,nf=23,nc=78,fin=1,rx,ry,resultado=0;
IniciarMatriz (Matriz,maxfilas,maxcol);
IniciarSer(Ser);
//ImprimirSer(Ser);
MostrarSerMatriz(Ser,1,Matriz );
MostrarSer(Ser,1);
while (!kbhit())
{ /// 2
while (Ser.dmenor >1)
{ ////3
MostrarSerMatriz(Ser,0,Matriz );
MostrarSer(Ser,0);
// MostrarMatriz(Matriz,nf,nc);
resultado=ModificarSer(Ser,Matriz);
// if (resultado >0)
MostrarSer(Ser,1);
MostrarSerMatriz(Ser,1,Matriz );
Sleep(50);
} /// 3
MostrarSerMatriz(Ser,0,Matriz );
MostrarSer(Ser,0 );
for (int col=1; col<Ser.longitud ;col++)
{
Ser.X[col]=Ser.X[col-1];
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 57
Ser.Y[col]=Ser.Y[col-1];
} // 2
Ser.X[0]=Ser.dx;
Ser.Y[0]=Ser.dy;
fin =1;
while(fin==1)
{ ////3
rx=1+rand()%(xmax-xmin);
ry=1+rand()%(ymax-ymin);
if (Matriz[ry][rx] ==0)
{Ser.dx=rx; Ser.dy=ry; fin=0; }
} ////3
// modificamos los valores del ser
Ser.dmenor=1000;
MostrarSerMatriz(Ser,1,Matriz );
} // del otro while ///2
getch();
return 0;
} // 1

Varios seres de diferentes colors



Plan dE practicas 24 de julio del 2013
Pase de parametros por valor
Pase de parametros por referncia
Pase de parametros usando referencias
Ingresar y mostrar un vector usnado arreglos
Ingresar y mostrar un vector usando punteros
Ingresar y mostrar una matriz usando arreglos
Ingresar y mostrar una matriz usando arreglos de punteros
Ingresar y mostrar una matriz usando doble puntero
Punteros a funciones
Punteros a arreglos de funciones . tortuga caminado
Funciones que devuelven punteros
Pruebe de archivos simulacion de corte
Cadenas estructuras archivos leer y escribir un archivo de texto cualquiera
Leer y escribir un archivo con delimitadores
Pase de parametros por parametros por valor y referencia

Pase de parametros por valor


#include <conio.h>
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 58
#include <stdio.h>
void cambiar( int x , int y)
{ int temp= x; x= y; y= temp;}
int main()
{ int a=10, b=50;
printf("\n ANTES a=%d b=%d ", a,b);
cambiar (a,b);
printf("\n DESPUES a=%d b=%d ", a,b);
getch();
}

PASE de pareametros por referncia


#include <conio.h>
#include <stdio.h>
void cambiar( int *x , int *y)
{ int temp= *x; *x= *y; *y= temp;}
int main()
{ int a=10, b=50;
printf("\n ANTES a=%d b=%d ", a,b);
cambiar (&a,&b);
printf("\n DESPUES a=%d b=%d ", a,b);
getch();
}

Pase de parametros usando referencias


#include <conio.h>
#include <stdio.h>
void cambiar( int &x , int &y)
{ int temp= x; x= y; y= temp;}
int main()
{ int a=10, b=50;
printf("\n ANTES a=%d b=%d ", a,b);
cambiar (a,b);
printf("\n DESPUES a=%d b=%d ", a,b);
getch();
}
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 59
// ingresar y mostrar un vector con arreglos

#include <stdio.h>
#include <conio.h>
int const max=3;
void Ingresar(int A[],int ne);
void Mostrar(int A[],int ne);
int main()
{ int Vector[max],ne;
printf("\n cuantos datos quiere ingresar "); scanf("%d", &ne);
Ingresar(Vector,ne);
printf("\n los datos ingresados son ");
Mostrar(Vector,ne);
getch();
}
void Ingresar(int A[],int ne)
{printf("\n Ingrese %d elementos ",ne);
for (int fila=0; fila<ne; fila++)
scanf("%d", &A[fila]);
}
void Mostrar(int A[],int ne)
{ for (int fila=0; fila<ne; fila++)
printf("%4d", A[fila]);
}



Ingresar y mostrar un vector con punteros


Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 60
#include <stdio.h>
#include <conio.h>
int const max=3;
void Ingresar(int *A,int ne);
void Mostrar(int *A,int ne);
int main()
{ int *Vector,ne;
printf("\n cuantos datos quiere ingresar "); scanf("%d", &ne);
Vector=new int [ne];
printf("\n la direccion del vector es %d", &Vector);
printf("\n el valor es %d", Vector);
printf("\n *vector %d", *Vector);
Ingresar(Vector,ne);
printf("\n los datos ingresados son ");
Mostrar(Vector,ne);
getch();
}
void Ingresar(int *A,int ne)
{printf("\n Ingrese %d elementos ",ne);
for (int fila=0; fila<ne; fila++)
scanf("%d", &*(A+fila));
}
void Mostrar(int *A,int ne)
{ for (int fila=0; fila<ne; fila++)
printf("\n %d %d %4d", fila ,A+fila,*(A+fila));
}

Ingreso de matriz con arreglos de punteros

#include <stdio.h>
#include <conio.h>
int const maxfilas=5;
void IngresarMatriz(int *A[],int nf,int nc);
void MostrarMatriz(int *A[],int nf, int nc);
void limpiarMatriz(int *A[], int nf , int nc);

int main()
{ int *Matriz[maxfilas],nf,nc;
printf("\n cuantos filas tiene la matriz "); scanf("%d", &nf);
printf("\n cuantos columnas tiene la matriz "); scanf("%d", &nc);
IngresarMatriz(Matriz, nf,nc);
printf("\n los datos ingresados son "); MostrarMatriz(Matriz,nf,nc);
getch();
limpiarMatriz(Matriz, nf , nc);
}
void IngresarMatriz(int *A[],int nf,int nc)
{ int fila,col;
printf("\n Ingrese %d elementos por fila ",nc);
for ( fila=0; fila<nf; fila++)
{ A[fila]= new int [nc];
printf("\n fila %d", fila);
for ( col=0; col<nc; col++) scanf("%d", &*(A[fila]+col));
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 61
}
}
void MostrarMatriz(int *A[],int nf,int nc)
{ for (int fila=0; fila<nf; fila++)
{ printf("\n");
for (int col=0; col<nc; col++)
printf("%4d", *(A[fila]+col));
}
}
void limpiarMatriz(int *A[], int nf , int nc)
{ for (int fila=0;fila<nf;fila++) delete [] A[fila];
}




Matriz con filas variables


#include <stdio.h>
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 62
#include <conio.h>
int const maxfilas=5;
void IngresarMatriz(int *[],int , int []);
void MostrarMatriz(int *A[],int ,int []);
void limpiarMatriz(int *A[], int nf );

int main()
{ int *Matriz[maxfilas],nf,nc;
int Col[maxfilas];
printf("\n cuantos filas tiene la matriz "); scanf("%d", &nf);
IngresarMatriz(Matriz, nf,Col);
printf("\n los datos ingresados son "); MostrarMatriz(Matriz,nf,Col);
getch();
limpiarMatriz(Matriz, nf );

}
void IngresarMatriz(int *A[],int nf, int Col[])
{ int fila,col,necol;
for ( fila=0; fila<nf; fila++)
{ printf("\n cuantas columnas tiene la fila %d ", fila);
scanf("%d", &necol); Col[fila]=necol;
A[fila]= new int [necol];
for ( col=0; col<necol; col++) scanf("%d", &*(A[fila]+col));
}
}
void MostrarMatriz(int *A[],int nf, int Col[])
{ for (int fila=0; fila<nf; fila++)
{ printf("\n");
for (int col=0; col<Col[fila]; col++)
printf("%4d", *(A[fila]+col));
}
}
void limpiarMatriz(int *A[], int nf )
{ for (int fila=0;fila<nf;fila++) delete [] A[fila];
}



#include <conio.h>
#include <stdio.h>
int factorial(int );
int main()
{ int n;
int (*p) (int) ;// p es puntero a una funcin
p=factorial;
printf("\n ingrese n "); scanf("%d", &n);
printf("\n factorial es %d",(*p)(n));
getch();
}
int factorial(int n )
{ int f=1, i;
for (i=1;i<=n;i++)
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 63
f=f*i;
return f;
}





#include <conio.h>
#include <stdio.h>
int factorial(int );
int main()
{ int n,r;
int (*p) (int) ;// p es puntero a una funcin
p=factorial;
printf("\n ingrese n y r"); scanf("%d%d", &n, &r);
printf("\n combinatoria es %d", (*p)(n)/ ( (*p)(r)* (*p)(n-r)));
getch();
}
int factorial(int n )
{ int f=1, i;
for (i=1;i<=n;i++)
f=f*i;
return f;
}



#include <conio.h>
#include <stdio.h>
int factorial(int );
int main()
{ int n,r;
int (*p) (int) ;// p es puntero a una funcin
p=factorial;
printf("\n ingrese n y r"); scanf("%d%d", &n, &r);
printf("\n combinatoria es %d", (*p)(n)/ ( (*p)(r)* (*p)(n-r)));
getch();
}
int factorial(int n )
{ int f=1, i;
for (i=1;i<=n;i++)
f=f*i;
return f;
}
Practicas de lenguaje de programacin ABRIL AGOSTO 2013 Parte 2 \ Ismael Vliz 64



#include <conio.h>
#include <stdio.h>
#include "E:\datos\milib.h"

char *numeros[10]={"CERO","UNO", "DOS", "TRES", "CUATRO", "CINCO", "SEIS",
"SIETE","OCHO", "NUEVE"};
int main()
{ char *cad=numeros[4];
int fila,col;
int paso=7;
for (col=0;col<10;col++)
{ cad=numeros[col];
textcolor(0,10);
for ( fila=0; *(cad+fila)!='\0' ; fila++)
{ gotoxy( col*paso+1,fila+1); printf ( " %c",* (cad+fila));
}
textcolor(0,12);
gotoxy( col*paso+1,fila+1); printf ( "nlet %d", fila);
}
// printf("\n nletras %d ",i);
getch();
}


Ver el problema de simualcion de corte

Você também pode gostar