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.

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

Prototypes go at the top of the program (usually).

You have an extraneous semicolon at the end of this line which causes it to not control the next line as you expect it to: if(CompareStrings(text[i], text[i+1]) == -1); Also, the indices you are using in the above line are entirely incorrect. They should be j and min, not i and i+1.

You seem to think that the "..n" gives a newline. Actually it should be "\n" (backslash n).

BTW, half the places where you use the number 10 you actually mean 20. If you had used your defined constants LEN and NUM you wouldn't have had that problem.

Hey assuming you want to sort your strings in order "smaller to larger" as you have been using the word "min" many times these are the things you need to do.

1>Correct every mistake nucleon has already stated.

2>Your selection sort function is a completely mess. You are comparing text and text[i+1] and setting value of j to min which doesn't make any sense.It looks like this with errors marked:

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]); 
/*Error : You need to compare text[min] and text[j] */
if(CompareStrings(text[i], text[i+1]) == -1); 
/* Why extra comparison mentioned above??? How ever this thing isn't doing anything*/ 
{min = j;} /*j isn't into picture at all*/
}
SwapStrings(text[i], text[i+1]); 
/* why running a loop with j when u r just doing this?*/
}
return;
}

It should be as this :

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++)
     {
          if(CompareStrings(text[min],text[j]) == 1) min = j;
     }
     SwapStrings(text[i],text[min]);
}
return;
}

These are the errors i saw when I read the program once.I haven't compiled it.Try this once and post your further queries.

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.