Skip to main content
added 280 characters in body
Source Link
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. Your swaps function ends with the assignment of s and t to the same string, We could do something like

void swaps(char*s,char*t)
{
    char* temp = s;
    s = t;
    t = temp;
}

Which actually exchanges the values of the s and t pointers, but it does not work either because we are not able to change the memory addresses that point a[i] and a[i + 1], ie the output would be the same as the input

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

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

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. Your swaps function ends with the assignment of s and t to the same string, We could do something like

void swaps(char*s,char*t)
{
    char* temp = s;
    s = t;
    t = temp;
}

Which actually exchanges the values of the s and t pointers, but it does not work either because we are not able to change the memory addresses that point a[i] and a[i + 1], ie the output would be the same as the input

a simple way is to avoid this function and do a swap directly on main. 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;
}
deleted 59 characters in body
Source Link
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;
}

By the way this question could be considered out of topic

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

By the way this question could be considered out of topic

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;
}
added 155 characters in body
Source Link
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;
}

By the way this question could be considered out of topic

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

By the way this question could be considered out of topic

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

By the way this question could be considered out of topic

added 49 characters in body
Source Link
MARS
  • 5.2k
  • 3
  • 14
  • 23
Loading
Post Undeleted by MARS
Post Deleted by MARS
Source Link
MARS
  • 5.2k
  • 3
  • 14
  • 23
Loading