0

I am really new to programming. i've tried to make a program that will sort an array using two functions but i just can't make it to work, i don't know what i did wrong.

#include <stdio.h>
#include <stdlib.h>


int sort();
int sp();
int x;


int main()
{
    int v[20],k;
    scanf("%d",&x);
    for (k=1;k<=x;k++)
    scanf("%d",&v[k]);
    sort(&v[20]);
    for (k=1;k<=x;k++)
        printf("%d",v[k]);
    return 0;
}


int sort (int *a[20])
{
    int i,j;
for (i=1;i<=x;i++)
    for (j=1;j<=x;j++)
    {if(*a[j]>*a[j+1])
        sp(&a[j],&a[j+1]);
    }

}

int sp(int *l, int *m)
{
    int n=*l;
    *l=*m;
    *m=n;
}

I tested the swaping part and that works ( the sp function ), but i don't know how to work with arrays in functions.

When i test the program, there is no output, the program just stops.

4
  • @JohnColeman i initialized x before main so it can be global variable, as i used it both in main and sort function. Commented Dec 14, 2019 at 18:32
  • 3
    You have entirely too much indirection in your sort and arguments passed to thereof. And whatever tutorial is telling you to declare arbitrary argument list function decls should really be replaced with something from the last decade or two. Finally, both sort and sp claim to return int, then fail to deliver on that promise. Not that anyone is using that result anyway. Commented Dec 14, 2019 at 18:33
  • @WhozCraig Ok, so what should i change for my algorithm to work? Commented Dec 14, 2019 at 18:43
  • Honestly, almost everything. The one thing in this that will work is the sp function, assuming the return type is changed to void (since it doesn't actually return anything). Commented Dec 14, 2019 at 18:48

1 Answer 1

1

Among the problems in the posted code:

  1. Indexing is incorrectly 1-based. C is a zero-based indexing system. An array of 20 elements is indexed using [0] through [19].
  2. Arbitrary function argument declarations. In C, int foo(); declares a function called foo that returns int and takes an arbitrary list. Therefore, foo(), foo(1), foo(1,2,3), foo("grandma's house", 3.14), all are compliant. Your function declaration should prototype exactly what you're expecting. In this case, void sort(int a[], int len);
  3. Improper pointer indirection. Your sort function should take a pointer and length as arguments. As discussed before, however, your declaration argument list is arbitrary, and your implementation actually takes a pointer-to-pointer (int *a[], which is equivalent to int **a).
  4. Check your IO input operations. Validate your input operations. Don't assume they work. Assumption is the mother of all...
  5. Boundary checks. Make sure you never overrun your array boundaries by verifying the input data won't allow that to happen in the first place.

Each of these issues, and more, is addressed below:

#include <stdio.h>
#include <stdlib.h>

void sort(int a[], int len);
void sp(int *l, int *m);

int main()
{
    int x, v[20];

    if (scanf("%d", &x) == 1 && x > 0 && x <= 20)
    {
        for (int i = 0; i < x;)
        {
            // prompt for input
            printf("a[%d] :", i);
            fflush(stdout);

            // try to read next element
            if (scanf("%d", v + i) != 1)
            {
                // failed to read. clear stdin through newline
                int c;
                do {
                    c = fgetc(stdin);
                } while (c != EOF && c != '\n');

                // if we actually hit EOF, stop reading and
                //  reset x to however many items we read well.
                if (c == EOF)
                {
                    x = i;
                    break;
                }

                continue;
            }

            // read successful, move to next element
            ++i;
        }

        for (int i = 0; i < x; ++i)
            printf("%d ", v[i]);
        fputc('\n', stdout);

        sort(v, x);

        for (int i = 0; i < x; ++i)
            printf("%d ", v[i]);
        fputc('\n', stdout);
    }

    return 0;
}


void sort(int a[], int len)
{
    if (len <= 1)
        return;

    int swapped = 1;
    while (swapped && len-- > 0)
    {
        swapped = 0;
        for (int i = 0; i < len; ++i)
        {
            if (a[i + 1] < a[i])
            {
                sp(a + i, a + i + 1);
                swapped = 1;
            }
        }
    }
}

void sp(int *l, int *m)
{
    int n = *l;
    *l = *m;
    *m = n;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.