Hot answers tagged

7

The warning (converted to an error due to the -Werror option to clang on the appliance) control may reach end of non-void function Says that you've a function in your program that's declared to return a value, but it's not guaranteed that it will. For example, foo(), in the following piece of code, is declared to return an int int foo(void) { if (...


6

helpers.c has no main function. You need a main function in every C program, it's the program's starting point, so to speak. Anyway, the problem with your command line argument is you're missing the file containing the main function - namely find.c - in your list of source files. Andrej


5

Have you written the sort and search functions in helpers.c? That is one of the assignments in the problem set. find won't work until you've actually written the code. As the pset says: And now the fun begins! Notice that find.c calls search, a function declared in helpers.h. Unfortunately, we forgot to implement that function fully in helpers.c! (To ...


5

INT_MAX is defined in limits.h, which is a standard C header. INT_MAX: Maximum value for an object of type int (32767 or greater) You may wonder why find.c needs that? int straw = GetInt(); if (straw = INT_MAX) If you look at cs50.c (which defines GetInt()), you'll see that GetInt() returns INT_MAX when it doesn't receive an integer: // get ...


4

find.c and helpers.h cannot be altered. When check50 runs, it uses its own copies, so your alterations are lost. That will cause the compile to fail. You need to make all of your changes in helpers.c If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)


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

Looking at the distribution code for find.c, we can see this line near the top of the file: #include "helpers.h" That header file, helpers.h, contains only the prototypes for your search and sort functions; the actual implementations are in helpers.c.1 So how do we get from find.c to helpers.c without the line #include helpers.c somewhere in the mix? The ...


3

So, I took a quick look at the code..... for (int i = 0; i < n; i++) { if (value == values[n]) Which element in the values array is being checked on each loop???? What's incrementing? [EDIT: additional response.] During the first pass through the loop, it checks to see if it is a match, it returns true. BUT, if it isn't a match, it ...


2

To deepen what Andrej and Cygni_61 both correctly said, well, you need to 'make find' instead of 'make helpers', which I assume is what you were doing from the clang command. In the Makefile, you can see that running make find will compile find.c, helpers.c, and helpers.h.


2

To deepen what Andrej correctly said, consider helpers.c as an auxiliary file where helper functions reside. When you write complex code, you can distribute your fnctions into different files: this way, each file will contain only those files inherent to a defined scope or matter. As an example, imagine a project that handles picture files. You will have: ...


2

If you're sure that everything is correct, you may drop Rob Bowden a note at rob@cs.harvard.edu! Please mark this as best answer to take this question out of the unanswered list. Thank you Kareem.


2

We are not given the source code for search() in helper.c. We are asked to implement this search() function using linear search first then binary search. And since you haven't implemented any of these yet, it will return false (i.e., you will not be able to detect the needle even if it is present in haystack till you code it yourself).


2

You are comparing min and max. You should be comparing values[min] and values[max] Otherwise, you end up with, for example: {41,42,43} so n = 3 min is 0, max is 2 mid is 1. if 1 == 42 else if 1 < 42 min = 1 etc. when you should be doing if values[1] == 42 (and values[1] is 42) Also, as Paul said in his answer, remove that ...


2

To debug "find" with gdb, enter the following command: gdb find You need to enter the filename of the executable as a parameter following gdb on the command line. Don't use ./ or .c when you enter the command on the command line. After that, the (gdb) prompt will appear. To run your program, simply enter r followed by whatever parameters you want to send ...


2

You must tell the compiler to link the cs50 library by using the -lcs50 flag. The full command should be: clang -ggdb3 -O0 -std=c99 -Wall -Werror sort.c helpers.c -lcs50 -o sort If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the ...


2

The reason why 100 is ignored is because you're comparing values[min] with values[j] only. And since j is initially set to i + 1 which is 1, values[0] is totally ignored. The role of the nested loop should be to iterate through the rest of the elements in the array, picking up the minimum value, swapping it with values[i] in case this minimum values is less ...


2

In testing, the code works fine. Is it possible that you have a problem with your sort function? Have you verified that sort is correct by printing out the sorted list with different test data? As a side note, I am curious why you decided to write binary_search() the way you did. Specifically, search returns a bool, while binary_search returns an int. Your ...


2

There are two issues to address here. The expected return value of this function is a bool, but you are returning an int. Now, it is the case that you can use 1 and 0 as true and false, respectively. However, 0 means false and 1 means true. As a result, the return values are actually opposite what you probably intend them to be. Consider the structure of ...


2

You have problems with the array that sets your counting array to 0: int array_holder[max_value]; for(int c = 0;c < max_value;c++) { array_holder[c]=0; } Think about implementing a counting sort, you will need to provide enough buckets to store all the numbers from 0 up to a maximum value. A small modification to your code will fix this fault. Also,...


2

C, by default, passes parameters to functions by value. That means when the function swap is called, two new variables, x and y are created, and the values of the integers are copied into them. When the function returns, the two variables x and y cease to exist. The original data in my calling function is unchanged. This is covered in Week 2 shorts and the ...


2

I used the debugger on it, and it looked perfectly fine. temp[0] to temp[65535] were initially 0. Have you tried using debug50 on it, like ./generate 1000 50|debug50 ./find 127? Maybe the segmentation fault happens somewhere else? Not sure whether it's a good idea, but I'd rewrite if (temp[i] != 0) { values[j] = i; ...


2

My sincere recommendation is ... start again. You have two nested loops, each time the DO-WHILE loop begins, the min and max values are set to zero, as the end-of-cycle condition for while is values[min] > values[max] can occur at any time or never happen, so we can have an infinite loop, also you are changing the values of the array itself so that your ...


1

find.c has been very stable, so I'm confident that the problem isn't there. It sounds more like you are experiencing infinite loops and not program crashes. I'm also highly suspicious that the problem lies in your sort function, since it is the very next call in find.c, and based on your description of the behavior. I would look for the possibility of ...


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

Your code doesn't print anything because you have a return statement before the for loop that would print the sorted array. Either delete return completely or even better move it below the last for loop. This will solve the printing problem (which you don't need to have in your sort function, it's just for debugging I hope), but your code still doesn't work ...


1

Look at the implementation of GetInt() from http://cdn.cs50.net/2011/fall/lectures/5/src5/cs50.c. /* * Reads a line of text from standard input and returns it as an * int in the range of [-2^31 + 1, 2^31 - 2], if possible; if text * does not represent such an int, user is prompted to retry. Leading * and trailing whitespace is ignored. For simplicity, ...


1

You while-loop never finishes because n never changes. You need to either rethink your logic so the loop terminates when the value was not found, returning false, or detect when there's no values left to search inside the loop, and then return false. If you still want to go with the recursion-route, you need to change n when you call the function again. I ...


1

Classic array overshoot problem. Look at your sort code inner for loop. for(i=0; i<n; i++) if (values[i] > values[i+1])... The values[] array has n elements, numbered from 0 to n-1. Your if statement tries to test values[i+1], but i+1 = n. You're trying to test values[n], which is beyond the end of the array. Instant error. I bet you're ...


1

From what I can see, you have at least one mismatched curly brace that isn't paired up with another. Make sure that all of your curly braces are properly paired. If you highlight a curly brace or a parenthesis in gedit, it will tell you where the matching one is, or if it isn't paired. Also, I'm wondering about the undefined reference to main. What ...


1

I answered on your reddit post but I'll post it here too: Run generate and send it to a text file, then run find under gdb using the text file as input ./generate 581 50 > 581nums.txt gdb ./find (gdb) run 127 < 581nums.txt If that works without crashing, then you know it's not your code but something on your system that is causing the crash.


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