jueves, 10 de mayo de 2012

Código del Radix Sort.


using System;
using System.Collections.Generic;
namespace RadixSort
{
                class Radix
                {
                public void RadixSort(int[] a)
               
                   // Este es nuestro arreglo auxiliar.
                   int[] t=new int[a.Length];
                   
                   // Tamaño en bits de nuestro grupo.
                    // Intenta también 2, 8 o 16 para ver si es rápido o no.
                   int r=2; 
                   
                   // Número de bits de un entero en c#.
                   int b=32;
                   
                   // Inicia el conteo a asignación de los arreglos.
                    // Notar dimensiones 2^r el cual es el número de todos los valores posibles de un número r-bit.
                   int[] count=new int[1<<r];
                   int[] pref=new int[1<<r];
                   
                   // Número de grupos.
                   int groups=(int)Math.Ceiling((double)b/(double)r);
                   
                   // Máscara para identificar los grupos.
                   int mask = (1<<r)-1;
                   
                   // Implementación del algoritmo
                   for (int c=0, shift=0; c<groups; c++, shift+=r)
                    {
                       // Reiniciar el conteo en los arreglos.
                       for (int j=0; j<count.Length; j++)
                           count[j]=0;
               
                       // Contar elementos del c-vo grupo.
                       for (int i=0; i<a.Length; i++)
                           count[(a[i]>>shift)&mask]++;
               
                       // Calculando prefijos.
                       pref[0]=0;
                       for (int i=1; i<count.Length; i++)
                           pref[i]=pref[i-1]+count[i-1];
               
                       // De a[] a t[] elementos ordenados por c-vo grupo .
                       for (int i=0; i<a.Length; i++)
                           t[pref[(a[i]>>shift)&mask]++]=a[i];
               
                       // a[]=t[] e inicia otra vez hasta el último grupo.
                       t.CopyTo(a,0);
                      
                       Console.WriteLine("{0}",c);
                    }
                   // Está ordenado             
                }
                              
                               public static void Main(string[] args)

                               {
                                               int[] a = new int[7] {2,3,1,0,5,6,9};
                                              
                                               Console.WriteLine("Ordenamiento Radix");
                                              
                                               Radix O = new Radix();                                
                                               O.RadixSort(a);
                                              
                                               Console.ReadLine();
                               }
                }
}

Práctica de Interacción.


//Secccion de librerias
#include "stdafx.h"
#include <GL/glut.h>
#include <math.h>


//seccion de prototipos
void Inicializa();
void Dibuja();
void Teclado(unsigned char, int, int);
void Especial(int, int, int);
void Anima();
void Raton(int, int, int, int);
void Pasivo(int, int);
void Mueve(int, int);
void Tiempo(int);


//variables globales
float ambiental[4]={.6,.5,.3,0};
float difusa[4]={.3,.4,.7,0};
float especular[4]={1,1,1,0};
float posicion[4]={0,7,2,1};
float rx,ry,rz,sx=1,sy=1,sz=1,tx,ty,tz;


void main()
{
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(600, 600); 
glutInitWindowPosition(100,50); 
glutCreateWindow("Ejemplo Interactividad");
glutDisplayFunc(Dibuja);
Inicializa();
glutKeyboardFunc(Teclado);
glutSpecialFunc(Especial);
glutIdleFunc(Anima);
glutMouseFunc(Raton);
glutMotionFunc(Mueve);
glutPassiveMotionFunc(Pasivo);
glutTimerFunc(10,Tiempo,10);
glutMainLoop();
}


void Inicializa()
{
glClearColor(0.235235,0.235325,0.134,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90,1,0.1,50);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
glLightfv(GL_LIGHT0,GL_AMBIENT,ambiental);
glLightfv(GL_LIGHT0,GL_DIFFUSE,difusa);
glLightfv(GL_LIGHT0,GL_SPECULAR,especular);
glLightfv(GL_LIGHT0,GL_POSITION,posicion);
glLightf(GL_LIGHT0,GL_CONSTANT_ATTENUATION,1);
glLightf(GL_LIGHT0,GL_QUADRATIC_ATTENUATION,0.0001);
glLightf(GL_LIGHT0,GL_LINEAR_ATTENUATION,0.0001);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}


