I realised that when I included the "else return false" in my bool search, it will never be able to "find the needle". Conversely, if I were to remove that part, the program works fine. It is able to find 2008 and not find 2013. Any idea why is this so?
/**
* helpers.c
*
* Computer Science 50
* Problem Set 3
*
* Helper functions for Problem Set 3.
*/
#include <cs50.h>
#include "helpers.h"
/**
* Returns true if value is in array of n values, else false.
*/
//value = needle , values[] = haystack, n = size
bool search(int value, int values[], int n)
{
// TODO: implement a searching algorithm
if(n<0)
return false;
for(int i=0;i<n;i++)
{
if(value == values[i])
{
return true;
}
else
{
return false;
}
}
return false;
}
/**
* Sorts array of n values.
*/
void sort(int values[], int n)
{
// TODO: implement an O(n^2) sorting algorithm
return;
}