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