944,198 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1473
  • C RSS
Jun 10th, 2006
0

I need Help with Sorting Program

Expand Post »
  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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ds_matrix is offline Offline
1 posts
since Jun 2006
Jun 11th, 2006
0

Re: I need Help with Sorting Program

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.)
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 11th, 2006
0

Re: I need Help with Sorting Program

> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jun 15th, 2006
0

Re: I need Help with Sorting Program

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
duckmurder is offline Offline
2 posts
since Jun 2006
Jun 15th, 2006
2

Re: I need Help with Sorting Program

Quote 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?
Team Colleague
Reputation Points: 1135
Solved Threads: 173
Super Senior Demiposter
Rashakil Fol is offline Offline
2,480 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Any windows Graphics programmers?
Next Thread in C Forum Timeline: fprintf ()`





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC