title: Solving demanding structure task
body: <p>The structure is given:</p>
  <pre><code>struct Date {
    int day, month, year;
  };
  struct Subject {
    int id;
    char name_of_subject[100];
    double ects;
  };
  struct Grade {
    char namesurname[100];
    int grade_value;
    struct Subject subject;
    struct Date date;
  };
  </code></pre>
  <p>It is necessary to write a function digits_in_date that receives struct array of grades. For all students who have a total of 30 or more ECTS credits in the subjects they have passed (they have a grade of 6 or higher), it is necessary to exclude from the array (without changing the order!) those grades whose date contains the minimum number of different digits. Eg the date 20 February 2020 contains only two different digits (2 and 0). If they have more than one grade whose dates have the same (minimum) number of digits, all such grades should be dropped from the array. You can find out that the grades belong to the same student based on the name and surname (assume that if two students have the same name and surname, then it is the same student). Assume that the data is valid (valid dates, valid grades, ects credits are not negative, etc.) Auxiliary arrays are not allowed. Only allowed auxiliary array is when making histogram.</p>
  <p>EXAMPLE:</p>
  <pre><code>struct Grade arr[3] = {
        {&quot;Luc Manning&quot;, 10, {1, &quot;Physics&quot;, 25}, {19, 1, 2020}},
        {&quot;Kaia Feeney&quot;, 8, {1, &quot;Math&quot;, 25}, {19, 1, 2020}},
        {&quot;Luc Manning&quot;, 7, {2, &quot;Linear Algebra&quot;, 10}, {11, 1, 2020}}};
  </code></pre>
  <p>EXPLANATION:</p>
  <p>Sum of Luc Manning ECTS points is 35, so we check how many different digits are there in his subjects dates. Physics has 4, and Linear Algebra has 3 different digits in dates, so Linear Algebra should be removed.
  Sum of Kaia Feeney ECTS points is 25, so we don't check anything.</p>
  <p>OUTPUT would be:</p>
  <pre><code>Luc Manning - Physics
  Kaia Feeney - Math
  </code></pre>
  <p>INSTRUCTION:
  Create an auxiliary function that determines the number of different digits in the date using a histogram. Then the auxiliary function that calculates the number of ECTS credits and finally the ejection function. This will make your task drastically easier.</p>
  <p>The only problem I have in code is that my program removes wrong subjects, and algorithms for the rest of task are correct.
  Half of the code are just autotests :) The code is not so long.</p>
  <p>EDIT:
  after @pm100 made a function that would save 400 lines of code, and with my modification of original code, now the code is:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  
  struct Date {
    int day, month, year;
  };
  struct Subject {
    int id;
    char name_of_subject[100];
    double ects;
  };
  struct Grade {
    char namesurname[100];
    int grade_value;
    struct Subject subject;
    struct Date date;
  };
  
  int num_digit(int n) {
    int num = 0;
    while (n != 0) {
      num++;
      n /= 10;
    }
    return num;
  }
  
  int check_digits(int d, int m, int y) {
    char dateStr[10];
    sprintf(dateStr, &quot;%02d%02d%04d&quot;, d, m, y);
    int count[10] = {0};
    for (int i = 0; i &lt; strlen(dateStr); i++) {
      char ch = dateStr[i];
      count[ch - '0']++;
    }
    int num = 0;
    for (int i = 0; i &lt; 10; i++) {
      if (count[i] &gt; 0)
        num++;
    }
    return num;
  }
  
  double credits(struct Grade arr[], int n, int i) {
    int j;
    double sum = arr[i].subject.ects;
    for (j = i + 1; j &lt; n; j++)
      if (strcmp(arr[j].namesurname, arr[i].namesurname) == 0)
        sum += arr[j].subject.ects;
    return sum;
  }
  
  int find_min(struct Grade arr[], int n, int i) {
    int j,
        min = check_digits(arr[i].date.day, arr[i].date.month, arr[i].date.year);
    for (j = i + 1; j &lt; n; j++)
      if (strcmp(arr[j].namesurname, arr[i].namesurname) == 0)
        if (check_digits(arr[j].date.day, arr[j].date.month, arr[j].date.year) &lt;
            min)
          min = j;
    return min;
  }
  
  int digits_in_date(struct Grade arr[], int n) {
    int i, j, k, min;
    double sum_ects;
    for (i = 0; i &lt; n; i++) {
      sum_ects = credits(arr, n, i);
      if (sum_ects &gt;= 30) {
        min = find_min(arr, n, i);
        for (j = 0; j &lt; n; j++)
          if (strcmp(arr[j].subject.name_of_subject,
                     arr[min].subject.name_of_subject) == 0) {
            for (k = j + 1; k &lt; n; k++)
              arr[k - 1] = arr[k];
            j--;
            n--;
          }
      }
    }
    return n;
  }
  
  int main() {
    int i, s;
    printf(&quot;\nAT1: Trivial case\n&quot;);
    struct Grade arr1[3] = {
            {&quot;Luc Manning&quot;, 10, {1, &quot;Physics&quot;, 25}, {19, 1, 2020}},
            {&quot;Kaia Feeney&quot;, 8, {1, &quot;Math&quot;, 25}, {19, 1, 2020}},
            {&quot;Luc Manning&quot;, 7, {2, &quot;Linear Algebra&quot;, 10}, {11, 1, 2020}}};
    s = digits_in_date(arr1, 3);
    for (i = 0; i &lt; s; i++)
      printf(&quot;%s - %s\n&quot;, arr1[i].namesurname, arr1[i].subject.name_of_subject);
  
    printf(&quot;\nAT2: We must not observe the same student more than once\n&quot;);
    struct Grade arr2[4] = {
        {&quot;Mujo Suljic&quot;, 10, {1, &quot;Uvod u programiranje&quot;, 25}, {19, 1, 2020}},
        {&quot;Fata Fatic&quot;, 8, {1, &quot;Uvod u programiranje&quot;, 25}, {19, 1, 2020}},
        {&quot;Mujo Suljic&quot;, 7, {2, &quot;Inzenjerska fizika&quot;, 10}, {11, 1, 2020}},
        {&quot;Mujo Suljic&quot;, 10, {3, &quot;Inzenjerska matematika&quot;, 6}, {1, 9, 2020}}};
    s = digits_in_date(arr2, 4);
    for (i = 0; i &lt; s; i++)
      printf(&quot;%s - %s\n&quot;, arr2[i].namesurname, arr2[i].subject.name_of_subject);
      printf(&quot;CORRECT:\nMujo Suljic - Uvod u programiranje\nFata Fatic - Uvod u programiranje\nMujo Suljic - Inzenjerska matematika\n&quot;);
  
    printf(&quot;\nAT3: Multiple successive ejections\n&quot;);
    struct Grade arr3[6] = {
        {&quot;Mujo Suljic&quot;, 10, {1, &quot;Uvod u programiranje&quot;, 25}, {2, 2, 2020}},
        {&quot;Mujo Suljic&quot;, 6, {5, &quot;Osnove elektrotehnike&quot;, 25}, {20, 2, 2022}},
        {&quot;Fata Fatic&quot;, 8, {1, &quot;Uvod u programiranje&quot;, 25}, {19, 1, 2020}},
        {&quot;Mujo Suljic&quot;, 7, {2, &quot;Inzenjerska fizika&quot;, 10}, {11, 1, 2020}},
        {&quot;Mujo Suljic&quot;, 10, {3, &quot;Inzenjerska matematika&quot;, 6}, {20, 1, 2020}},
        {&quot;Fata Fatic&quot;, 8, {4, &quot;Inzenjerska matematika&quot;, 25}, {9, 9, 2021}}};
    s = digits_in_date(arr3, 6);
    for (i = 0; i &lt; s; i++)
      printf(&quot;%s - %s %g\n&quot;, arr3[i].namesurname, arr3[i].subject.name_of_subject,
             arr3[i].subject.ects);
      printf(&quot;CORRECT:\nMujo Suljic - Inzenjerska fizika 10\nMujo Suljic - Inzenjerska matematika 6\n&quot;);
  
    printf(&quot;\nAT4: Array with one element\n&quot;);
    struct Grade arr4[1] = {
        {&quot;John Smith&quot;, 10, {1, &quot;Subject 1&quot;, 29}, {19, 11, 1991}}};
    s = digits_in_date(arr4, 1);
    printf(&quot;%d &quot;, s);
    arr4[0].subject.ects = 31;
    s = digits_in_date(arr4, 1);
    printf(&quot;%d\n&quot;, s);
   printf(&quot;CORRECT:\n1 0\n&quot;);
  
    printf(
        &quot;\nAT5: We don't eject anything out, no one has 30 credits in total\n&quot;);
    struct Grade arr5[6] = {
        {&quot;Mujo Suljic&quot;, 10, {1, &quot;Uvod u programiranje&quot;, 25}, {20, 2, 2020}},
        {&quot;Pero Peric&quot;, 6, {5, &quot;Osnove elektrotehnike&quot;, 25}, {20, 2, 2020}},
        {&quot;Fata Fatic&quot;, 8, {1, &quot;Uvod u programiranje&quot;, 25}, {19, 1, 2020}},
        {&quot;Mujo Suljic&quot;, 7, {2, &quot;Inzenjerska fizika&quot;, 1}, {11, 1, 2020}},
        {&quot;Mujo Suljic&quot;, 10, {3, &quot;Inzenjerska matematika&quot;, 2}, {20, 1, 2020}},
        {&quot;Fata Fatic&quot;, 8, {4, &quot;Inzenjerska matematika&quot;, 2}, {19, 1, 2020}}};
    s = digits_in_date(arr5, 6);
    printf(&quot;s: %d\n&quot;, s);
    printf(&quot;AT5 again: Inzenjerska fizika now carries 4 ECTS credits so Mujo has &quot;
           &quot;the required number of credits\n&quot;);
    arr5[3].subject.ects = 4;
    s = digits_in_date(arr5, 6);
    for (i = 0; i &lt; s; i++)
      printf(&quot;%s - %s %g\n&quot;, arr5[i].namesurname, arr5[i].subject.name_of_subject,
             arr5[i].subject.ects);
             printf(&quot;CORRECT:\ns: 6\nAT5 again: Inzenjerska fizika now carries 4 ECTS credits so Mujo has &quot;
           &quot;the required number of credits\nPero Peric - Osnove elektrotehnike 25\nFata Fatic - Uvod u programiranje 25\nMujo Suljic - Inzenjerska fizika 4\n&quot;
  &quot;Mujo Suljic - Inzenjerska matematika 2\nFata Fatic - Inzenjerska matematika 2\n&quot;);
  
    //
    printf(&quot;\nAT6: Mujo Suljic did not pass Injzenjerska fizika, he does not &quot;
           &quot;have 30 ECTS credits\n&quot;);
    struct Grade arr6[6] = {
        {&quot;Mujo Suljic&quot;, 10, {1, &quot;Uvod u programiranje&quot;, 25}, {20, 2, 2020}},
        {&quot;Pero Peric&quot;, 6, {5, &quot;Osnove elektrotehnike&quot;, 25}, {20, 2, 2020}},
        {&quot;Fata Fatic&quot;, 8, {1, &quot;Uvod u programiranje&quot;, 25}, {19, 1, 2020}},
        {&quot;Mujo Suljic&quot;, 5, {2, &quot;Inzenjerska fizika&quot;, 10}, {11, 1, 2020}},
        {&quot;Mujo Suljic&quot;, 10, {3, &quot;Inzenjerska matematika&quot;, 4}, {20, 1, 2020}},
        {&quot;Fata Fatic&quot;, 8, {4, &quot;Inzenjerska matematika&quot;, 25}, {19, 1, 2020}}};
    s = digits_in_date(arr6, 6);
    for (i = 0; i &lt; s; i++)
      printf(&quot;%s - %s %g\n&quot;, arr6[i].namesurname, arr6[i].subject.name_of_subject,
             arr6[i].subject.ects);
              printf(&quot;CORRECT:\nMujo Suljic - Uvod u programiranje 25\nPero Peric - Osnove elektrotehnike 25\n&quot;
  &quot;Mujo Suljic - Inzenjerska fizika 10\nMujo Suljic - Inzenjerska matematika 4\n&quot;);
    return 0;
  }
  </code></pre>
  <p>I only get correct output for AutoTest1 - AT1 (which is the same as example), and the rest AT are wrong.
  Could you help me modify this to work properly?</p>