0

In my code, I get a segmentation fault some, but not all of the times that I run it. Sometimes, it sorts the code and finds the needle, sometimes it doesn't sort correctly and ends with a segmentation fault or can't find the needle. Please help me fix this issue. My code is`/**

* helpers.c
*
* Helper functions for Problem Set 3.
*/

 #include <cs50.h>
 #include <stdio.h>
 #include "helpers.h"

 /**
 * Returns true if value is in array of n values, else false.
 */
bool search(int value, int values[], int n)
{
if (value < 0) {
    return false;
}
int low = values[0];
int high = values[n - 1];
int mid = (low + high) / 2;
if (mid == value) {
    return true;
}

else if (mid < value) {
    high = mid - 1;
}

else {
    low = mid - 1;
}
search(value, values, n);
//printf("Low %i High %i Mid %i", low, high, mid);

 //TODO: implement a searching algorithm
return false;
 }

/**
* Sorts array of n values.
 */
void sort(int values[], int n)
{
int swapcount = 1;   
//int iterations = 0;
//int swapcount = 1;
    /*if (n == 1) {
        return;
    }*/



        for (int i = 0; i < n-1 ; i++) {
            if (values[i] > values[i + 1]) {
                printf("before swap %i, %i\n", values[i], values[i + 1]);
                int temp = values[i];
                values[i] = values[i + 1];
                values[i + 1] = temp;
                printf("after swap %i, %i,\n", values[i], values[i + 1]);
                swapcount++;
            }

            /*else {
                printf("Nothing to swap\n");
            }*/

            /*for (int j = 0; j < n; j++) {
                    printf("%i\n", values[j]);
            }*/
        }
        //return 0;
    if (swapcount == 1) {
        return;
        for (int j = 0; j < n; j++) {
            printf("Sorted array is %i", values[j]);
        }

    }

        //iterations ++;
        /*printf("%i Iterations\n", iterations);*/
        /*printf("Sorted is\n");
        for (int j = 0; j < n; j++) {
            printf("%i\n", values[j]);
        }*/
        //printf("Your swapcount is %i\n", swapcount);
        printf("%i swaps\n", swapcount);
        for (int w = 0; w < n; w++) {
            printf("array is %i\n", values[w]);
        }
        sort(values, n - 1);


// TODO: implement a sorting algorithm
//return;
}

`

2 Answers 2

0

As far as I can tell your sort function works as intended, the problem lies in your recursive search function. I would suggest debugging this function using debug50 or gdb if you prefer. Specifically, pay attention to how your variables high, mid, and low change as you call your search function recursively. If you are confused as to why the issue is occurring I recommend watching (or re watching) Doug's short on variables and scope:

https://www.youtube.com/watch?v=pBqyy8NwzqY

For debugging in debug50 try this video for a breakdown:

https://youtu.be/-G_klBQLgdc

For gdb try either or both of these:

https://www.youtube.com/watch?v=USPvePv1uzE

https://www.youtube.com/watch?v=y5JmQItfFck

Personally I found the first video to be most helpful although it is older.

2
  • I tried to use the debugger, but I don't really understand how it works. Could someone either explain it or leave a link to something that explains it? Thanks
    – Joel Banks
    Jul 9, 2017 at 20:53
  • I edited my answers to include links to debugging tutorials. Debug50 is easier to start with, but gdb is probably the best long term option. Jul 9, 2017 at 21:02
0

The only problem I can see is in the search algorithm. Check that the values of your expressions match their declared types.

You must log in to answer this question.

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