Questions tagged [cs50]

Code questions pertaining to Harvard's self-study introductory. It is STRONGLY RECOMMENDED to additionally tag with C or Python. Also consider using the spin-off https://cs50.stackexchange.com/

Note there is a spin-off site at https://cs50.stackexchange.com/. Consider joining the community and directing your questions there.


The CS50 (a.k.a. CSCI S-50) courses are computer science courses from Harvard University as part of its edX program.

There is a C library available at CS50 Library which provides support functions for the course.

There is also a Python library at CS50 Library which provides support functions for the course.

The tag should be used primarily for questions also tagged or (though the course does use some other languages too, and if there is a need, the extra languages would be valid too).

This tag should be used for questions which use the "cs50.h" header, python-cs50 or -lcs50 library. Another symptom that it is appropriate is the use of the type string (which is defined with typedef char *string; in the cs50.h header) or functions such as get_char(), get_double(), get_float(), get_string(), get_int(), get_long_long() and SQL().

The term 'pset' with a number (e.g. PSET5 — for Problem Set 5), with varying capitalizations is also often an indication that could be appropriate.

  • As an alternative to edx, there is a standalone website (called CS50x) where all the course materials and problem sets can be found.
  • There is CS50 Stack Exchange site dedicated to the course.
  • You can run CS50 programs in Visual Studio Code for CS50 (Codespaces)
  • The previous CS50 IDE has been deprecated. Use the Codespaces link above instead. For reference, here is the link to the previous CS50 IDE
  • CS50 has a "CS50 Sandbox" to run in a test environment. The CS50 Sandbox will be decommissioned on July 1, 2023. Sandbox URL: http://sandbox.cs50.io/ A suggested alternative after that date is Replit
3444 questions
5
votes
5 answers

Is There C equivalent to Python's: if __name__ == '__main__': to write main() at the top of the file?

In C writing function declarations before main seem redundant. I don't want to use these methods: Write the functions in a different file then import on top. Write the whole function before main. if __name__ == '__main__': main() What is the…
5
votes
1 answer

undefined reference to `__ubsan_handle_nonnull_arg'

I have been working on the problem set speller for the last days and so far this is what I have. Unfortunately, it does not compile and I am a bit lost. I would be really grateful if somebody can help me out and tell me, what I am doing wrong. //…
viet
  • 61
  • 1
  • 2
5
votes
1 answer

Exiting with 1 in Python

This is one of my first programs in Python, so I might be missing something obvious. In this first part of my code that I've posted, I'd like to make sure the user passes a command line argument. If not, I want to show an error and return 1. When I…
kwicz
  • 63
  • 7
5
votes
1 answer

C round function is throwing error: "undefined reference to `round'..."

I hope someone can help me. I am working through CS50x and am working on Pset1 - greedy. I am getting the following error whenever I compile my code: /tmp/greedy-46be96.o: In function `main': greedy.c:(.text+0x95): undefined reference to…
Phrike
  • 91
  • 1
  • 2
  • 7
5
votes
2 answers

How to make my hashing algorithm faster

My question is connected with task from CS50, pset5. For ones who don't know any about that, I'll try to explain. Nothing very special. I just need to make function which will intake dictionary file (it was written before, all of the words in that…
4
votes
2 answers

Why can an array used in fread/fwrite in a position of a buffer be written with or without & and yield the same result?

Why do these yield the same result? uint8_t header[HEADER_SIZE]; fread(&header, sizeof(uint8_t), HEADER_SIZE, input); fwrite(&header, sizeof(uint8_t), HEADER_SIZE, output); uint8_t header[HEADER_SIZE]; fread(header, sizeof(uint8_t), HEADER_SIZE,…
4
votes
1 answer

I am trying to write a bool function that checks if the string input has any duplicate characters

I wrote this function under main I want to check if the string input has any duplicate characters but when I used the debug feature it turns out that the outer loop only loops once while the inner loop loops correctly. I don't know why the outer…
4
votes
3 answers

Non-function do while loop in C

I'm new to programming and was hoping I could get some help. I'm attempting to create a 'do while' loop in C as part of my CS50 problem sets. I'm attempting to use two different conditions under 'while', but for some reason they are not being…
Disierd
  • 53
  • 4
4
votes
1 answer

How do functions from a header file get linked to the .c file?

In problem set 4 of cs50, there is a main function filter.c, which uses header file helper.h. The latter contains the prototypes of a few functions, and these complete functions are written out in helper.c My question is: since filter.c only…
Zegher V
  • 117
  • 7
4
votes
3 answers

How to catch the longest sequence of a group

The task is to find the longest sequence of a group for instance, given DNA sequence: "AGATCAGATCTTTTTTCTAATGTCTAGGATATATCAGATCAGATCAGATCAGATCAGATC" and it has 7 occurrences of AGATC. (AGATC) matches all occurrences. Is it possible to write a…
hamvee
  • 141
  • 3
  • 13
4
votes
3 answers

Beginners question - Why does C use * to declare a pointer and not &?

So let's say: int n = 50; int *p = &n; Why do we use * and not & in C? What's the key difference between * and &?
4
votes
1 answer

I can't submit my CS50 code solution using terminal on mac

So I am trying to submit my CS50 (https://cs50.harvard.edu/x/2020/tracks/mobile/android/fiftygram/) in my terminal by using submit50 cs50/problems/2020/x/tracks/android/fiftygram. I have installed pip3 and CS50 but I keep getting an error zsh:…
codelearner
  • 173
  • 8
4
votes
3 answers

Python problem with TypeError: unsupported operand type(s) for +: 'int' and 'str'

I'm new to the whole programming world, I've encountered a problem with Python when doing the caesar exercise of cs50. I could not figure out what went wrong, highly appreciate your help! from cs50 import get_string from sys import argv if…
djriles
  • 63
  • 1
  • 4
4
votes
3 answers

Why is implicit declaration of gets() not allowed in C99?

I am starting to learn programming in C language the book I am refering to code shows some source code with gets() and my IDLE recognises it as well. But still while compiling it, my compiler doesn't agree with it. Can anyone help me out? I am using…
Mr. Anderson
  • 49
  • 1
  • 1
  • 3
4
votes
7 answers

Multiple type specifiers in declaration list

#include #include #include int main(void) { //ask user for input string s = get_string(); //make sure get_string() returned a string if(s != NULL) { //iterate over the characters one at a…
phindex
  • 51
  • 2