【发布时间】:2016-07-30 12:51:26
【问题描述】:
我意识到当我在布尔搜索中包含“else return false”时,它永远无法“找到针”。相反,如果我要删除该部分,该程序可以正常工作。它能够找到 2008 年而找不到 2013 年。知道为什么会这样吗?
/**
* 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;
}
【问题讨论】: