CS50 helpers.c, Code not finding value (needle)? Any ideas? -
not sure why cant find value in array. whenever run code returns "didn't find needle in haystack."
/** * helpers.c * * computer science 50 * problem set 3 * * helper functions problem set 3. */ #include <cs50.h> #include "helpers.h" /** * returns true if value in array of n values, else false. */ bool search(int value, int values[], int n) { //makes sure input value positive while (n > 0) { //searches until value found (int = 0; < n; i++) { if (value == values[i]) { printf("found it!\n"); return true; } } } return false; }
haven't started sort
/** * sorts array of n values. */ /*void sort(int values[], int n) { // todo: implement o(n^2) sorting algorithm return; }*/
i have changed , used if statement way was, still gives same.
#include <cs50.h> #include "helpers.h" /** * returns true if value in array of n values, else false. */ bool search(int value, int values[], int n) { /* if (n >= 0) { return false; }*/ (int = 0; < n; i++) { if (value == values[i]) { printf("found it!\n"); return true; } } return false; }
problem solved!it seems accidentally made second file same name outside folder. changing wrong file!!!
the problem part:
while (n > 0)
which puts in endless loop. should replace if statement:
if(n<=0){ return false; }
Comments
Post a Comment