0
#include <stdio.h>

#define NUMBER 20                                         

#define OPTION 6 

void main ()
{

    int optionList[NUMBER]= 
        {4,4,5,2,1,3,1,0,4,3,3,1,2,5,4,2,3,4,3,1}; 

    int count[OPTION] = { 0 } ;

    for (int i = 0; i <= NUMBER-1; i++) 
        ++count[optionList[i]]; 
} 

I don't understand ++count[optionList[i]]. Is it a loop increment for both array 'count 'and 'optionList'? How does it work?

4
  • 1
    Broken down: int x = optionList[i], and then ++count[x] Essentially optionList here is used as a lookup table that maps values 0...19 to 0...5 Commented Dec 24, 2020 at 5:21
  • OT: Instead of using the loop condition i <= NUMBER-1 you can use i < NUMBER. The latter is more common, so more people will recognize it and understand it immediately, as well as being less to write. Commented Dec 24, 2020 at 5:24
  • 1
    This statement ++count[optionList[i]]; increments values in the count array. Its not at all a "loop increment" -- i++ is a loop increment. Also aside: main is NOT void -- its meant to return an int Commented Dec 24, 2020 at 5:30
  • regarding: void main () regardless of what non-conforming compilers may allow, there are only two valid signatures for main() They are: int main( void ) and int main( int argc, char *argv[] ) Commented Dec 25, 2020 at 3:24

2 Answers 2

2

optionList - is an array of 20 elements
count - is an array of 6 elements

the for loop iterates over i = 0 to inclusively i = 20 - 1 = 19.
In each iteration step it executes ++count[optionList[i]];

Let's calculate ++count[optionList[i]]; for i=3 step by step: First we look at optionList[i], so we take the i-th element from optionList. The 3rd element from optionList (beginning to count from 0) is 2. So ++count[optionList[i]] evaluates to ++count[2]. ++value means incrementing the value behind value by one. ++count[2] means that the 2nd element from count is increased by one. So after executing ++count[optionList[i]]; the array count now looks like this: {0, 0, 1, 0, 0, 0} (if it looked like this {0, 0, 0, 0, 0, 0} before)

Conclusion

Look at optionList as an array of indices. The loop increments count at every index from optionList.

Sign up to request clarification or add additional context in comments.

Comments

1

It you think about this from the "inside out" way, the optionList[i] gets evaluated first. This returns the value at optionList[i]

ex) If i = 0, optionList[i] = 4

Then the count[{value}] gets evaluated.

ex) If i = 0, optionList[i] = 4. count[optionList[i]] is the same as count[4] in this case.

So the fourth spot in the count array then gets incremented by the prefix ++. The loop essentially goes through and increments the value of the count array at each location specified by optionList[i].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.