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

Dani AI

Generated

A few quick, concrete notes that build on and :

  • is right about the stray semicolon and the bad format string; correctly describes the selection-sort logic you want. Beyond those, the program has inconsistent buffer sizes (you defined LEN but many functions use 10), a redundant second call to readFromFile in main, and unsafe reading with fscanf("%s") (plus your "%s..n" is not an escape sequence). Also make sure prototypes or headers are visible before main and include <string.h> and <stdlib.h> when you use strcmp/qsort.

Use safe line input (handles spaces and prevents overflow):

if (fgets(text[i], LEN, fp) == NULL) break;
text[i][strcspn(text[i], "\n")] = '\0';

(Requires #include <string.h>; LEN must include room for the terminating '\0'.)

If you prefer to avoid writing CompareStrings and SwapStrings, use the standard qsort on an array of pointers — this avoids copying strings when swapping:

int cmp(const void *a, const void *b) {
    return strcmp(*(const char * const *)a, *(const char * const *)b);
}

/* build pointer array: for (i=0; i<count; ++i) ptrs[i] = text[i]; */
qsort(ptrs, count, sizeof ptrs[0], cmp);

If you keep selection sort, follow this checklist: use min and j (not i and i+1) for comparisons, do NOT put a semicolon immediately after the if, perform the swap after the inner loop, and always use LEN/NUM constants instead of magic numbers. When swapping fixed-size arrays, copy via a temporary buffer sized LEN (or swap pointers if you use a pointer table). Finally, check return values on file I/O and stop reading on EOF so the program works correctly with fewer than NUM lines.

Recommended Answers

All 2 Replies

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.