Hot answers tagged

5

In this pset, the file find.c has the statement #include "helpers.h" which will cause helpers.c to be incorporated into the final executable program find. The file find.c has main, so no, you don't need main in helpers.c. Your code has a number of syntax errors, something that that programmers will constantly get, but it will be less and less with ...


4

According to the spec [emphasis added]: Complete the implementation of /search in such a way that it outputs a JSON array of objects, each of which represents a row from places that somehow matches the value of q, as in the staff solution below. If you return jsonify([q]) it is returning a JSON array of a JSON array of objects. You shouldn't use that. It'...


3

In the search function, try replacing if (values[middle] == value) by if (values[middle] == value || values[middle+1] == value). Because middle is an integer and middle = (lower + upper) / 2; works like a floor function so middle may never achieve the value of last index of the array. Not only last index but there would be some other indices too inside the ...


3

I don't want to sound cruel here, but your sort routine has serious problems. It is incomplete, it has code that appears to serve no purpose ( an if statement with no code to process if true), and doesn't compare any two elements in the list to sort (or at least not the two intended elements). Your biggest problem (in my opinion) is that you are comparing, ...


2

I'm not so sure you should create distinctive indexes for each column. What I did was to create a FULLTEXT index for 3 columns together, and then search using that. If you want to create different FULLTEXT indexes for each column (it could work I'm not sure) you could do it like that. Create your FULLTEXT indexes by going here, selecting all the columns you ...


2

In your for loop you have for (n = 0; n < values[n]; n++) The values[n] has the value of the nth element in the values array. So on the first iteration you have n == 0 and you check, is 0 < values[0]? then on the second iteration you check, is 1 < values[1]? When what you want to check is if n is smaller than the length of the array, not a value ...


2

The problem with that is that true is 1, not 0, so return true is the same as return 1. If the return type really is bool, then you're returning the exact opposite of what you want to return. You may be confused by this and the standard retrun values used by main and other standard functions. In many cases, they return an int, but use 0 to indicate success ...


2

It's because you pass the address of midpoint as the third parameter of comparer(), instead of values[]. Try int result = comparer(midpoint, value, values); instead of int result = comparer(midpoint, value, &midpoint); Edit There are also some problems with your sort(). It should be if (values[i] > values[i + 1]) { temp = values[i]; ...


2

1: The more columns you are searching the more results you return. For example as you start to type the word "Boston" the search query takes 'B' and looks for every word in the column with a 'B' in it. It will return a lot of results. Now the more columns you search the more result you will get. 2: This question is linked to the first as in the more letters ...


1

Your sort is flawed, causing it to eject the largest value and add 0 to the list. Look at the printout of the list to confirm. In the first pass through the outer for loop, k=0. This allows i to become n-1. But that interacts with your swap sequence. You swap values[n-1], the last element in the array, with values[n], an undefined element. This introduces ...


1

This code will never find anything unless value == midpoint. On each pass through the loop, it updates the min or the max, but it never updates midpoint. Since midpoint never changes, the loop will either find the value on the first pass at the midpoint, or the loop will just run to completion without ever finding value. Side note: it would be more ...


1

One of the lessons of this pset is how to build an executable from multiple source files using a makefile. The link error is happening because you are trying to make helpers. Because there are no instructions in the makefile on how to compile and link helpers, it tries to use default rules. The default rule says to create an executable based on the name of ...


1

Compilers are dumb. They won't do exhaustive logic analysis of a function, but they will check to see if they think there's a path through the code that doesn't end with a return statement, assuming the function has a return type. That's almost certainly what's happened in your case. Here's an indepth explanation, but perhaps overly thorough. Maybe it will ...


1

I ran your code and commented out the break statement and it ran perfectly. Why are you getting an infinite loop? Did you change something that you weren't supposed to elsewhere? Is there more to this than the posted code? The break statement is preventing the while loop from running more than once, so unless the needle is found on the first pass, it will ...


1

The sm() function recursively calls itself, but fails to recursively return the result. For a detailed discussion, check out Pset3 Binary Search problems The break in this while loop forces the loop to terminate after only one pass, when the break is encountered. It will never make a second pass. It appears to be necessary because l and h never change ...


1

I've just realised I had to put $places instead of $alpha_match. I should've paid more attention to the code below of how the JSON is created. facepalm


1

You are missing something, but it is not obvious. There is a mysql setting called ft_min_word_len (the minimum length of words to be indexed), which defaults to 4. MySQL doc is here. See this post for details on how to change it. (Of course you would want the minimum to be 2). Also from MySQL doc: Note FULLTEXT indexes on MyISAM tables must be rebuilt ...


1

I got it, the correct syntax would be: SELECT * FROM `places` WHERE MATCH (`place_name`,`admin_name1`,`admin_name2`,`postal_code`) AGAINST (`+Cambridge +Massachusetts` IN BOOLEAN MODE);


1

New answer, based on new information. There's something wrong with your parentheses. That "else" has no "if" preceding it. It isn't with the outer if because it is inside that code block. It isn't with the inner if because the curly brace above the else closes the for block. Hopefully color coded braces will help.


1

Hint only: if (n < 1) is the problem. Review the arguments to search.


1

I am sorry this pset has been such a struggle for you. I think you are really close to the finish line. Take a deep breath and let's go through this step by step. The most important thing is that you do not lose the code you have worked so hard on. The plan: Backup your work. There is no such thing as too much backup. Restore Makefile, find.c and helpers....


1

in case we have a single element left in the array, max and min would be equal. the midpoint is calculated, and we check whether array[midpoint] is equal to the value we're looking for. if the previous condition is not true, namely array[midpoint] is NOT equal to the value we're looking for, the algorithm still proceeds further and either sets max to a ...


1

Say your array is 3,2,1 On the first pass of your loop (i = 0) your array will become: 2,3,1 Then i becomes 1 and on the second pass it will become: 2,1,3 This is good, as now the 3 is in the right spot. But now i is 2 and your loop will compare 3 with the null terminator and swap those. Then your loop will end with your array essentially being 2,1,\0,...


1

First of all, the code you have written, is supposed to implement a linear search O(n), which means it traverses the whole values array and if it finds the value in there, it returns true, otherwise it returns false if the whole array has been traversed and value wasn't found. Your program doesn't work because of the else return false; clause. You ...


1

Asked and answered in comments. Except for a minor flaw, the search code appears correct. It should be finding 127. More likely, there's a problem with the code that loads the numbers in the array. Try working with a smaller data set, maybe 10 elements, and print out the array to see if it is loading correctly. Also, you should know that you will never ...


1

You don't need: else { return false; } If we have return true the loop will end, so just put a return false at the very end of the function.


1

Sorting is one of the technique that is used in solving MANY problems(by MANY, I mean literally many), and that is why, one of the most prestigious book of computer science, The Art of Computer Programming devotes more than half of one of its book for sorting. The power of sorting can be recognized by the fact that lookup in an unsorted list takes O(n) ...


1

OK, so this was the code that didn't work in my recursive binarysearch(): // search bottom half of list if (value < values[mid]) { max = mid - 1; binarysearch(value, values, min, max); } And then, I changed it to return binarysearch() and now I'm good to go.


1

Turns out that I just needed to re-compile find.c. So, I guess if your code should work in theory and syntax is correct, recompile what your working on and see if that works.


1

Consider what would happen the first time the loop executes. For example, if the first item in the list is not the number you are looking for, then when the code reaches the 'if' condition, the condition will evaluate to false, and the function will return right there and then without comparing the remaining values. The code should only return true if a ...


Only top voted, non community-wiki answers of a minimum length are eligible