[C con Clase] no me corre el programa

German Ponce german.informatico en gmail.com
Jue Sep 20 21:48:30 CEST 2007


holas , les agradesco por querer ayudarme, estoy investigando algoritmos y
me encontre con uno llamado bucketsort , el cuales muy interesante ,
encontre un codigo que genera nuemors aleatorios y trate de convinarlos, el
problema al parecer es que algo hice mal al fusionar esos codigos y me
arroja problemas


este es el codigo:

#include <iostream>
#include <stdio.h>
#include <conio.h>
#define TAM 1
using namespace std;

void init_mm( );// inicializa en creador de numeros aleatorios
int number_range( int from, int to );
int number_mm( void );
static    int rgiState[2+55]; /* Dejar esto asi */


class element            //element
{
public:
    int value;
    element *next;
    element()
    {
    value=0;
    next=NULL;
    }
};

class bucket           //cada bucket tiene un rango especifico de valores

{
public:
element *firstElement;
bucket()
{
firstElement = NULL;
}
};
int array[TAM];
int main()
{
    int lowend=0;         // minimo elemento
    int highend=100000000;      //max elemento
    int interval=10;      //numero de intervalos
    const int noBuckets=(highend-lowend)/interval; //calculo de numeros de
buquet requeridos
    cout <<"el numero de buckets es:"<<noBuckets<<endl;
    bucket *buckets = new bucket[noBuckets];
    bucket *temp;

    for(int i=0;i<noBuckets;i++)   //creando cada uno de los bucket
    {
        temp=new bucket;
        buckets[i]=*temp;
        //cout<<"array["<<i<<"]="<<array[i]<<endl;
        //cout<<"*temp ="<<&temp<<endl;
    }

    cout<<"--------los elementos ordenados por el metodo Bucket sort son
------------------\n";
    init_mm(); /* inicia el generador */
    printf("Generando 10 numero aleatorios entre 0 y 100.000.000:\n");
    int array[TAM];
    for(int i=0;i<TAM;i++)
       array[i]=number_range( 1, 100000000 );
       cout<<"hasta aca ba bien"<<endl;
       //system ("pause");

    for(int j=0;j<TAM;j++)   //enviando elementos al bucket apropiado
    {
    //cout<<array[j]<<endl;
    element *temp1,*pre;
    temp1=buckets[array[j]/interval].firstElement;
        if(temp1==NULL)//si el elemto es el primero en el bucket
        {
            temp1=new element;
            buckets[array[j]/interval].firstElement=temp1;
            temp1->value=array[j];
        }
        else
        {
            pre=NULL;
                while(temp1!=NULL)     //al salir de este while queda
apuntando al lugar apropaido dentro del bucket
                   {
               /*
               El problema está en que si temp es NULL, no puedes comprobar
               temp->value>array[j], ya que para saber temp->value intenta
               dereferenciar un puntero nulo.
              Sugiero cambiar la condición del if por if  (temp!=NULL),
              en cuyo caso ya funciona :-)

              */
               if(temp1==NULL)//antes habia //if(temp->value>array[j])
                   break;// sale de s cilo si el elemento es menro que el
apuntado
                   pre=temp1;
                   temp1=temp1->next;
                   }

                //antes habia //if(temp->value>array[j])
                if(temp1==NULL) //if the new value is in betwen or at the
begining
                {
                    if(pre==NULL)  //inserta al principio si el bucket tiene
elementos ya
                    {
                        element *firstNode;
                        firstNode=new element();
                        firstNode->value=array[j];
                        firstNode->next=temp1;
                        buckets[array[j]/interval].firstElement=firstNode;
                    }
                    else  //insercion a la mitad
                    {
                        element *firstNode;
                        firstNode=new element();
                        firstNode->value=array[j];
                        firstNode->next=temp1;
                        pre->next=firstNode;
                    }
                }
                else// si el valor creado en el ultimo en el bucket
                {
                    temp1=new element;
                    pre->next=temp1;
                    temp1->value=array[j];
                }

        }
 }

    cout<<"------------------------la lista ordenada es---------------\n";
    for(int jk=0;jk<10;jk++)
    {
        element *temp;
        temp= buckets[jk].firstElement;
            while(temp!=NULL)
            {
                cout<<"*"<<temp->value<<endl;
                temp=temp->next;
            }
    }

cout<<"--------------------------------fin--------------------------------\n";

system("pause");

return 1;

}

  int number_mm( void )
    {
    int *piState;//puntero a int
    int iState1;
    int iState2;
    int iRand;
    piState        = &rgiState[2];
    iState1         = piState[-2];
    iState2         = piState[-1];
    iRand         = ( piState[iState1] + piState[iState2] )
            & ( ( 1 << 30 ) - 1 );
    piState[iState1]    = iRand;
    if ( ++iState1 == 55 )
    iState1 = 0;
    if ( ++iState2 == 55 )
    iState2 = 0;
    piState[-2]        = iState1;
    piState[-1]        = iState2;
    return iRand >> 6;
}//fin number-nm

/*
* Genera un numero aleatorio.
*/
int number_range( int from, int to )
    {
    int power;
    int number;
    if ( ( to = to - from + 1 ) <= 1 )
    return from;
    for ( power = 2; power < to; power <<= 1 )
    ;
    while ( ( number = number_mm( ) & ( power - 1 ) ) >= to )
    ;
    return from + number;
}//fin number_range

/*
* Este es el algoritmo de Mitchell-Moore del libro: Knuth Volume II.
*/
void init_mm( )
    {
    int *piState;
    int iState;
    piState    = &rgiState[2];
    piState[-2]    = 55 - 55;
    piState[-1]    = 55 - 24;
    piState[0]    = ( (int) time( NULL ) ) & ( ( 1 << 30 ) - 1 );
    piState[1]    = 1;
    for ( iState = 2; iState < 55; iState++ )


    {
        piState[iState] = ( piState[iState-1] + piState[iState-2] )
                & ( ( 1 << 30 ) - 1 );
    }
    return;
}//fin init_mm
------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: <http://listas.conclase.net/pipermail/cconclase_listas.conclase.net/attachments/20070920/020b0cfe/attachment.html>


Más información sobre la lista de distribución Cconclase