Questions tagged [for-loop]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0
votes
1answer
17 views

Python - for loop not being executed, leading to NameError of variables defined in the loop

It's a simple Python program to find the number of primes until a certain number. The for z in range(1,y): loop does not execute, but rather execution jumps to the print(count," Pr...) statement ...
1
vote
1answer
45 views

pset3 Plurality

Having an issure writing my plurality code pset. i wanted to have my programme promt a user when they have made an invalid vote entry. and it works ok but ive found a bug while testing it: code: #...
0
votes
1answer
17 views

My program doesn't run when i close the condition's range. When i leave the range open it does't give the answer i want

#include <cs50.h> #include <stdio.h> #include <math.h> int main(void) { float change; int cents = 0, coin_number = 0, quart = 25, dime = 10, nickel = 5, penny = 1; //...
0
votes
1answer
21 views

Hexadecimal to Decimal Conversion - Loop runs in reverse order (opposite to what is expected)!

The following code converts Hexadecimal numbers to Decimal numbers (successfully). I'm unable to figure out why the code behaves in just the opposite manner as it should be! I was planning to write: ...
0
votes
1answer
119 views

what is the significance of break; in tabulate function?

I know break statements are pretty basic, but it's got me here: This is my function: for (int i = 0; i < voter_count; i++) { for (int j = 0; j < candidate_count; j++) { if (...
0
votes
1answer
170 views

How To Use a For Loop To Store Values Into Multiple Variables?

I'm working on the Credit problem set. Along the way, I've encountered a scenario where I want to use a for loop to store multiple values. If I type printf("%d", cc_number[p]); in that for loop, it ...
0
votes
1answer
14 views

Pset 2 - loop question

im trying to make it so my program returns Success after it checks whether the provided argument is all digits. i can see why this code prints success 3 times if i provide a 3 digit number as the ...
0
votes
2answers
78 views

PSET2 Vigenere: Output bug, keyword loop going over strlen of key

I am having an issue specifically with the second for loop (under "//iterate over key inputs" comment). The counter j is going over the strlen of argv[1], and thus feeding a wrong input to my shift ...
0
votes
1answer
24 views

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 [closed]

#include<stdio.h> #include<string.h> unsigned int FindFunctionDefn( char strFunctionName[], char strSourceCode[] ){ int n = strlen(strSourceCode); int m = strlen(...
0
votes
1answer
35 views

mario and problems with my array

# include <stdio.h> #include <cs50.h> So I've been playing around with this problem alot and unforturnatly asked many questions. But I think I've figured out the basics, if not back to ...
0
votes
2answers
175 views

pset4 how does incrementing j help to iterate over pixels?

I'm currently working my way through understanding the code of copy.c used in "whodunit" and "resize". This loop is supposed to iterate over the scanlines of a Bitmap (by incrementing i) and within ...
0
votes
1answer
51 views

Many Error Messages Surrounding For Loop

I've been working on my caesar.c and I've built this so far. Unfortunately when I try to compile it I'm greeted with a flood of error messages surrounding the for loop noted below. I cannot figure out ...
1
vote
1answer
46 views

Pset2 - Vigenere - Encypts all variations of “barfoo” as “ca\…” (no case issues)

This is my first question on Stack Exchange. I had a few different issues with this problem but I felt I'd finally sorted them out until I ran into this. I've been working for a number of hours today ...
0
votes
1answer
28 views

PSET3 Game of Fifteen init for loop stops unexpected

So I am stuck at initializing the board. my for loop is doing what it should but it stops when it is not actually done. The board I initialize is made of the dimensions d * d and should start left to ...
0
votes
0answers
38 views

What is wrong with my code?

In this program I am trying to receive an input as a string using argv[] and then I am trying store corresponding values of argv[1][i] into an array var[i] using switch case and I am finally adding up ...
0
votes
1answer
28 views

I have described below the problem. Can someone answer? [closed]

