7

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 given it a variable. There are 2 ways around that. One way: for (int i = height; i>0; i--) and then, in your inner loop, use i rather than height. Or, ...


3

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 how many times (eg for (i = 0; i < strlen(somestring); i++). The for stock in stocks notation is "syntactic sugar"; it knows how long stocks is, so it does ...


2

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 pset? A2.: In C you can translate a for loop to a while loop and vice-versa. So, again, choose the one you like best. Here's some pseudocode for you to ...


2

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 answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)


2

Using the 5 pyramid as a guide: Height: 5          ##        ###      ####    #####  ###### The psuedocode is straightforward: for height times    for s times        ...


2

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 three sections. A quick fix would be declaring them before the for loop as follows char a, b; for (int i = 0, n = strlen(oldStr), a = 'a', b = 'b'; i < n; ...


2

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 to be encoded. The inner for loop will loop over the key. That means that for EACH character in the plain text, the entire key will be looped over. Why? The ...


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 CS50-related, and I see no indication that it is, that you choose another forum. Perhaps Stack Overflow?


1

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 number. Error will come in the print because total is never defined/declared. For y > 1 name error will come in bench function because n is never defined/...


1

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

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 break statement terminates execution of the smallest enclosing switch or iteration statement. In this case it terminates the the j loop and moves on to the ...


1

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 the values. Also in a for loop, in the initialization part you can declare more than one variable. So from your code sample you could do something like: int ...


1

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 has to drop out of the loop, at which point it must have passedm so that's when you print a success message. If this answers your question, please click on the ...


1

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 need to loop through the plaintext and do changes to each character in the plaintext. Some other advice with the code is, what if the plaintext is upper case, ...


1

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 pixels in a line. j counts from 0 to biWidth, counting by 1 with each pass through the loop. Inside the loop, one pixel is processed in each pass. So, j is ...


1

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 numbers from 0 to 25, but are not converted back to encoded letters. The effect is this. When an 'a' or 'A' is encoded, it is transformed to 0. It is ...


1

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

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

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 indices instead of integers. If you want to use strings like "name" to get values from the array you should use a dictionary. Your values will get replaced for ...


1

You could use while and somewhat emulate for, or do an if with a break inside your loop (which I would prefer).


1

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 least once. Here, I'd prefer for loops instead.


1

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 multiple nested loops passing all prices, holding values, etc as dictionaries? is partially true. I don't know as you need to use "multiple nested loops", or any ...


1

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 hashes? And you need the end="" only when you don't want the newline character to be added at the end. For the last part of the line, you actually want a newline. ...


1

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++) //incrementing for j { printf("%s", " "); //aligning right. } for(int k = 0; k < i+2; k++) { printf("#"); //printing the hashes ...


1

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. For this you want to work it out with pen and paper first using some of your left brain. For the bottom line (i=0), you want n+1 #'s and no a's. Taking n=8, for ...


1

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 the variable is outside of. So, to use variables outside of a loop, they must simply be created outside the loop. the float f is a perfect example. They need ...


1

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>; <condition>; <increment command>) In your case your <increment command> is i++, so i gets incremented by 1 in each iteration. But, in your loop, after ...


1

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 paper first. What happens if pyramid = 8, as is the illustration in the pset specs, for example? space=7 and hash=1 in the first iteration for the 1st level of ...


1

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 (x == y){ // compares a char and a string y is a pointer to a string, not a character. You are attempting to compare a char to a pointer. There are two ...


1

This problem was most likely caused by the original make compilation settings in the Appliance. A simple update50 command fixed it, as the poster noted in a comment to the original post. The for loop syntax that we learn in CS50, an example of which is included below, is defined in the C99 standard for the C programming language. for (int i = 0; i < 10; ...


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