| | |
I need Help with Sorting Program
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2006
Posts: 1
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
///Program for Select Sort //Name: Sharan Shanmugaratnam //Date: June 6th, 2006 //Constants #include <stdio.h> #include <ctype.h> #include <string.h> #include <time.h> #define SIZE 1000 int main (void){ char x[1000][1000], y[1000][1000]; int size; char a[0][1000]; int sorting=0; FILE*file; //Opens the file that contains the 1000 words. file = fopen("words1.txt","r"); //Reads the file that contains the 1000 words. for (int i=0;i<SIZE;i++){ fscanf(file,"%s\n",&x[i]); } fclose(file);//Close the file that contains the 1000 words. char temp[81]=""; int lower=0; clock_t start=0, end=0; // clock_t is a special variable structure double cpu_time_used=0; start = clock(); for (int in=0;in<size;in++){ lower=in; for (int d=in;d<size;d++){ if (strcmp(a[d],a[lower])<=0)lower=d; } strncpy(temp,a[in],81); strncpy(a[in],a[lower],81); strncpy(a[lower],temp,81); } end = clock(); cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; printf ("\nSelection sort time: %f",cpu_time_used); fflush(stdin); getchar(); return 0; }
fflush(stdin); Un-oh.6000 or 1000?
Could you attach the file to be sorted?
What the heck is a supposed to be?
C Syntax (Toggle Plain Text)
char a[0][1000];
"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
> 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.
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.
•
•
•
•
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
All my posts may be redistributed under the GNU Free Documentation License.
![]() |
Similar Threads
- selection sorting issues (C)
- help with sorting program (C++)
- Need Help for identify the Sorting Action (VB.NET)
- C++ and AI (C++)
Other Threads in the C Forum
- Previous Thread: Any windows Graphics programmers?
- Next Thread: fprintf ()`
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures student suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h






