We changed our privacy policy. Read more.

Hot answers tagged

3

Your problem lies with the / operator. Because the RGB values and the denominator are all integers, integer division is being performed and the fractional remainder is dropped instead of being passed to the round function. Try replacing 3 with 3.0 (which is explicitly a floating point value). When at least one value is a float, floating point math will be ...


3

Well, your description is sort of right. The image size is incorrect. When I ran the code, the headers are fine. The problem is that the image data that's being generated is too large by roughly the value of scale. In your for loop, why are you using the biHeight value for the output file to control the reading of the input file???? If this answers ...


2

Look at your asub and bsub right before returning an answer. (you can use print to see what they are). Looks like they're getting re-defined for each i rather than accumulating a list of all possible options.


2

You have some substrings that are shorter than the others because there are no n characters left at index i. How many substrings of length n can you have in a string of length len(a)? n is part of the formula that goes into the range call.


2

In your example, the bitmap is scaled by a factor of 2 horizontally, but 3 vertically. Since you write an output line on reading, and additionally n output lines in a loop, you write n+1 output lines per input line, not n. I'd remove the output line on reading, so that there are no two things doing same.


2

First of all, "recursive" means something that calls itself repeatedly. You mean that you can't get the loop to repeat. Think about the condition clause. while (n < 0 && n > 8); When can any number be less than 0 AND greater than 8 at the same time????? Maybe you meant to use OR instead of AND?


2

That works fine and will reprompt for any value greater than 23. Is it possible that you haven't run make since your last change? You have to recompile (using make mario) every time you change & save your source code.


2

If it is for a certain number of iterations, and the counter does not matter, I'd recommend using one of the two forms for (int counter = 0; counter < repeats; counter++) or for (int counter = repeats; counter > 0; counter--) because both clearly show the number of iterations they do. In your case, the inner loop for (int s = n - r; s >...


1

When translating your original code from C to Python, you copied the indentation. That third for loop is not nested inside the second. In C, this doesn't matter because it ignores indentation and will follow the curly braces. In Python, though, the indentation means that the loop is nested, so your code is not as similar as it might appear. In other words, ...


1

Hmmm, it's funny, but most people have no trouble printing the #'s and fight with printing the spaces. You've almost figured out the spaces, but are having trouble printing out the hashes. Save your current and older code, you'll find it useful later. ;-) On to the current problem. Think about what you're trying to do. How many #'s are printed on the ...


1

Normal practice is to use a for loop when the number of passes is well defined, either by a hard coded value or by a value contained in a variable. When the number of passes is based on a condition or a non-well-defined value, a while loop is preferred. Having said this, if you want to continue your approach, it is doable. You might want to try to write it ...


1

You need a semicolon immediately after each for loop. Otherwise, all the statements until the next semicolon are considered the body of that for loop. The compiler will complain if you put a semicolon on the same line as an empty-body, so it needs to be by itself on the next line. https://www.quora.com/What-does-a-for-loop-without-curly-braces-do Also, you ...


1

Do you know how you would confirm that the variable was stored? i.e. in response to the question "confirming (for yourself) that you’ve indeed stored the user’s input successfully?" $ ./mario Height: -1 Height: 0 Height: 42 Height: 50 Height: 4 Stored: 4


1

I fixed it for you. #include <cs50.h> int main(void) { //Prompt for the height of the pyramid int height; do { //Takes input, assigns it to height and prints it height = get_int("Pick a positive integer between 1 and 8:"); printf("Height: %d\n", height); } while (height<1 || height>8)...


1

You are averaging the values in the wrong place. You have to calculate the average after you have added all the values around the current pixel. Don't forget to reset the variables for the following iterations.


1

For rewinding the input file, you forgot the - in front of (bi.biWidth * 3 + padding). With a positive number and SEEK_CUR, you skip forward.


1

I'll give you several nudges. Most of the problems arise from the nested for loop structure being off. For example, the current code writes out padding after writing out every pixel. It should only write out padding at the end of each line. Next, the code currently reads one pixel and writes one pixel, N times. It should be reading each pixel ONCE and ...


1

Did you notice that the first line is ok and everything after that is bad? Remember that there's padding at the end of each line in the input file. The code needs to skip over that padding before starting to process the next line. There's nothing in the code to do that. Side note: Your code calculates the output padding inside the for loop that processes ...


1

Have you actually looked at the raw hex data contained in the program's output files? Notice anything interesting? Where is the bmp signature that's supposed to be in the first 2 bytes? Next question: Which header is supposed to be printed out first, bi or bf? ;-)


1

With your remove, you only remove the first occurrence of the element, and only from one list. Also, changing the iterable you are iterating is bad style, can lead to unexpected or sometimes even undefined behaviour. One approach would be to remove duplicates before you enter the nested loops, which can be achieved in many ways. I used sets, those are ...


1

You've run into float imprecision, which is the lesson of this Problem. The spec and walkthrough advise you to convert the user's float amount into an integer representing the number of cents (and to beware of imprecision by rounding that value rather truncating). Then, do all of your calculations with integers.


1

Think about the logic of printing hashes. First, forget about the spaces when printing hashes. Next, think about these questions: How many hashes are supposed to be on the first line of the pyramid? Does the number of hashes on the first line change if a pyramid has a height of 1 or 5 or 23? Does a line always have one more hash than the line before? With ...


1

First issue: The code has a logical infinite nested loop because the same control variable, o, is used in two loop setups. The result is that the outer loop never completes. Next, a programming tip. If you see the exact same code repeated as a block, then it should raise a red flag that the second copy of the code is probably not necessary. In this case,...


1

First of all, you shouldn't have to have an if statement to calculate the image size depending on whether there is padding or not. Next, think about this. biWidth is measured in pixels. Padding is measured in bytes. Exactly what are you multiplying by 3 and why? If this answers your question, please click on the check mark to accept. Let's keep up on ...


1

You don't mention what the error is, but more than likely it's caused by you returning a list inside a list. If list(s) returns a list, then [list(s)] returns a list containing one list. You should simply return the list.


1

Oh my god those fixes did it. Actually thought about the width and height issue right after I posted this question. Thank you so much! This pset has been a real bear for me and I've been going back and forth on all of the components (whodunit, resize and recover) for about three weeks now. So glad to be done with this! Hopefully I can finish recover and move ...


1

You might consider cloning your workspace in CS50 IDE > Dashboard. Then experiment in the clone to learn whether it will "break" your IDE. Since backup is hardly ever a bad idea, you can backup your workspace to your local computer with File -> Download Workspace.


1

return [] This returns an empty list. So all of your functions are returning empty. Be sure you return a list of the strings; don't print them.


1

The problem is in vertical scaling - the code isn't complete. The code is scaling horizontally just fine. It also looks like it's set up to repeat processing each line for vertical replication, but it doesn't go back to the beginning of the line before trying to reprocess it. The net effect is that the code is correctly scaling horizontally, but ...


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