0

Ok this is the code for sorting I have written but now I don't know what file to compile to check for my results. Because of the addition of printf() function I have added stdio.h into my helpers.c but still I am not getting my printf()s. Tried compiling with ./generate 10 50 | ./find 12 still no result. I can't get my sorting list printed out. Am i missing some key thing here?

void sort(int values[], int n)
{
    // TODO: implement an O(n^2) sorting algorithm
    int i, j, k;
    for ( j = 0; j < n - 1; j++)
    {

        for (i = 0; i < n - j; i++)
        {
            if (values[i] > values[i +1])
            {
            int temp = values[i] ;
            values[i] = values[i+1] ;
            values[i+1] = temp ;

            }
        }
    }    
    return;
    for ( k = 0; k < n; k++)
    {
        printf("no %i : %i\n", k , values[k]);
    }

}

Or am I supposed to make another file paste this code in there and get an array of lets say 10 ints through GetInt() and than see if I am getting the desired sorted list or not?

1

Your code doesn't print anything because you have a return statement before the for loop that would print the sorted array. Either delete return completely or even better move it below the last for loop.

This will solve the printing problem (which you don't need to have in your sort function, it's just for debugging I hope), but your code still doesn't work correctly. To use your program along with generete, you have to do three things:

  1. make generate
  2. make find
  3. ./generate n [s] | ./find needle

Where n is the number of elements in the array to be generated and s is an optional seed. I would suggest you start by using a standard seed so that each time you generate the same numbers. So while debugging use

./generate 5 1 | ./find 2728

That should get you going.


If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark. And don't forget to keep coding!

You must log in to answer this question.

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