In the continued lecture of :

  1. At about 17:00 he writes the for loop. What does i stand for? I don't think it is age, but it can't be the number of students? What does it mean?

  2. What is ages[i]? Why does he assign it to GetInt()? What does it represent? (at about 20:00)

    1. What is the difference between i + 1 and ages[i] + 1? (further into video)

I really need to understand this. Is it necessary for the problem set 2?

also do I need to rewatch the 2016 lectures?

Thanks in advance!


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 to 3, so on each pass through the for loop, the i in ages[i] will be replaced by it's value, so on the first pass, i = 0 and ages[i] is ages[0]. On each pass, i is incremented, so the loop will go from ages[0] to ages[1] to ages[2] and finally to ages[3].

Rob is not assigning ages[i] to GetInt(). He is doing just the opposite. GetInt() will get an integer from the user and will assign it to ages[i]. This will put an integer into each element of the array, one element on each pass of the for loop, as explained above.

ages[i] + 1 vs. i+1 : Let's say that i = 2. Then i+1 = 3. However, ages[i]+1 will add 1 to the value stored at ages[i], and since i=2, it will add 1 to ages[2]. If ages[2] = 10 before this, then it would be 10+2 = 12 after performing the addition.

These are some of the most fundamental tools in programming. You absolutely need to understand them for every pset and for programming in general. If you don't understand these, or any other material in the class, then you should start by reviewing the lectures, the walkthroughs and the section videos until you do. Also, searching for more explanation on google or youtube will help you.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance.

  • One question: why do you add 2 to 10 (ages[2])? You said you added 1? – sophiemath Jan 3 '16 at 23:17
  • Also, 2 more question: – sophiemath Jan 3 '16 at 23:17
  • Ugh, enter key! Anyways, 1st question... up vote 0 down vote favorite I have watched two videos about Command-line Arguments: Lecture Week 2 cont. and Christopher Bartholomew's video (CS50) on command-line arguments. In the lecture, Mr. Bowden says the number in argc is counted this way: argv[0] is command, argv[1] is 1st argument, and so on. But in the other video, argv[0] is command, argv[1] is the 1st argument, then ... argv[n] is null. I think the second video is correct because when Mr. Bowden put in hello-3 as his first argument, he got hello, (null) since (null) is the second argument. – sophiemath Jan 3 '16 at 23:19
  • 2nd question: I did update50 and got this: Package configuration, Configuring grub-pc, and so on. Is it bad? What is it saying? – sophiemath Jan 3 '16 at 23:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .