C - determine number of number of array elements in each group
<p>I have an array of strings that I have sorted, e.g. {case, case, case, control, control};</p>

<p>I've iterated over the array, incrementing any time strcmp between the ith element and ith+1 element != 0 to determine the numbers of different groups in the array, in this case 2. However, I'm wondering if anyone has any suggestions about how to determine the number of elements in each group (if you don't know beforehand how many different groups there will be).</p>

<p>Thanks in advance!</p>

<p>Edit, as per the advice of @Riley, I've incremented an array class_size, with the class number as the index. It actually worked quite well when I included this incrementation within the first for loop as a second if condition (adding 1 to each index when the loop completed). However, I couldn't figure out how I would determine the size of the class_size array before I knew what n_classes would be, so I made it a separate loop, but it doesn't quite seem to be working. It seems like the problem is that I can't initialize a variable-size object (class_size[n_classes]), but I'm not sure how to get around that if I don't know beforehand how many classes there will be:</p>

<pre><code>int n_classes = 1;
for(int i = 0; i &lt; total_columns - 1; i++)
{
    if(strcasecmp(data[i].c_name, data[i+1].c_name) != 0)
    {
        n_classes++;
    }
}

int n_classtemp = 0;
int class_size[n_classes];
for(int i = 0; i &lt; total_columns - 1; i++)
{
    if(strcasecmp(data[i].c_name, data[i+1].c_name) != 0)
    {
        n_classtemp++;
    }
    if(strcasecmp(data[i].c_name, data[i+1].c_name) == 0)
    {
        class_size[n_classtemp]++;
    }
}

for (int i = 0; i &lt; n_classes; i++)
{
    class_size[i] = class_size[i] + 1;
}
</code></pre>

<p>Edit:
I was able to fix this by manually initializing the array:</p>

<pre><code>int n_classes = 1;
for(int i = 0; i &lt; total_columns - 1; i++)
{
    if(strcasecmp(data[i].c_name, data[i+1].c_name) != 0)
    {
        n_classes++;
    }
}

int n_classtemp = 0;
int class_size[n_classes];
memset(class_size, 0, n_classes*sizeof(int));
for(int i = 0; i &lt; total_columns - 1; i++)
{
    if(strcasecmp(data[i].c_name, data[i+1].c_name) != 0)
    {
        n_classtemp++;
    }
    if(strcasecmp(data[i].c_name, data[i+1].c_name) == 0)
    {
        class_size[n_classtemp]++;
    }
}

for (int i = 0; i &lt; n_classes; i++)
{
    class_size[i] = class_size[i] + 1;
}
</code></pre>