Hot answers tagged

3 votes
Accepted

PSET 3 Runoff Error: variable length array declaration not allowed at file scope

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 ...
user avatar
  • 930
2 votes

Selection Sort: Holes in Logic

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 ...
user avatar
  • 5,172
2 votes

Arrays and their sizes in C

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 ...
user avatar
  • 20.7k
2 votes
Accepted

Lecture 2 Arrays

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 ...
user avatar
1 vote
Accepted

Problem understanding array with Caesar project

argv[1] is a string and each char in the string is index 0, 1, 2....etc. The correct notation is argv[1][0], argv[1][1], argv[1][2]..... etc.
user avatar
1 vote

Passing arrays into function in tideman

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 ...
user avatar
1 vote
Accepted

runtime error in sandbox for array examples from week 2 lecture

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]....
user avatar
1 vote
Accepted

I'm getting an for error expected expression. What exactly I'm I doing wrong?

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 (...
user avatar
  • 63.4k
1 vote
Accepted

Trouble modifying elements of an array of candidates in Plurality

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 ...
user avatar
  • 20.7k
1 vote

[pset2][Vinegere] Code not enciphering text after certain letters, please help!

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 ...
user avatar
1 vote
Accepted

Why string variable has no square bracket?

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 ...
user avatar
1 vote

Pset1: Error when trying to assign single digits of int variable to array using for loop

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 ...
user avatar
  • 20.7k
1 vote
Accepted

Pset2: Copying string contents

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 ...
user avatar
  • 20.7k
1 vote
Accepted

Error handling while running credit.c for pset1

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. ...
user avatar
  • 1,139
1 vote
Accepted

Finding & printing character after space in string in C

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 ...
user avatar
1 vote
Accepted

Pset7 - Portfolio: $positions not filling

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() ...
user avatar
1 vote
Accepted

What happens when you assign a character string to a char* instead of writing element by element?

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. ...
user avatar
1 vote
Accepted

PSET 4 Resize: Pointers, arrays and file IO

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 ...
user avatar
  • 63.4k
1 vote

Clarification regarding for loops and arrays

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 ...
user avatar
  • 63.4k

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