Hot answers tagged

7

Where do I implement that? In helpers.c? helpers.h? A library's .h file is a header file. It does NOT contain definitions. Instead, it contains code that declares the library's resources that are available for you to use. These resources include functions, variables, structs, type definitions, etc (these, together, are commonly referred to as the interface)....


3

One of the skills that are indirectly taught in this class, as well as being one of the most important skills you must learn as a programmer is how to debug a program. You will run into seg faults frequently as a programmer, so you really need to learn how to isolate them yourself! There are a number of techniques you can use. You could step through 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, ...


3

The problem is that you are swapping values[i] with values[i+1] while also allowing i to go to a maximum of n-1. Let's say that n=10, so the array has 10 elements, 0 through 9. When the code sorts through the array, at the end of the first pass, i=9. The code compares values[i] to values[i+1], or values[9] to values[10], which isn't defined. Instead of ...


3

In Big O notation and Omega notation the constants are not computed. Say you have an array with 1000 places. It's not that different to say 1000 than 999. So the n-1 becomes n. Also let's say that you have to traverse an array, that is to read all of its elements. If the array is 1000 places, ti has an Ω(1000), but we still say that it is constant time as Ω(...


3

Well, the number of iterations you did was basically n + (n - 1) + (n - 2) + ... + 1 Mathematically, this is equal to n (n + 1) / 2 which is equal to n^2 + n / 2 and since we don't care about small numbers (n / 2) in this case, we can ignore it (if n = 1000, n^2 = 1,000,000 while n/2 = 500 which is not a big deal). So we can say it's a O(n^2) algorithm.


2

"This is a bug in clang, which has been fixed in version 3.5. The CS50 team is working on getting an update out with update50." -see link Variable optimized out eventhough it will be used later


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

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

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

Your sort is essentially correct. It does restart at the beginning of the list each time. The only problem is that it is going beyond the end of the array. The array has n elements, numbered from 0 to n-1. Look at this code: for(int i = 0; i < n; i++) { swapcounter = 0; if (values[i] > values[i + 1]) When n=3 and i=...


2

preferences[pairs[i].winner][pairs[i].loser] > preferences[pairs[i+1].winner][pairs[i+1].loser] This condition swaps the elements in ascending order while check50 checks for the pairs array for descending order.


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

The array[start] < array[end] is the wrong condition. You want to keep looping as long as the search interval has elements, so use start <= end instead (<= as you chose both start and end to be included in the interval). Then you can remove those if (array[start] == target) and if (array[end] == target) which seem to compensate for leaving ...


1

Both search and sort have problems. First, sort isn't complete. It only makes one pass through the list, so it will only bubble sort the largest value to the last position. I suggest a review of the lectures and class material on bubble sorts. Second (and without doing an in-depth analysis), it looks like the search routine goes into an infinite loop when ...


1

Your do..while loop really should be a regular while loop, but it would blow up only if your array to search was empty (then you're accessing values[-1] within the loop). Your condition is wrong. Unless there is exactly one item in the array, it might be an infinite loop. The correct condition is to enter the loop when there are elements left in the search ...


1

The code creates the counter array: int counter[65535]; but doesn't initialize it. What is in the array? Try printing out the contents of the array (or maybe the first 20 elements) to see. As a side note, while this will work for a list of integers with values of up to 65535, it is problematic. The code should be able to handle sorting a ...


1

You are comparing to element values[(n+1)/2], but your recursive calls look like you meant values[n/2] instead, which makes a lot more sense, as n/2 is always the middle element's index, or the higher of the two middle elements' indices in case of even n. Imagine n being 1, an array of exactly one element. You'd access values[(1+1)/2], or values[1], which is ...


1

apparently, you're trying to implement selection sort. the problem appears to be with this condition if (values[j] < values [i]) { min = j; } are you sure you wanna update min every time values[j] < values[i]? for example, consider the array {5, 3, 4}. let's agree that after the first iteration of the outer loop, this array should look like {3, 5,...


1

The syntax for loop consists of ; and not ,. for (int i = 0 ; i < n ; i++) { // your stuff here }


1

Your problem is in your sorting. Check the Wikipedia article about bubble sort implementation: https://en.wikipedia.org/wiki/Bubble_sort#Implementation. In the most non-optimized case, the pseudo-code looks like this procedure bubbleSort( A : list of sortable items ) n = length(A) repeat swapped = false for i = 1 to n-1 inclusive do /...


1

The URL should be updated in the spec shortly. Here it is in the meantime: https://www.youtube.com/watch?v=TYnkJckP1Ss


1

In search, move the calculation of mid into the loop, and make sure the /2 is outside the parentheses. The sorting algorithm has no potential for an infinite loop, and looks correct. Also, it's usually best to have as few global variables (declared outside of any function) as possible.


1

There are problems here. First, it only makes one pass through the array, so all it will do is push the largest value to the end. The rest of the list will likely not sort. Next, since i can increase to n-1, there's a problem. If i = n-1, then i+1 = n, which is beyond the end of the array. This is an error, but it usually won't report as an error. Instead, ...


1

Looks like some kind of select sort. For that to work, in the outer loop, before the inner loop, set min to i ("smallest non-sorted element so far is at index i"), and in the inner loop (starting at i+1 as it should), compare only elements at j and min, not i. As min is initialized to i each iteration of the outer loop, you don't have to handle it in any ...


1

I figured it out. It seems like I didn't manage to swap values[i] and values[min] in selection sort the right way, so I did a bit of researching and saw that I need a third integer to do that. Here is the fixed code: /** * helpers.c * * Computer Science 50 * Problem Set 3 * * Helper functions for Problem Set 3. */ #include <cs50.h> #include "...


1

If the list is not already sorted, you have an infinite loop. the counter variable will be incremented at some point, meaning that the while condition is true. Since counter is only incremented and never reset to 0, the do/while loop will execute forever. Perhaps you wanted to set counter to 0 as the first line inside the do/while and before the for loop? ...


1

The problem here is that the code only makes one pass through the list. It will bubble the largest element to the end of the list, but then stops. It needs to come back to the beginning and repeat the process to sort the next largest item up, and again for the 3rd largest, 4th, 5th, etc. If this answers your question, please click on the check mark to ...


1

The above code is partially correct. Although the logic seems to be appropriate, it is not efficient. All you have to do is set the value of i as minimum every time you enter the 'i' loop, and then start with j loop starting with j=i+1 till the end. Every time you find an element smaller than the element at index 'min', update the 'min' variable to an index ...


1

Consider what happens, when i = n - 1.


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