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
3
votes
3 answers

How to add product digits rather than products themselves in C?

I am trying to finish an assignment in C for the CS50 course in which I must implement Luhn's algorithm to validate a credit card number. Here is a quick example to elaborate: credit card number: 4003600000000014. Now for every other digit,…
Bryce
  • 71
  • 8
3
votes
1 answer

How to count longest sequence of recurring characters in very long string inside text file in Python

Hello my question is how to count the longest sequence of AGATC (meaning AGATCAGATCis sequence of two) in the very long string. I need to use python. My code for now looks like this: pattern = re.compile(r'AGATC') matches =…
3
votes
1 answer

Made a script on the Luhn's Alg. Unable to understand how to create and call functions clearly for revised script

I am new to programming. I am currently taking online lectures quite rigorously and completed a task using Luhn's Algorithm. It is just one script that runs straight through, but for my future-self I want to learn to code more efficiently as…
schu97
  • 31
  • 4
3
votes
4 answers

The Exclamation Notation and `strcmp` Function in C programming

There is a question that makes me confused during learning with the Harvard University CS50 course. The following is the question that bothers me for a long time. For the following code, it wants to compare the string called "EMMA" with the array…
Ricky.L
  • 47
  • 4
3
votes
3 answers

Pset7 - Movies Stuck on 12 and 13 SQL?

I am currently working of CS50 PSET7 (https://cs50.harvard.edu/x/2020/psets/7/movies/) and I CAN NOT figure out how to do 12.sql and 13.sql (explained in link). Can someone PLEASE help me?
3
votes
2 answers

Why can't I declare more than one variable in a for loop's initial statement?

When using a with one variable, we declare it to 0 as i=0 like here but when we use two variables as if I add n = strlen to make the code more efficient then i=0 is not declared but a comma is used and n=strlen(s) is declared. Why can't we use…
user12027492
3
votes
2 answers

Recovering JPEG image from .raw file using C

I am doing a problem set provided by Harvard's online lecture. I've finally made a solution to recover a set of JPEG images from a file (card.raw). It seems like the code itself does not throw errors, but it is returning distorted image and I am a…
JasonTheMarketer
  • 197
  • 2
  • 5
  • 16
3
votes
1 answer

How can I make a variable salt to be used in the crypt function in C?

As an assignment, I have to find a password of a user starting from the hash of that password (which was made using crypt). So, I'm trying to create a variable salt (2-letter string) which I then tend to use in the crypt function until the result…
Maxim
  • 31
  • 1
3
votes
2 answers

I am trying to perform a Caesar shift on the character z (ASCII 122). How do I give it "more room" to prevent it from overflowing past 127?

In the CS50 2019 Caesar assignment, I am supposed to perform Caesar shifts on characters by a given number of letters (key). To do this, I add the value of key to each letter, as such: for each character in plaintext: plaintext[character] +=…
yeojunjie
  • 41
  • 4
3
votes
7 answers

I am having issues with the get_string function

I just finally got the cs50 library working after a lot of trial on my Windows Vscode. Now, the problem is that the get_string function would not work as used below: int main(void) { string s = get_string("Enter string: "); // ensure string…
Darotudeen
  • 1,914
  • 4
  • 21
  • 36
3
votes
2 answers

Issue with while loop executing when it shouldn't (c)

So as part of a computer science course I've been doing a little bit of C. One of the challenges was to create a program that would tell a hypothetical cashier how many coins they would need in order to give change to a customer. I accomplished this…
3
votes
3 answers

Why am I getting "double free or corruption" with the following code?

I am writing a simple program to make sure I fully understand how pointers in C work as I go through Harvard's CS50. Here is the code: #include #include int main(void) { int *a = malloc(sizeof(int)); *a = 5; …
Matvey
  • 335
  • 4
  • 18
3
votes
3 answers

CS50 Recover Segmentation Fault issue

The goal of this program is recover JPGs from a file. I've been working on this problem for about four or five days in the CS50 online class and I just cannot figure it out. I continue to get a segmentation fault and I have no idea why. I've tried…
3
votes
1 answer

CS50: LIKE operator, variable substitution with % expansion

I am trying to get a query into a variable called results, in which I query the database to find the books with a title like the input from the search bar received from a post method. The query I am running is as follows: results =…
DGs
  • 89
  • 10
3
votes
3 answers

C - out of bounds for type string

Im trying to solve some problem sets of CS50. I need to extract the first two characters of a string "stra", then concatenate them and convert to a string, then compare that string to strings in an array. I have read vastly that in C there are no…