///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;
}

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!

fflush(stdin); Un-oh.

6000 or 1000?

Could you attach the file to be sorted?

What the heck is a supposed to be?

char a[0][1000];

Does this compile for you? (Please compile C code with a C compiler.)

> 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.

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

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.