I need Help with Sorting Program

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2006
Posts: 1
Reputation: ds_matrix is an unknown quantity at this point 
Solved Threads: 0
ds_matrix ds_matrix is offline Offline
Newbie Poster

I need Help with Sorting Program

 
0
  #1
Jun 10th, 2006
  1. ///Program for Select Sort
  2.  
  3. //Name: Sharan Shanmugaratnam
  4. //Date: June 6th, 2006
  5.  
  6. //Constants
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include <string.h>
  10. #include <time.h>
  11. #define SIZE 1000
  12.  
  13. int main (void){
  14. char x[1000][1000], y[1000][1000];
  15. int size;
  16. char a[0][1000];
  17. int sorting=0;
  18. FILE*file;
  19.  
  20. //Opens the file that contains the 1000 words.
  21. file = fopen("words1.txt","r");
  22.  
  23. //Reads the file that contains the 1000 words.
  24. for (int i=0;i<SIZE;i++){
  25. fscanf(file,"%s\n",&x[i]);
  26. }
  27. fclose(file);//Close the file that contains the 1000 words.
  28. char temp[81]="";
  29. int lower=0;
  30.  
  31. clock_t start=0, end=0; // clock_t is a special variable structure
  32. double cpu_time_used=0;
  33. start = clock();
  34.  
  35. for (int in=0;in<size;in++){
  36. lower=in;
  37. for (int d=in;d<size;d++){
  38. if (strcmp(a[d],a[lower])<=0)lower=d;
  39. }
  40. strncpy(temp,a[in],81);
  41. strncpy(a[in],a[lower],81);
  42. strncpy(a[lower],temp,81);
  43. }
  44. end = clock();
  45. cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
  46. printf ("\nSelection sort time: %f",cpu_time_used);
  47.  
  48.  
  49. fflush(stdin);
  50. getchar();
  51. return 0;
  52. }
The program takes an input from a txt file of 6000 words...it needs to sort those 6000 words and print the time it took to complete the sort..it is not working for some reason...and im really lost as to what I should do to fix it...please help!
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,457
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 251
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: I need Help with Sorting Program

 
0
  #2
Jun 11th, 2006
fflush(stdin); Un-oh.

6000 or 1000?

Could you attach the file to be sorted?

What the heck is a supposed to be?
  1. char a[0][1000];
Does this compile for you? (Please compile C code with a C compiler.)
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: I need Help with Sorting Program

 
0
  #3
Jun 11th, 2006
> char temp[81]="";
But apparently your words are up to 1000 characters long.
Fix this, or fix your arrays.

> y[1000][1000]
1MB of unused space, just get rid of it.

It seems to me that you're likely to be blowing away your entire stack (and much more) before you've even begun.
Start with a small file of only say 10 short strings and verify that your algorithm works. Then scale it up to full size.
Try it with a file of only TWO words, and step through the code with a debugger.

> if (strcmp(a[d],a[lower])<=0)
Try using the x array (the one you read into) rather than something completely useless liike your a array.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 2
Reputation: duckmurder is an unknown quantity at this point 
Solved Threads: 0
duckmurder duckmurder is offline Offline
Newbie Poster

Re: I need Help with Sorting Program

 
0
  #4
Jun 15th, 2006
Attempt to run the program after restarting your computer, if that does not work then make sure that your opperating system supports the program that you're attempting to run
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,055
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: I need Help with Sorting Program

 
2
  #5
Jun 15th, 2006
Originally Posted by duckmurder
Attempt to run the program after restarting your computer, if that does not work then make sure that your opperating system supports the program that you're attempting to run
Could you, um, please explain your reasoning?
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC