Hot answers tagged

20

This is an example of a recursive call. Even though it is calling itself correctly, it is missing the code necessary to recursively return the result. As it is written above, it will execute the recursive call to itself, but when it does find a number, it will return true on the first step back through the recursion. However, at the second step and ...


4

There was a question about this question on facebook, so instead of replying there, I thought I'd try to give some examples here, to supplement Cliff's amazing answer. It might be that none of this makes sense, but it's how I wrap my mind around it. Feel free to correct as necessary. Let's work off the simplest case, an array of {1, 2, 3}. If we are ...


2

What bool does 0 represnet - true or false? Your search() routine returns a bool, meaning either true or false. However, you have fallen into the trap of using numbers instead of true or false. The compiler treats 0 as false and any non-0 as true, so you are returning false where you want to return true and vice versa. This is understandable, because ...


1

There are several problems. The most significant is this: for (char* i = buffer; *i != '\n'; i++) { // creating a variable to store letters in int templetter = i[j]; This code is supposed to get the next letter and store it in templetter. However, it is skipping letters. Consider this: the for loop increments the i pointer. That means that it will ...


1

Well your program is correct, in the sense that it doesn't have any compiling errors, but it doesn't do anything. It just checks whether the user gave 2 arguments or not. You are correctly returning 1 in case he provides any other number of arguments except from 2 (you can use echo $? to see this) but that is not all the program should do. Also you should ...


1

I would suggest that you visit the CS50 Study page for Functions to get a good basic understanding of how functions work, what it means to return etc. There are several slides, examples, and videos to help you. Essentially, in your example, you are create a new GRect called 'rect' and then returning that object ('rect') to the calling function (which in ...


1

Hard to see what exactly is going on without getting a look at your wider code but the logic in this loop can't be right. Your "for" loop will only ever run once because it will return either true or false on its first run. I would suggest that you remove the "else{return false;}" and change "return 0;" to "return false;" at the end - that way false will ...


1

you are right, in fact the printf function return value is as follows: Return Value On success, the total number of characters written is returned. If a writing error occurs, the error indicator (ferror) is set and a negative number is returned. If a multibyte character encoding error occurs while writing wide characters, errno is set to ...


1

This is a really interesting question. Is it possible? Yes. Is it desirable? In my humble opinion, it's something to be avoided. Yes, it's possible. There is a function for this: exit(<number>) where number is your choice of an integer. It will immediately terminate the program and will release and clean up as much as it can, like file pointers, ...


1

First, note that you change n only if something is wrong. Let's suppose that board [0][0] = 1 as it should be. Then, this condition else if (board[i][j] != n) { n = n+ (d*i)+j; return false; } doesn't work so you still have n == 1, and continue to check board[0][1]. Suppose it is 2 as it should be. You check else ...


1

Having the errors multiply shouldn't necessarily be looked at as the code getting worse. Rather, the new errors should be analyzed for what they are. Sometimes an error masks other errors, other times, an error causes other errors. The reality here is that you have both conditions happening. First, the return issue is being detected by the compiler and ...


1

"Will the loop break?" Technically speaking, no, it won't break. By using a return true statement, the entire function will immediately terminate and return a value of true to the calling code (or if in main, it will immediately terminate the program). My guess is that this is exactly what you want to do. Understand that a break performs a different ...


1

Yes, It will. By return keyword your program will be out of current function's scope. So it will be out of that loop.


1

When you call swap() with int a and int b you are not actually "passing" swap() the variables. Instead you are handing swap() a copy of them, more specifically a copy of the value the variables contain. So you are indeed swapping correctly, you are just not handing over the final results back over the the caller. Below is a correct swap function. void swap(...


1

This is a case where the code is doing exactly what is written, but maybe not what you want. Can you specify what it is supposed to be doing? I don't remember where this comes from in the course, (can you please state the source?) but the code appears to be doing what it is designed to do. If you test with any three numbers the following happens: If any ...


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