0

I am stuck with the implementation of the sort function. I have written a code, but I can't check if the code is correct. This is what I have done thus far:

void sort(int values[], int n)
{

for (int i = 1; i < n; i++)
{
    int element = (int) values[i];
    int j = i;

    while (j > 0 && (int) values[j-1] > element)
    {
        values[j] = values[j-1];
        j = j - 1;            
    }

    values[j] = element;
    printf("%i\n", values[j]);
}
return values[];

}

Here things become strange. When I run find.c the output is correct. However when I use gdb to check the program and I go to the appropriate line and type 'step' I go to the sort function in helpers.c. When I go to the next line however gdb returns to find.c. It seems that find.c doesn't use any of the lines in my sort function. Moreover I put a print command after each iteration to check if the sort function truly sorts the input, but when I run the program these values aren't printed.

I don't understand why my sort function doesn't seem to work. The linear search function does work, so it appears that there is nothing wrong with the link between find.c and helpers.c. Any help with how I can overcome this problem is therefore much appreciated.

Max

5
  • This function, as it is, will not even compile. You can't return anything from a void function. So it makes no sense that you're running it through gdb. Make sure you're compiling the correct .c file (and that you haven't named the one that has this code something else, for example) This code you posted will never compile, let alone run.
    – Irene
    Commented Dec 2, 2015 at 20:12
  • The reason why I use void in the function is because that was stated in the assignment. I quote: " However, take care not to alter our declaration of sort . Its prototype must remain: void sort(int values[], int n); As this return type of void implies, this function must not return a sorted array; it must instead "destructively" sort the actual array that it’s passed by moving around the values therein.
    – Max
    Commented Dec 2, 2015 at 20:47
  • I know what the specs say. What i am saying is that a function of type void CANNOT possibly return anything. If you try to compile a function like: void funct () { int a = 42; return a; } You will get a compiler error: " error: void function 'funct' should not return a value" This will not only not compile at the appliance, it won't compile anywhere. So please, compile and post the correct code.
    – Irene
    Commented Dec 2, 2015 at 23:36
  • Irene is saying that your return statement in line 18 is invalid since you defined your function as having no return in line 1. However, this is not the problem you were asking about. I have submitted an answer below.
    – Andrew G.
    Commented Dec 21, 2016 at 6:35
  • @Max You seem to not understand what @Irene has said. If your function if of type void, you can't return anything. void means nothing is there to return. Also take some time to watch shorts. Doug explains why arrays are passed by reference. not by value. Due to this, any changes you make to an array will be done to original array itself. Another thing return values[] doesn't make sense. values is name of array, why are you using values[] ? Also as @Andrew G pointed out, make sure to re-compile every time, you make changes to your C-program
    – C--
    Commented Apr 17, 2021 at 7:05

1 Answer 1

0

I was having the same problem with gdb skipping over my sort function. I spent hours trying to figure this out but the solution was so simple.

I forgot to execute "make find" again after I had written my new sort function.

Earlier in the problem set we made a search function in helpers.c and then we executed "make find" in the terminal (make find also makes helpers.c and helpers.h). At that point in time there was no body to the function sort. Going further into the problem set, we actually did write in the body of the function sort. However, without executing "make find" again, the gdb thought it was the previous version of helpers.c where there was actually no body to the function sort so the gdb just skips right over our new sort function.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .