Question subject: Help with selection sort for strings?
Posted: Wed Mar 04, 2009 7:17 pm
Joined: Wed Mar 04, 2009 7:13 pm Posts: 1 Has thanked: 0 time Have thanks: 0 time
Ok so I have to turn in this C program today that is able to selectively sort a list of names from an input file, but when I compile it and run it, it doesn’t sort it correctly. Can anyone please help me here? I’m losing my mind to my computer.
[code] #include<stdio.h>
# define LEN 20 # define NUM 10
int readFromFile(char text[NUM][LEN]) {[size=85][/size]
Question subject: Re: Help with selection sort for strings?
Posted: Thu Mar 05, 2009 10:08 pm
Joined: Fri Nov 21, 2008 6:18 pm Posts: 51 Location: thessaloniki Has thanked: 0 time Have thanks: 2 time
: well if i understand what you want to do correctly have a look at this example i think it will help you solve your problem ...
Code:
#include<string.h> int main() { int i,n,j; int bSwapped=1; char *names[100]; // your actual Array Declaration char* pstr; char temp[20]; // temperory Char Array to store the Readed Sring printf("Enter total no Of Students"); scanf ("%d",&n); for(i=0;i<n;i++) { printf("Enter the %d StudentName : ",(i+1) ); scanf("%s",temp); name[i] = strdup(temp); } while (bSwapped) { bSwapped=0; for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if(strcmp(name[i],name[j])>0) { pstr=name[i]; name[i]=name[j]; name[j]=pstr; bSwapped=1; } } // Print the Sorted List of Student names for(i=0;i<n;i++) printf("\n %s",name[i]); return 0; }
_________________ if you want make an effort yourself no one will make it for you... best regards
biskot188
Question subject: Re: Help with selection sort for strings?
Posted: Thu Mar 05, 2009 10:12 pm
Joined: Fri Nov 21, 2008 6:18 pm Posts: 51 Location: thessaloniki Has thanked: 0 time Have thanks: 2 time