3
votes
Accepted
Pset7, Index: How to use loops in Python and Jinja?
The for loop is working and is displaying exactly the data it has. And it technically (theoretically?) is identical to a for loop in C. Perform this code x times. Syntactically, you (normally) tell C ...
2
votes
2
votes
Accepted
PSET6 Load - How To Approach? Also, Is It Better To Use While() Loop or For() Loop?
Q1. Should I use fgetc() or fread()
A1.: You can make both work! Choose the one you're more comfortable with.
Q2. I don't know whether to use a for loop or a while loop. Is one better for this ...
2
votes
Accepted
FOR loop error when compiled
A small error. The second int in your for statement is redundant and the compiler doesn't like it. The first one will apply to both i and j. Remove the second int and you should be ok.
If this ...
2
votes
Accepted
Caesar : Extra random characters at the end of a char ciphertext array
You have to understand how printf and strlen work. Both of them (and several other functions) depend on the existence of the end of string marker, \0 at the end of the target string. If the EOS marker ...
2
votes
Accepted
PSET2 Vigenere: Output bug, keyword loop going over strlen of key
There are a number of problems with this code. Let's deal with your question first.
The code is constructed as a nested pair of loops. The outer for loop will loop over the length of the plain text ...
2
votes
i want the number of line in which the function is locate , like the function "func2" is in second line so the output shoud be 2
The CS50 StackExchange is for questions about the CS50 course (and problem sets, etc.).
It is not a general "I need help with my homework from another course."-forum.
I'd suggest that if this is not ...
2
votes
Stumped on PSET 1 Mario Less Comfortable
Looks like you have an extra for loop there, which will cause the entire thing to loop l times when that's probably not what you intended.
1
vote
Accepted
Isalpha not working for all char of my string
There are a couple issues here.
First, there's a known issue with debug50. For certain library functions, if you "step into" the line of code with the function call, debug50 will try to step ...
1
vote
Accepted
Running an 'if' statement inside of a 'for' loop; Need to continue loop w/o printing anything, but not sure what value to return
First, since not all the relevant code is here, we have to make some assumptions. First, length = strlen(key). Second, this is in main and not in a function that's called by main. Third, the code ...
1
vote
PSet1 - Coins issue
You also have the problem that it will always produce 0 as a result because of this:
return 0;
Instead, it should
return quarter;
That still leaves the problem of leaving the correct amount of ...
1
vote
PSet1 - Coins issue
There's a semi-colon after your for loop, so it's simply ignoring the body. Remove that and you should be good.
You can do this without a loop. Think about how you would approach this if you were to ...
1
vote
Stumped on PSET 1 Mario Less Comfortable
in your code check the first for loop, why is the counter decreasing ?
why do you need 4 for loops?
to create the right aligned pyramid you need only 3 for loops.
external loop, to count from the ...
1
vote
Accepted
pset4 how does incrementing j help to iterate over pixels?
Both the i and j counters are used to count passes through the loops. They aren't actually used inside the loops, but they don't need to be.
Let's start with the inner loop. biWidth is the number of ...
1
vote
Accepted
Pset2 - Vigenere - Encypts all variations of "barfoo" as "ca\..." (no case issues)
The problem lies in the last for loop. The for loop is controlled by strlen(pt), which is recalculated on every loop iteration. Inside the loop, the ASCII values of the letters in pt are converted to ...
1
vote
PSET3 Game of Fifteen init for loop stops unexpected
Your Code seem fine
I just executed it and it runs okay. Update and restart your IDE and see if that solve your problem.
1
vote
Accepted
pset4 resize - can't write all the characters from infile to outfile
There is "\n" (new line) char in the end of the first raw. So there are 33 chars, while you read-write only 32.
1
vote
Accepted
For loop help in python
In a dictionary the indices use strings, whereas lists are indexed using integers beginning with 0. The error:
TypeError: list indices must be integers, not dict
is because you are using string as ...
1
vote
Accepted
CS50 Final Project Jinja For Loop Not Running
It's running but nothing "matches" (ie is true) because of the date formats. Assuming "due_date" is TEXT in the db with the format "YYYY-MM-DD", current_date needs to be ...
1
vote
Accepted
Python - for loop not being executed, leading to NameError of variables defined in the loop
If y is 1, the z loop doesn't run. From w3schools:
The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified ...
1
vote
Accepted
pset3 Plurality
https://cs50.stackexchange.com/a/38819/25714
Check this post on seg fault (plurality) that I have answered.
If found to help be sure to tick the answer to keep other smiling too 😃
1
vote
Accepted
what is the significance of break; in tabulate function?
Each round counts one vote per voter, for their top preferred, non-eliminated candidate. Once that vote is counted, program moves on to the next voter. From the C99 standard section 6.8.6.3:
A ...
1
vote
Accepted
How To Use a For Loop To Store Values Into Multiple Variables?
Since you have identified in your code sample above that you understand how to declare and put values in an array. Then I suggest the simple means is to just create an array of 8 length that can store ...
1
vote
Accepted
Pset 2 - loop question
Maybe you're just overthinking it a little? ;-)
Just move the printf(Success) statement below the for loop. If the test fails, it's going to exit through the return statement. Otherwise, the code ...
1
vote
PSET2 Vigenere: Output bug, keyword loop going over strlen of key
I would recommend you to do this problem set in one for loop. You can just loop through the plaintext. Using nested loops will make the problem more complicates than you think. It's because you only ...
1
vote
How do I add a condition to a for() loop in Python?
You could use while and somewhat emulate for, or do an if with a break inside your loop (which I would prefer).
1
vote
Accepted
PSet1 While Loop to Print Spaces for Mario Not Working
For the first inner loop, use a while loop, not do..while, as you don't want any space printed in front on the last line.
The difference between while and do..while is that the do..while runs at ...
1
vote
PS7: Please give me a hint on flask loop usage
If I understand this correctly, you are seeing the list of stocks but symbol, share_count, price_current and holding all display the values from the first stock. I think this "Am I meant to be using ...
1
vote
Accepted
CS50 2017 Problem Set 6 Mario.py For loop
I don't think it's a good idea to name the loop variable spaces when that name is already taken; that's probably where it goes wrong.
Also, why don't you multiply the spaces like you did with the ...
1
vote
Accepted
pset1 mario.c this works, but can I better optimise?
My solution is very similar to yours.
The printf("%s", " "); could be changed to printf(" ");. You could (not sure if you should) combine the two for-loops
for(int j = 0; j < h-i-1; j++) //...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
for-loop × 60pset1 × 13
cs50x × 10
mario × 10
c × 7
pset2 × 6
python × 5
string × 4
pset4 × 3
pset7 × 3
finance × 3
jinja × 3
while-loop × 3
pset3 × 2
pset6 × 2
resize × 2
vigenere × 2
caesar × 2
greedy × 2
flask × 2
whodunit × 2
compilation-error × 2
pset6python × 2
isalpha × 2
for × 2