For(i=4; i<8 ) so in the start with i = 4 and I want to get 4+1=5 5+8=13 13/2=6 6+1=7 7+8=15 15/2=7 7+1=8 0,1,2,3,4,5,6,7,8 How do I do this?
0
votes
1answer
43 views

pset4 resize - can't write all the characters from infile to outfile

everyone! I really need your help. I'm working on pset4 and trying to understand the logic of resize problem. I have two files:testinp.txt and testout.txt. Inside of testinp.txt I have: ...
0
votes
1answer
549 views

Problem with “expression result unused error” in for-loop?

I am getting an error message of "expression result unused" for the bottom two for loops (specifically h + 1 and s - 1). I've tried looking for through the other questions for an answer but nothing ...
0
votes
1answer
746 views

For loop help in python

i'm trying to do a 'for' loop in python, but my values keeps getting replaced. How can i write the code below in a way where i can keep a record of each iteration? Should i be using a list[] instead ...
1
vote
1answer
4k views

Pset7, Index: How to use loops in Python and Jinja?

I felt like I could understand the for loops great in c, but seem to be hitting a wall here. Some generic conceptual questions before i get into it: 1) ANSWERED. what sequence does the program run, ...
0
votes
1answer
223 views

How do I add a condition to a for() loop in Python?

I'm porting crack.c to crack.py. I crack.c, I am able to do this: for (int i = 0; i < sizeof(letters) && strcmp(hash, solve) != 0; i++) In order to break the for() loop if I find the ...
0
votes
1answer
136 views

new to Python: for loop

I see David using a lot of for loops like this, usually in a table: for (singular variable) in (plural variable) e.g. for registrant in registrants He doesn't actually declare the singular ...
1
vote
1answer
105 views

PSet1 While Loop to Print Spaces for Mario Not Working

I have approached this problem by using a forloop with two nested loops. The second nested loop works and prints the correct number of hashes. The first nested loops does not appear to working at all, ...
3
votes
3answers
12k views

MARIO LESS COMFORTABLE

I have been working at this pset for hours. within 5 minutes i got the hello.c and water.c assignments completed. But when it comes to mario.c, I am utterly lost. I have watched every video. I ...
0
votes
0answers
397 views

I'm working on isbn.c. Here, instead of one 'yes', it's displaying several yes'es. What mistake did I make?

Why is my output showing 'yes' multiple times? I think it's because the values of 'num' and 'num1' are being run separately each time to make the value 'finally', but I'm not sure. Please help me fix ...
0
votes
2answers
60 views

PS7: Please give me a hint on flask loop usage

Despite reading documentation I cannot figure out why my loop is not functioning properly and is only dynamic for stock symbol? Am I meant to be using multiple nested loops passing all prices, holding ...
0
votes
1answer
1k views

CS50 2017 Problem Set 6 Mario.py For loop