void Dibuja()
{
GLUquadricObj *p;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
p=gluNewQuadric();


gluQuadricDrawStyle(p,GLU_FILL);
gluQuadricNormals(p,GLU_SMOOTH);
gluQuadricOrientation(p,GLU_OUTSIDE);
glMaterialfv(GL_FRONT,GL_SPECULAR,especular);
ambiental[0]=0;ambiental[1]=1;ambiental[2]=0;
glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE, ambiental);
glMaterialf(GL_FRONT,GL_SHININESS,20);
glShadeModel(GL_SMOOTH);
glTranslated(-4,5,-15);
glRotated(rx,1,0,0);
glScaled(sx,sx,sx);
gluCylinder(p,1,1,4,20,20);
gluDeleteQuadric(p);


glLoadIdentity();
glMaterialfv(GL_FRONT,GL_SPECULAR,especular);
ambiental[0]=0;ambiental[1]=0;ambiental[2]=1;
glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE, ambiental);
glMaterialf(GL_FRONT,GL_SHININESS,20);
glShadeModel(GL_SMOOTH);
glTranslated(0,-5,-15);
glTranslated(tx,ty,tz);
glRotated(ry,0,1,0);
glScaled(sy,sy,sy);
glutSolidTeapot(2);


glLoadIdentity();
glMaterialfv(GL_FRONT,GL_SPECULAR,especular);
ambiental[0]=1;ambiental[1]=0;ambiental[2]=0;
glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE, ambiental);
glMaterialf(GL_FRONT,GL_SHININESS,20);
glShadeModel(GL_FLAT);
glTranslated(4,5,-15);
glRotated(rz,0,0,1);
glScaled(sz,sz,sz);
glutSolidTorus(1,2,10,10);


glutSwapBuffers();
}
void Anima()
{
rx+=.5;
glutPostRedisplay();
}


void Teclado(unsigned char t, int x, int y)
{
if(t=='c' || t=='C')
{
sx-=.1;
glutPostRedisplay();
}
if(t=='t' || t=='T')
{
sy-=.1;
glutPostRedisplay();
}
if(t=='o' || t=='O')
{
sz-=.1;
glutPostRedisplay();
}
}


void Especial(int t, int x, int y)
{
if(t==GLUT_KEY_RIGHT)
{
tx+=.2;
glutPostRedisplay();
}
if(t==GLUT_KEY_LEFT)
{
tx-=.2;
glutPostRedisplay();
}
if(t==GLUT_KEY_UP)
{
ty+=.2;
glutPostRedisplay();
}
if(t==GLUT_KEY_DOWN)
{
ty-=.2;
glutPostRedisplay();
}
if(t==GLUT_KEY_PAGE_DOWN)
{
tz-=.2;
glutPostRedisplay();
}
if(t==GLUT_KEY_PAGE_UP)
{
tz+=.2;
glutPostRedisplay();
}
}


void Raton(int t, int e, int x, int y)
{
if(t==GLUT_LEFT_BUTTON && e==GLUT_UP)
{
sx+=.1;
glutPostRedisplay();
}
if(t==GLUT_RIGHT_BUTTON && e==GLUT_UP)
{
sy+=.1;
glutPostRedisplay();
}
if(t==GLUT_MIDDLE_BUTTON && e==GLUT_UP)
{
sz+=.1;
glutPostRedisplay();
}
}


void Mueve(int x, int y)
{
rz-=1;
glutPostRedisplay();
}


void Pasivo(int x, int y)
{
rz+=1;
glutPostRedisplay();
}


void Tiempo(int p)
{
ry+=1;
glutPostRedisplay();
glutTimerFunc(p+1,Tiempo,p);
}

Examen TAP!!


1.- Desarrolle un programa en hilos, que ejecute dos métodos. Uno debe de calcular el perímetro de un círculo y el otro debe de calcular el perímetro de un triángulo.
2.- Desarrolle un programa en hilos, que ejecute dos métodos. Uno debe de mostrar la tabla de 7 y el otro debe de mostrar la secuencia de fibonacci.
3.- Desarrolle un programa en hilos, que ejecute dos métodos.Uno debe de calcular la fórmula de la aceleración y el otro, el tiro parabólico.