Hot answers tagged

3

int voter_count, candidate_count; candidate candidates[candidate_count]; In C, you cannot define an array with a variable size at a global scope (even if you could, candidate_count does not have a value at compile time so memory could not be allocated appropriately for the array). You need to define the array with a fixed size (let's say 5 for the sake of ...


2

The main problem is in the following statements: string answer = 0; answer[i]= min_value; Let's see if I can explain it clearly; a variable of type string is really a typedef (an alias in other words) of char *, that is, a pointer to a char, the first of the previous statements assigns a null (zero) pointer to answer, the second statement tries to ...


2

strlen takes a pointer value, a memory address, and from this memory location counts the bytes until it hits one of value 0 (in this context often called "null terminator"). Since array2 does not contain a byte of zero, what strlen reports depends on how the compiler placed all the variables on stack. For whatever reason, the stack grows backwards. That ...


2

The error you are getting is not saying that you are passing a double instead of a float, but that a double is expected. You are passing average, which is not a variable but a function. Your prototype declaration for the function (float average (void)) is also throwing an error because it does not match the function, which need has a definition of float ...


1

ranks[] means an array of values that has been named ranks; from the declaration, we can tell that the values will all be integers. Any time you see a name followed by square brackets in C-based languages (C, C++, Java, etc.), you know that you are dealing with an array. If you have an integer or integer variable (such as i) in the square brackets, then you ...


1

When the int array scores is declared here int scores[n]; it will have n elements, indexed to 0. When 3 is entered, the elements of scores are scores[0], scores[1] and scores[2]. There is no scores[3]. (Notice the loop executes until i < n to ensure it will be within bounds). This line scores[n] = get_int("Score %i: ", i + 1); will try to ...


1

First of all, this is why it's important to get the indentation aligned correctly so you can see what goes with what. Here's the corrected indentation. int main(int argc, string argv[]) { if (argc != 2) { printf("Usage: ./caesar key\n"); } else if (argc == 2) { for (int i = 0; i < strlen(argv[1]); i++) { ...


1

candidate zero = candidates[0]; creates a new variable of type candidate and copies over the value from candidates[0]. The new variable gets its initial value from candidates[0], but is not connected in any other way. zero.votes = 100; then sets that copy's votes to 100. If you wanted to have different variables refer to the same actual storage, you ...


1

The problem is in the names of your variables: As most of the names have no meaning, it is difficult to keep track of the meaning for each of these variables. In this case, your variable h makes things too complicated, that's probably why you lost track. You are iterating over the characters of the input and over the characters of the key. But you are ...


1

The reason why a string variable does not have brackets, is because it is not a string array. (It is a char array or a char pointer.) In fact, string is a new type, defined in the cs50 library. It will be explained in one of the following lectures (possibly Lecture 4). And to quote Cliff B: If this answers your question, please click on the check mark ...


1

A simple way is a loop over a copy of the number. number % 10 returns the last digit, while number / 10 returns the number with last digit removed. Remove digits from the copy until it's less than 100 and you have your first two digits. int firstdigits[1]; makes place for one integer at index 0. It doesn't have a firstdigits[1], that would require it to be ...


1

You declare ky as string[keylen], it should be char[keylen+1] (don't forget the null terminator, that's why +1). Don't forget to check argc before accessing argv[1], it might not exist at all. Don't add any number to argv in the main method's signature. I think it doesn't matter, but it might be misleading, as argv's length is stored in argc and not ...


1

int c = 0; ........ int numberArray[c]; By doing this you declare int numberArray[0] that is an array of zero members. So any index is "out of bounds" of this array, that is the mistake you see. Instead, you may want to declare an array of max number of members (16 if I remember correctly), and then fill it with digits (extracting digits from n one by one).


1

the isspace function should return a boolean, whereas you assign a char to the 'return value' of isspace. What you probably want to do is to check if, for every i, the current name[i] is a space, and then store name[i+1] (probably the initial) in some string or char for the initials. y++ takes the value of y and adds one. Because y is a char, thus an ascii ...


1

Oh. My. Goodness. My 2nd test condition wasn't a valid stock (just some random capital letters that I thought looked like an actual stock symbol) so if ($stock !== false) would kill the foreach() loop, thus only filling $positions with one index. On the plus side, in searching high and low for the answer to this ridiculous error I now understand what's ...


1

I'll let you visualize this for yourself. Go here http://www.pythontutor.com/c.html#mode=edit and paste your code, then hit "visualize execution". You'll see graphically what's happening in memory. EDIT: I'm sorry, the server seems to be down, so I'll try to use Python to show you what the site would have shown: And finally I'll direct you to a question ...


1

You have an infinite loop because of a typo that is writing to the output file until the system kills it because it has used up the disk space allotment. I'll let you figure out the typo, but it's here: for (int n = 0; n < new_padding; k++) After that, you have at least one issue - the header data is not calculated correctly. Again, since you ...


1

Rob was demonstrating the use of a for loop to load values into an array. The i doesn't stand for anything. It is simply a counter for the for loop. It is a standard and common practice to use a single letter, and most commonly, i as a counter in for loops like this. the array ages[] has 4 elements, numbered 0 to 3. In the for loop, i is incremented from 0 ...


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