GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #11  
Old 18-Apr-2005, 15:53
glulu76 glulu76 is offline
New Member
 
Join Date: Apr 2005
Posts: 22
glulu76 is on a distinguished road

Ok I think I know what I was doing wrong.


I think I got the bucket sort working. I think I know what I was doing wrong. I was trying to make the array and the sort seperate but really they kind of go together. Could someone just look over my code and please help me figure why it is not displaying the duplicates as duplicates. I added a while loop to try to catch them before they reached the sorted array but it is not working. I don't know why because I am not getting any errors. Here is the code.
CPP / C++ / C Code:
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define N 20

int main( void )
{


int unsorted[N] , bucket[10][N]={{0}} , sorted[N] ;
int   j , k , m , p ,z, flag = 0 ;

srand(time(NULL));
z=0;
while (z<20){
for(k=0 ;  k < N ; k++){
unsorted[k] =  1.0 + (double)rand()/(double)(RAND_MAX + 1) * 99.00;
//printf("%d\n", unsorted[k]);
if (unsorted[k] == z)

	{
		cout<<"This is a duplicate number "<<z<<"\n";
	}
	else
	{
		sorted[k]=unsorted[k];
		cout<<unsorted[k]<<" ";
		z++;
}
}
}
cout<<"\n\n";
for(p=1; flag != N ; p*=10){

flag = 0;

for(k=0;k<N;k++){
bucket[(sorted[k]/p)%10][k] = sorted[k];
if ( (sorted[k]/p)%10  == 0 ){
   flag++;
   }
}

if (flag == N){
   //printf("\n\n\n");
   for(j=0 ;  j < N ; j++){
  // printf("%d\n", sorted[j]);
	   cout<<sorted[j]<<" ";
   }
   return(EXIT_SUCCESS);
   }

for(j=0,m=0;j<10;j++){
for(k=0;k<N;k++){
if( bucket[j][k] > 0 ){
  sorted[m] = bucket[j][k];
  bucket[j][k] = 0 ;
  m++;
  }
}
}

}

return EXIT_SUCCESS ;
}

  #12  
Old 18-Apr-2005, 16:33
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by glulu76
Could someone just look over my code and please help me figure why it is not displaying the duplicates as duplicates. I added a while loop to try to catch them before they reached the sorted array but it is not working. I don't know why because I am not getting any errors. Here is the code.

I am having a heckofa time trying to follow what you are attempting. Maybe you can print out the progress through the generation loop and see if it's what you had in mind.

You could try something like this:

CPP / C++ / C Code:
  while (z < 20){
    for(k=0 ;  k < N ; k++){
      unsorted[k] =  1.0 + (double)rand()/(double)(RAND_MAX + 1) * 99.00;
      cout << "z = " << z << ", unsorted["<< k << "] = " << unsorted[k];
      if (unsorted[k] == z) {
          cout<<"   This is a duplicate number "<<z<<"\n";
        }
      else {
        sorted[k]=unsorted[k];
        cout << "   sorted[" << k << "] = " << sorted[k] << endl;
        z++;
      }
    }
  }

There is no way that I can see how to "fix" it, since I can't guess what it is supposed to be doing. I can see that, sooner or later, you end up with 20 numbers between 1 and 99 in array "unsorted" and 20 or fewer numbers in array "sorted", but I can't for the life of me figure out why you are doing this. I can see nothing here that tests to see if the new number is a duplicate of a previous number.


Regards,

Dave
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Merge sort on a linked list Temujin_12 C++ Forum 1 06-Mar-2008 21:33
bucket sort problem ap6118 C++ Forum 3 15-Apr-2005 19:02
[GIM] gim.h dsmith C Programming Language 0 18-Jan-2005 09:48
insert sort saphir55 C Programming Language 4 06-Dec-2004 15:00
help with Sort arrays/Size justachessgame C Programming Language 1 13-Nov-2004 00:46

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:38.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.