Wed Mar 04, 2009 7:17 pm
#include<stdio.h>
# define LEN 20
# define NUM 10
int readFromFile(char text[NUM][LEN]) {
FILE *fp;
int i;
if ((fp = fopen("input.txt", "r")) == NULL) {
printf("Error opening file");
return -1;
}
for (i = 0; i < NUM; i++) {
fscanf(fp, "%s..n", text[i]);
}
fclose(fp);
return 0;
}
void selectionSort(char text[NUM][LEN]) {
int i, j;
int min;
for(i = 0; i < NUM -1; i++)
{
min = i;
for(j = i + 1; j < NUM; j++)
{
CompareStrings(text[i], text[i+1]);
if(CompareStrings(text[i], text[i+1]) == -1);
{min = j;}
}
SwapStrings(text[i], text[i+1]);
}
return;
}
int CompareStrings(char s1[10], char s2[10])
{
int i;
for(i = 0; i < 10; i++)
{
if (s1[i] > s2[i])
{ return (1);}
if (s1[i] < s2[i])
{return (-1);}
if (s1[i] == 0 || s2[i] == 0)
{break;}
}
return 0;
}
void SwapStrings(char s1[10], char s2[10])
{
int i;
char c;
for(i = 0; i <10; i++)
{ c = s1[i];
s1[i] = s2[i];
s2[i] = c;
}
}
void SwapStrings(char s1[10], char s2[10]);
int CompareStrings(char s1[10], char s2[10]);
int readFromFile(char text[NUM][LEN]);
void selectionSort(char text[NUM][LEN]);
int main() {
char input[NUM][LEN];
int i;
if(readFromFile(input) == -1) {
printf("Error reading from file..n");
return -1;
}
readFromFile(input);
printf("UNSORTED ARRAY: ..n");
for(i = 0; i<10; i++)
{printf("%s", input[i]);
printf("..n");}
selectionSort(input);
printf("..nSORTED ARRAY: ..n");
for(i = 0; i<10; i++)
{printf("%s", input[i]);
printf("..n");}
return 0;
}
Thu Mar 05, 2009 10:08 pm
#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;
}
Thu Mar 05, 2009 10:12 pm
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.