Hot answers tagged

20 votes

Memory overlap in C

I believe you mean memmove which takes care of memory overlapping as oppose to memset. but what is memory overlapping anyway? suppose we have an array of 5 chars, where each char is a byte long ++++...
  • 17.5k
10 votes

Pset3: Plurality - Weird output!

You should rethink your logic for the print_winners function. Here's the pseudocode I'd use. Declare a maxvote int set to 0. Iterate over the list of candidates and check the number of votes. If ...
  • 18.5k
7 votes

How can I get the cs50.h to run in Visual Studio Code?

I was able to get mine working on my Mac with Visual Studio Code as follow: Assuming you have Visual Studio already installed and with all C/C++ extensions required for compiling and running C codes, ...
7 votes

How can I get the cs50.h to run in Visual Studio Code?

Following are the steps to get the CS50 libraries installed in VSC in Windows 10 with mingw64 environment: Pre requisite: Install Mingw64 to set up VSC for C/C++ programming https://www.youtube.com/...
  • 179
6 votes
Accepted

Can't cast a float to an int?

Your code isn't working for the same reason that this code: int x = 5; x + 5; printf("%i", x); does not print the number 10. In C, (int) is a type casting operator; much like the + operator, it ...
  • 2,146
6 votes
Accepted

implicit declaration of function is invalid in C99

Sorry, beginner error: But now if we try to print_name(s) in our main function, we still get an error. And that’s because the compiler reads from top to bottom, in order, so at the time main ...
6 votes
Accepted

Pset2 Readabilty: Why are my grade levels calculating incorrectly?

This is integer division. When you divide two integers, the result is an integer again, truncating the result, ignoring its fractional part. You can turn it into floating point division by making at ...
  • 20.8k
5 votes
Accepted

CS50 IDE undefined reference to `get_int'

On which version of the workspace are you? The current version as of 2017-11-13 is v125. If update50 doesn't end in an error message, you probably are at that version. Also, how do you compile? The ...
  • 20.8k
4 votes

What is markup language?

what exactly a markup language is and how it is different from programming language and scripting languages in layman's terms, a markup language is a set of markup tags. according to this: A ...
  • 17.5k
4 votes
Accepted

recover.c cannot detect jpeg signatures?

Your program is not recovering the images because the cs50 appliance or linux in this case uses the little-endian convention to store or read the data. I really don't know very much about this myself, ...
  • 1,389
4 votes

understanding 3D arrays

A 2D array is an array of arrays. a 3D array is an array of arrays of arrays. in this case we have an array of 2 arrays each of which is of 3 arrays each of which is 2 elements long. recall that, in ...
  • 17.5k
4 votes
Accepted

pset4 recover: recovered images seems to still be corrupted

if (jpegsFound > -1) Shouldn't that be an else if? Your current code says: read in 512 bytes if it's a signature close file (if open) open new file increment counter ...
  • 18.5k
4 votes

pset4 recover: recovered images seems to still be corrupted

I agree with Brenda's answer above. But also, you're initialising jpegsFound to 0. That's the value it will have before any signatures were found. Are you sure you want to write to the file when ...
  • 1,677
4 votes

Help with blur filter for pset4! Output image is significantly darker than input

Got the solution. Using the BYTE type to store the color values resulted in overflow. Solving by using int or double for redAvg, greenAvg, and blueAvg.
3 votes
Accepted

Why is my `mario` pyramid height restriction not working?

The while condition in a do while loop is the condition that causes the loop to repeat. do { //this } while (this is true) Can you see why yours is incorrect? You have used the opposite ...
  • 18.5k
3 votes
Accepted

Do we need to return 0 in C?

Typically and according to the standard, the return value of main should be of type int which obviously means that you should return an int value from main. However, you don't really have to do that ...
  • 17.5k
3 votes

Greedy hangs after prompting for user input

I keep getting hung up right after the prompt for user input. This is a clear indication that there is an infinite loop (at least once). The problem is that you have 4 infinite loops, the four while ...
  • 3,396
3 votes

Does only malloc allocated memory on the heap?

In the first case, I am clear that memory will be allocated on stack. not necessarily. it depends on where the variable is defined. if it's a global variable for example, the memory will be allocated ...
  • 17.5k
3 votes
Accepted

Create an array for a case statement - confused

If you know the color names, why not simply create an array of strings to hold them? char* color[] = {"RED","ORANGE","YELLOW","BLUE","GREEN","BLACK"}; Then, wherever you need to assign a color, you ...
  • 18.5k
3 votes
Accepted

CS50 PSET2 Check 50 Hello world trouble. Caesar cipher segmentation fault and encipher the plaintext

isupper() does not work on strings, it only works on chars, that's why you get the seg fault. "I did not find anything that helped to iterate over the chars of p. Can you give me a hint on how to do ...
3 votes
Accepted

C vs Python in MOOCs?

You can find the "official" answer from David Malan himself here: "Why does CS50 at Harvard use C as its primary language?" A few words from my own perspective... I began my formal programming ...
  • 1,414
3 votes
Accepted

jpg's fourth byte

& is bitwise AND, the 0xf0 is sometimes referred to as a bit-mask, as the operation sets certain bits to 0. Both operands are integers, interpreted in their binary representation. The result has a ...
  • 20.8k
3 votes
Accepted

Recover.c does nothing

I too have been having the same issue, but I would suggest a few edits that may help get your code working. The biggest issue I see with your code right now is that, after checking the first 512-...
  • 58
3 votes
Accepted

Easiest method to organize code

You went down the rabbit hole with this pset. You chose the wrong path at the fork on the road. Just think for a minute how mush you will have to change, if instead of 5 letters which is the current ...
  • 7,366
3 votes
Accepted

cs50/pset2/vigenere: Code works but check50 rejects 2 item

You are printing garbage values in the final printf statement: printf("ciphertext: %s\n", cipher); //printing of ciphered text You are asking it to print cipher as a string. You declared cipher as ...
3 votes
Accepted

Pset4 Recover: Program creates segmentation fault

There are a couple of problems in the code. Ordinarily, I'd suggest that you need to identify which line is causing the seg fault, but it looks like you're stalled out on that. In this case, running ...
  • 66.2k
3 votes
Accepted

strings an arrays

Follow the numbers. Decimal 1769234766 = hex 6974614E. Now, look at the hex pairs, stored right to left: 4E = N 61 = a 74 = t 69 = i The line feed is easy enough to understand. I'll leave it to ...
  • 66.2k
3 votes

I can´t get (pset1) greedy/cash.c pennies to work properly!

Perhaps you don't understand float imprecision fully enough. If you were to print the value of ch with more digits of precision, you would see what's happening. Try using the following: printf("%0....
  • 66.2k
3 votes
Accepted

cs50 pset1 mario math wrong or order?

There's a couple of problems. First, height is essentially a constant after it is entered by the user. It represents the number of rows to print, and as such, shouldn't be changed by the program. It ...

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