# find not working

so ive cleaned up my code but it still always prints 'found needle in haystack'! please help!

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

#include <cs50.h>
#include <stdio.h>
#include <string.h>

#include "helpers.h"

/**
* Returns true if value is in array of n values, else false.
*/

bool search(int value, int values[], int n)
{
//ensures that values are entered
if (n < 0)
{
printf("n must be a non-negative integer\n");
return false;
}
//iterates through 'haystack'

else
{
for (int i = 0; i < n; i++)

//finds midpoint position of array sized 'n'
{
int middlevalue = values[(n+1)/2];

//if the midpoint value is the 'needle' returns true
if (middlevalue == value)
{
return true;
}

}
}

//searches right side of array for 'needle'
int middlevalue = values[(n+1)/2];
if (value > middlevalue)
{

for (int i = 0; (middlevalue < i < n); i++)
{
if (i == value)

{
return true;
}
}
}
//searches left side of array
else if (value < middlevalue)
{
for (int i = 0; (0 < i < middlevalue); i++)
{
if (i == value)

{
return true;
}
}
}

return false;

}

/**
* Sorts array of n values.
*/
void sort(int values[], int n)

{

//for array of size 'n' pass through the array n-1 times
for (int h = 0; h < n; h++)
{

//for each element in the array (with each 'h' iteration, does not check last 'i' elements)
for (int i = 0; i < (n-h); i++)
{
//if the element is bigger than the subsequent one
if (values[i] > values[i+1])
{
//i+1 swaps with i

int j = (i+1); //i+1 stored as j
i = i+1;       //i stored as i+1
i = j;        //j stored as i

}
}

}

}
``````

## 1 Answer

To review, the bubble sort TODOs (according to the less-comfortable walkthrough) are as follows:

• iterate over list
• compare adjacent elements

• swap elements in the wrong order

• largest elements will 'bubble' to the end

• the list is sorted once no elements have been swapped

The bubble sort pseudocode according to the Bubble Sort Short is:

``````Set swap counter to a non-zero value
Repeat until the swap counter is 0:
Reset swap counter to 0
Look at each adjacent pair
if the two adjacent elements are not in order, swap them and add one to the swap counter
``````

You'll have to adjust your code to make sure that you follow the TODOs. Walk yourself through the TODOs and psuedocode and compare that to your own code, checking if you need to change anything. If you do this, you should be able to make the proper changes to your code.

I hope this helps! If you have any more issues, please comment below.

• so ive done this but I get the following when I try to compile error: expected expression values[i] = int h; Jul 5 '17 at 14:34
• Could you include your new code? Thank you. Jul 5 '17 at 14:35
• I updated the code :) Jul 5 '17 at 14:38
• I want h to represent the entire array so it can be reiterated over until sorted, and i want i to represent each element within that array, but I'm not sure about how to write this in the code. :) Jul 5 '17 at 14:50
• if i remove lines 84-86, then the code compiles but i keep getting segmentation faults after i input my 'hay' (50 and 43) and ctrl+D Jul 5 '17 at 15:02