Hot answers tagged

3

Even or odd, it doesn't really matter. The point is that you are dividing the list approximately in half, give or take. If the split doesn't occur exactly at the middle (odd number of elements), it can be either the element just before or after the midpoint (even number of elements). Eventually, it will come down to either two elements or 1 elements, ...


3

That's because it is case sensitive. Uppercase, "B" ascii value "66", binary "01000010" (this should be the value shown in lecture on stage). Lowercase, "b" ascii value "98", binayr "01100010". The difference between uppercase and lowercase in binary is one bit, hope that helps you.


2

Your calculation doesn't seem to work because you are only looking at 4 bits. A 4-bit signed integer can only have the range of -8 to 7 -(2^3) to 2^3 - 1. The leftmost bit is reserved for the 'sign' (if it's set, it's negative). So your hypothetical computer wouldn't be able to store -11. In a 32-bit signed integer (which is what we have in our CS50 ...


2

The binary numbering system has powers of 2 while the decimal numbering system has powers of 10. For example, the decimal value 548 is actually (8 x 1) + (4 x 10) + (5 x 100) or (8 x 10^0) + (4 x 10^1) + (5 x 10^2) As you might have noticed, the right most digit is in the ones place. The digit at the left of it is at the tens place and the digit at the ...


2

Each character is represented in ASCII. And each ASCII code can be represented in binary code. The space character is also represented in ASCII. So the sentence hello world would be 0110100001100101011011000110110001101111(00100000)0111011101101111011100100110110001100100. Since every character in ASCII is represented by 8-bits you don't have to specify ...


2

You have a naming conflict with the previously defined function bsearch() in stdlib. Try changing the name of your bsearch() function to something like binsearch() and recompile. If this answers your question, please click the check mark to accept this and remove the question from the unanswered pool. Let's keep up on forum maintenance. ;-)


1

You are getting this error message because, apparently the compiler couldn't find the main function in your program. Most probably you are compiling your code like this: make helpers the compiler does not find any main function in you program as a result it yells at you with this extremely long error message. Instead you should compile your code with: ...


1

Binary search means: You have a search interval, with left and right boundary. Is the interval empty? Yes => :( Value not found No => keep doing Calculate the middle index of the interval You evaluate the element at the middle index it's the value => Heureka! it's lower => place left boundary just right of the middle index it's higher => place right ...


1

Your binarysearch function follows this logic: if (something) { if (something_else) { else { /* ... */ } /* **ERR:NOTHING_IS_RETURNED** */ // Here you must add a return false statement } As you can see you're not returning anything in case the first if condition fails.


1

First - think about what a segmentation fault is. It happens when you try to access memory that doesn't belong to you. https://stackoverflow.com/questions/2346806/what-is-a-segmentation-fault Try to plug in some sample numbers into this equation. Here's a hint - try to start with a min of 0 and a max of 10 and see if your mid always stays between 0 and 10....


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

while (n > 0) When would "n" ever not be greater than zero? You'll never leave the loop, never return false. if else is this: if(true) { do this if true } else { do this if false } if else if is this: if(true) { do this if true } else if(true) { do this if second true }


1

To convert a number from decimal notation to binary you have to make a series of integer divisions by two and note the reminder. You stop when the division has as result 0. Let's start with a small number to demonstrate like 14. You do it like that: x = 14 x % 2 == [0] x / 2 == 7 x = 7 x % 2 == [1] x / 2 == 3 x = 3 x % 2 == [1] x / 2 == 1 x = 1 x % 2 == [...


1

Well, as the programmer, you should know what type of input you need to take from the user (e.g., you wanna take a char, a string, an int, etc) then you use a function maybe from the C standard library or from the cs50 library that takes care of that for you given that you're using it correctly. For example, the function GetChar from the cs50 library reads ...


1

In any number system, only certain amount of symbols are used to represent data. This 'certain amount' is known as the base or the radix. For example, in Decimal Number System, we are allowed to use only {0,1,2,3,4,5,6,7,8,9} characters. Similarly , in Binary Number System, only {0,1} are allowed. If you want to represent any data, you can use these ...


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