What is wrong with my second For Loop (spaces)? It seems like it only runs one time, according to the first line of the pyramid. import cs50 while True: print("Type a number between 0 and 23: ", ...
0
votes
1answer
80 views

pset1 mario.c this works, but can I better optimise?

Here is my code: include include int main(void) { int h; //gettin some input. do { h = get_int(); printf("height:\n"); //prompting for input. } while (h <= 0 || h > 23); //...
0
votes
1answer
327 views

Loop breaking in quick sort algorithm

Implement quick sort algorithm with faulty results. Details of what I want to do can be found at the video: https://courses.edx.org/courses/course-v1:HarvardX+CS50+X/courseware/...
0
votes
1answer
61 views

Parentheses or Brackets Problem?

I get this error, but I can't find where I forgot a paren / bracket... int SUM1 = 0; if ( (length == 13) || (length == 15) ) { for (int a = 1; a < length; a++2) { ...
0
votes
1answer
66 views

PSET6 Load - How To Approach? Also, Is It Better To Use While() Loop or For() Loop?

I watched the walkthrough for load, but I am struggling to understand what the problem set is asking for. What is load's objective, and what is the best way to approach load? I also don't know ...
0
votes
1answer
66 views

vigenere.c cs50 plaintext for loop starts after the key array loop ends

i need help in the sense that if i run my program with the key being one letter, whether capital or lowercase, it works properly, but if more than one letters in the key, the last letter is used for ...
0
votes
1answer
18 views

FOR loop error when compiled

Hey guys i was getting some compile errors on for loops that I thought to be sound, i put together a simple test for loop and received the same errors. Have i made a mistake or is there something ...
0
votes
1answer
267 views

Hacker pset1 credit.c always gives invalid?

I tried to attempt the credit.c problem. My code is as follows: #include <stdio.h> #include <cs50.h> #include <math.h> #include <string.h> int main(void) { printf("Please ...
1
vote
1answer
514 views

still needing help with mario

I have read and re-read through the help provided for mario here and other places, but am stuck at getting the right number of lines, but not the right characters in each line (i am substituting a for ...
0
votes
1answer
187 views

For Loops in Itsa Mario pset1

I don't understand what to put in for the (initialization; condition; update) part of the for loop. I used 4 variables in my code: i = height, r = rows, j = spaces, n = hashes. Whenever I type in "...
0
votes
1answer
66 views

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

//My code prints all of the coins //I cannot seem to be able to use the variables that are inside the for loop, outside that loop. //Any Ideas? int main(void) { float f; do { ...
0
votes
1answer
17 views

New at programming…Code output different than expected. Any ideas why?

I am working with flags and have the following code. The output I should receive is below the code; however, when I compile, the output is different. Can someone explain why this is? Thanks. int ...
0
votes
1answer
834 views

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

I'm trying to write a function to count the number of alphanumeric character(a,b,c,d,..,z), in a string. My code looks like it doesn't have error..., but when I run it, function stops to count ...
0
votes
1answer
226 views

Help! I am stuck on Mario-pset1. Pyramid

I am stuck on Mario. I keep getting a "redefintion" error in my loops when I try to make the pyramid. My problem is in the conditions part of my for loops, I think. // Get a number for building the ...
0
votes
1answer
189 views

Comparing chars

I'm trying to write some code that will take input, loop through the characters in the input, and print "hi" for every "a" that shows up in the input. I'm getting errors trying to compare the looped ...
0
votes
2answers
122 views

CS50 Mario, Signs 'n' Logic

I'm havin' a little tough time with Mario's pyramid. Well the essence of this question is I don't get some of the "triggers" and signs on the code. I mean, I don't understand some signs, e.g. the i++, ...
0
votes
2answers
458 views

pset1 Mario. Spaces work, hashes don't and i can't see why

So I have been working on CS50's pset 1, making marios half pyramid. I have been looking over other problems on this site and I've gotten this far. But I can't seem to see what the problem is with my ...
1
vote
1answer
767 views

How do I resolve a `for loop initial declarations` error while compiling?

I just copied copy.c to whodunit.c. Then I saved and tried to compile the program by running make whodunit. This resulted in the following error. whodunit.c: 73:5 error: for loop initial ...
0
votes
2answers
634 views

Mario pset1 for-loop spaces

Ok, just I found this on edX and I have started to work through it. I'm having trouble getting the for loops for Mario in pset1. Everything I get one to work, the other suddenly stops working. Here ...
2
votes
1answer
13k views

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

Is there a way to initialize two different types of variables in a for loop? For example: for (int i = 0, n = strlen(oldStr), char a, b; i < n; i++) So the variables i and n are ints while a and ...
3
votes
2answers
13k views

Problem with “expression result unused” error in for-loop

There is obviously something missing in my understanding of the for-loop. Part of my code looks like this: int height; do { printf("Height?: "); height = GetInt(); } while (height < 0 || ...