8 votes
Accepted

Problem with "expression result unused" error in for-loop

In a for loop, the first item is the initialization. You are saying for (height; height>0; height--) and the compiler is expecting that first item to be an initialization but you have simply ...
user avatar
  • 18.3k
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 ...
user avatar
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 ...
user avatar
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 ...
user avatar
  • 63.4k
2 votes

MARIO LESS COMFORTABLE

Using the 5 pyramid as a guide: Height: 5          ##        ###      ####  &...
user avatar
2 votes

How to initialize multiple types of variables in a FOR loop?

Short answer is no. There's no way to do it the way you want because declaring variables of two different types requires a semicolon between the two declarations and the for loop can't have more than ...
user avatar
  • 17.4k
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 ...
user avatar
  • 63.4k
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 ...
user avatar
  • 18.3k
2 votes
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 😃
user avatar
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.
user avatar
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 ...
user avatar
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 ...
user avatar
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 ...
user avatar
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 ...
user avatar
  • 63.4k
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 ...
user avatar
  • 55
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 ...
user avatar
  • 63.4k
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 ...
user avatar
  • 63.4k
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.
user avatar
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.
user avatar
  • 1,139
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 ...
user avatar
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).
user avatar
  • 20.7k
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 ...
user avatar
  • 20.7k
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 ...
user avatar
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 ...
user avatar
  • 20.7k
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++) //...
user avatar
  • 20.7k
1 vote

still needing help with mario

I think you have it the other way around, you are getting the correct number of lines (height n) but the wrong combination of characters per line. Your code generates 4 a's and 2 #'s for each line. ...
user avatar
  • 1,415
1 vote
Accepted

Pset1 Greedy: How can i use a variable that is inside a for loop, outside the for loop?

You are running into issues of scope. Very simply put, a variable only exists inside the curly braces that surround it. It will exist to lower levels of code (code blocks bound by curly braces) that ...
user avatar
  • 63.4k
1 vote
Accepted

C prog - COUNTER to find the number of alphanumeric's character in a string?

Your problem is pretty simple. In a for loop, after the end of each loop, the third parameter (<increment command>) of the loop is executed, every time. for (<initialization>; <...
user avatar
  • 7,326
1 vote

Help! I am stuck on Mario-pset1. Pyramid

there are a few things wrong with your code: you want an int from 0 to 23, your while condition isn't checking for that you want to rethink the loops for printing out spaces and hashes. Use a pen and ...
user avatar
  • 1,415
1 vote

Comparing chars

Your problem is much more basic and simple. Look at these lines: char x; // a char variable char *y; // a pointer to a string y = "a"; // loads a string into memory at y if ...
user avatar
  • 63.4k

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