1

I'm really newbie to c programing, and I have no idea how can I make it work. I want to make sort function that sorting array of integers by using 2 argument. I want to use recursive on sort function. and sorting from end of array to first. by ascending order.

void  ft_sorting(int *arr, int size);

but I don't know what went wrong. It totally out of my understanding with pointer and array. right now I really have no idea. Can some one pointing what I did wrong. And what I need to learn and fix. Thank you!

void    ft_recure(int *a, int *b, int j, int k)
{
    if (--j >= 0)
    {
        if (a[k] < b[j])
        {
            a[k] = b[j];
        }
        else
        {
            ft_recure(a[k], b[j], j, k);
        }
    }
    else
        return a[k];
}

void    ft_sort(int *tab, int size)
{
    int i;
    int h;
    while (size > 0)
    {
        i = size;
        h = i;
        tab[size] = ft_recure(tab, tab, i, h);
        size--;
    }
}

and also I try this.

int  ft_recurs(int x, int y, int a, int b)
{
    int j;
    
    j = a;
  if( a > 0)
  {
    
    if(*x < *(y - 1);)
    {
        b = *(y - 1);
      *x = b;
    }
    ft_recurs(*x,*(y - 1),a - 1, b);
  }
  else
  {
    return *x;
  }
}

void  ft_sort_int_tab(int *tab, int size)
{
  int memo;
  int i;

  while(--size >= 0)
  {
    i = size;
    tab[size] = ft_recurs(tab[i], tab[size], i, memo);
  }
}
inertia
  • 13
  • 4
  • C or C++? Pick one. – Shawn Jul 17 '22 at 18:59
  • 1
    You'll be glad to hear you don't need anyone's help to figure this out, just a tool you already have: your debugger! This is exactly what a debugger is for. It [runs your program, one line at a time, and shows you what's happening](https://stackoverflow.com/questions/25385173/), this is something that's every computer programmer must know how to do. With your debugger's help you'll able to quickly find all problems in this and all future programs you write, without having to ask anyone for help. Have you tried using your debugger, already? If not, why not? What did your debugger show you? – Sam Varshavchik Jul 17 '22 at 18:59
  • `ft_recurs(*x,*(y - 1),a - 1, b);` --> `return ft_recurs(*x,*(y - 1),a - 1, b);`, but notice that you try to dereference an `int` in `*x` and `*(y - 1)`, thats wrong. – David Ranieri Jul 17 '22 at 19:02
  • Assuming `size` is the number of elements in the array `tab`, then `tab[size]` will be out of bounds. – Some programmer dude Jul 17 '22 at 19:02
  • How does this compile when `void ft_recure(...)` does `return a[k];` ?? Even if you corrected that to be `int ft_recure(...)` none of the other control paths return a value. – Weather Vane Jul 17 '22 at 19:04
  • 1
    Thank you. Looks like its out of my knowledge. I'll try more a bit. and if can't I'll learn more and come back for fix this problem. It's my first question. And I really glad that at least I try to reach out for help and have people ready to point me what is right what is wrong. I really appreciated for your support! – inertia Jul 17 '22 at 19:13

1 Answers1

0

In the first approach, you did improper when calling API again:

    void    ft_recure(int *a, int *b, int j, int k)
    {
        if (--j >= 0)
        {
            if (a[k] < b[j])
            {
                a[k] = b[j];
            }
            else
            {
                ft_recure(a[k], b[j], j, k);
            }
        }
        else
            return a[k];
    }

a and b input to API ft_recure is a pointer but in ft_recure(a[k], b[j], j, k); it is value. You should correct this as: ft_recure(&a[k], &b[j], j, k); if you expect to input the address of k and j elements.

In your alternative usage:

    int  ft_recurs(int x, int y, int a, int b)
    {
        int j;
        
        j = a;
      if( a > 0)
      {
        
        if(*x < *(y - 1);)
        {
            b = *(y - 1);
          *x = b;
        }
        ft_recurs(*x,*(y - 1),a - 1, b);
      }
      else
      {
        return *x;
      }
    }

The input is value but in the function you are using *x andn *(y-1) is not really a proper way. May be you could try int ft_recurs(int x[], int y[], int a, int b). But if so, you also need to provide pointer address at ft_recurs(*x,*(y - 1),a - 1, b); and then the issue come back to similar to first approach.

ThongDT
  • 162
  • 7