0

I come from python and am trying to learn C, but I've only yesterday started with this pointer stuff. I wrote an inverted sorting algorithm that's supposed to take an unsorted array, then in each pass pick the highest and lowest elements, put them in each end then recursively do the same ignoring already sorted elements. The problem is I always get some error (different errors depending on what I try to change) which I can't fix regarding pointer types. Can you guys help?


#include <stdio.h>


void inv_cocksort(int *arr[], int first, int last);

int main(void)
{
    int unsorted[11] = {3, 1, 4, 5, 4, 2, 6, 9, 2, 8, 7};
    int unsorted_length = 11;

    for (int i = 0; i < unsorted_length; i++)
    {
        printf("%i", unsorted[i]);
    }

    inv_cocksort(&unsorted, 0, unsorted_length-1);

    for (int i = 0; i < unsorted_length; i++)
    {
        printf("%i", unsorted[i]);
    }


    void inv_cocksort(int *arr[], int first, int last)
    {
        if (first > last)
        {
            return;
        }
        else
        {
            for (int i = 0; i < last-1; i++)
            {
                if (arr[i] < arr[last])
                {
                    int temp = *arr[last];
                    *arr[i] = *arr[last];
                    *arr[last] = temp;
                }
                if (arr[i] > arr[first])
                {
                    int temp2 = *arr[first];
                    *arr[i] = *arr[first];
                    *arr[last] = temp2;
                }
            }
            inv_cocksort(&arr[], first+1, last-1)
        }

}
3
  • Which error do you get when you do what? Commented Apr 6, 2020 at 5:37
  • I think you need to change for (int i = 0; i < last-1; i++) to for (int i = first; i < last-1; i++) or similar Commented Apr 6, 2020 at 5:39
  • I change how I define the function; I try to define the first argument as int *unsorted[] to tell it to accept and address and pass &unsorted to it. I get errors like invalid pointer type and changed int into pointer without casting. Can't get it to work. Commented Apr 6, 2020 at 5:40

3 Answers 3

1

I think you need to change

for (int i = 0; i < last-1; i++)

to

for (int i = first+1; i < last-1; i++)

or similar.

Sign up to request clarification or add additional context in comments.

1 Comment

What makes you think so?
1

There are many issues:

You want this:

#include <stdio.h>


void inv_cocksort(int arr[], int first, int last)
{
  if (first > last)
  {
    return;
  }
  else
  {
    for (int i = 0; i < last - 1; i++)
    {
      if (arr[i] < arr[last])
      {
        int temp = arr[last];
        arr[i] = arr[last];
        arr[last] = temp;
      }
      if (arr[i] > arr[first])
      {
        int temp2 = arr[first];
        arr[i] = arr[first];
        arr[last] = temp2;
      }
    }
    inv_cocksort(arr, first + 1, last - 1);
  }
}

int main(void)
{
  int unsorted[11] = { 3, 1, 4, 5, 4, 2, 6, 9, 2, 8, 7 };
  int unsorted_length = 11;

  for (int i = 0; i < unsorted_length; i++)
  {
    printf("%i ", unsorted[i]);
  }

  inv_cocksort(unsorted, 0, unsorted_length - 1);

  for (int i = 0; i < unsorted_length; i++)
  {
    printf("%i ", unsorted[i]);
  }
}

This compiles at least without warnings, but it doesn't work correctly.

1 Comment

I can fix the logic later, really, just need to get it to compile in the first place
0

Fixed it.

I had firstly added a whole bunch of starts and &s because I was afraid the function would only receive a copy of the passed values, order that copy and lose it as soon as the stack memory was no longer allocated to it. It turns out array names actually work as pointers to the first value even if the arrays are not strings, so I didn't really have to manipulate any memory at all.

Fixed the logic also, I'm sorry for uploading such a buggy code at first.

And cocksort is because it's cocktail shaker, not quite but maybe? idk.


#include <stdio.h>


void inv_cocksort(int arr[], int first, int last);

int main(void)
{
    int unsorted[11] = {3, 1, 4, 5, 4, 2, 6, 9, 2, 8, 7};
    int unsorted_length = 11;

    for (int i = 0; i < unsorted_length; i++)
    {
        printf("%i", unsorted[i]);
    }
    printf("\n");

    inv_cocksort(unsorted, 0, unsorted_length-1);

    for (int j = 0; j < unsorted_length; j++)
    {
        printf("%i", unsorted[j]);
    }
}


    void inv_cocksort(int arr[], int first, int last)
    {
        if (first > last)
        {
            return;
        }
        else
        {
            for (int i = first; i <= last; i++)
            {
                if (arr[i] < arr[last])
                {
                    int temp = arr[last];
                    arr[last] = arr[i];
                    arr[i] = temp;
                }
                if (arr[i] > arr[first])
                {
                    int temp2 = arr[first];
                    arr[first] = arr[i];
                    arr[i] = temp2;
                }
            }
            inv_cocksort(arr, first+1, last-1);
        }
    }

2 Comments

If you´re at your goal accept an answer. By the way I would not name it "cocksort" - if you want to abbreviate "cocktail shaker" then rather name it "cockt_sort". ;-)
thanks for the feedback. I did upvote the answers, I didn't accept any specific one because none of them were what I was looking for, which was further clarification on how pointers and memory addresses work in C when in the context of arrays and being called as function arguments.

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.