Skip to main content
4 of 5
deleted 59 characters in body
MARS
  • 5.2k
  • 3
  • 14
  • 23

Here I see a problem with the swaps function and the indexes of a [i], as I believe on the return of the swaps function is lost control of the indexes i, a simple way is to avoid this function and do a swap directly on main. It is also possible that the indexes of the for loops do not work correctly (although I have not checked it), it seems to me more secure unrelated indexes. I wrote this version of your program that I think works well:

#include <stdio.h>
#include <string.h>
#include <cs50.h>
#include <string.h>

int main(void)
{
    int n = GetInt();
    char* a[n];
    int c = 0;
    for(int i = 0; i < n; i++)
    {
        a[i] = GetString();
    }
    puts("");
    for(int j = 0; j < n; j++)                                  
    {
        for(int i = 0; i < n; i++)
        {
            if(j != i)
            {
                c = strcmp(a[i],a[j]);
                if(c > 0)
                {
                    char* temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }        
        }
    }
    for(int i = 0; i < n; i++)
    {
         printf("%s\n", a[i]);
    }
    return 0;
}
MARS
  • 5.2k
  • 3
  • 14
  • 23