Ad

Solving Demanding Structure Task

- 1 answer

The structure is given:

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;
};

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.

EXAMPLE:

struct Grade arr[3] = {
      {"Luc Manning", 10, {1, "Physics", 25}, {19, 1, 2020}},
      {"Kaia Feeney", 8, {1, "Math", 25}, {19, 1, 2020}},
      {"Luc Manning", 7, {2, "Linear Algebra", 10}, {11, 1, 2020}}};

EXPLANATION:

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.

OUTPUT would be:

Luc Manning - Physics
Kaia Feeney - Math

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.

Code:

Code is long because of the restriction of not using auxiliary arrays. Most of it consists of a bunch of integers which in normal cases would be one array. The only problem I have in code is that my program removes wrong subjects, and algorithms for the rest of task are correct.

Could you help me modify this to work properly?

#include <stdio.h>
#include <string.h>
#include <sys/types.h>

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) {
  int c0 = 0, c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0, c6 = 0, c7 = 0, c8 = 0,
      c9 = 0;
  int num_digit_day = num_digit(d);
  int num_digit_month = num_digit(m);
  int num_digit_year = num_digit(y);
  if (num_digit_day == 1) {
    if (d == 0)
      c0 = 1;
    if (d == 1)
      c1 = 1;
    if (d == 2)
      c2 = 1;
    if (d == 3)
      c3 = 1;
    if (d == 4)
      c4 = 1;
    if (d == 5)
      c5 = 1;
    if (d == 6)
      c6 = 1;
    if (d == 7)
      c7 = 1;
    if (d == 8)
      c8 = 1;
    if (d == 9)
      c9 = 1;
  }
  if (num_digit_day == 2) {
    int first_digit_day = d / 10, second_digit_day = d % 10;
    if (first_digit_day == 0)
      c0 = 1;
    if (first_digit_day == 1)
      c1 = 1;
    if (first_digit_day == 2)
      c2 = 1;
    if (first_digit_day == 3)
      c3 = 1;
    if (first_digit_day == 4)
      c4 = 1;
    if (first_digit_day == 5)
      c5 = 1;
    if (first_digit_day == 6)
      c6 = 1;
    if (first_digit_day == 7)
      c7 = 1;
    if (first_digit_day == 8)
      c8 = 1;
    if (first_digit_day == 9)
      c9 = 1;
    if (second_digit_day == 0)
      c0 = 1;
    if (second_digit_day == 1)
      c1 = 1;
    if (second_digit_day == 2)
      c2 = 1;
    if (second_digit_day == 3)
      c3 = 1;
    if (second_digit_day == 4)
      c4 = 1;
    if (second_digit_day == 5)
      c5 = 1;
    if (second_digit_day == 6)
      c6 = 1;
    if (second_digit_day == 7)
      c7 = 1;
    if (second_digit_day == 8)
      c8 = 1;
    if (second_digit_day == 9)
      c9 = 1;
  }
  if (num_digit_month == 1) {
    if (m == 0)
      c0 = 0;
    if (m == 1)
      c1 = 1;
    if (m == 2)
      c2 = 1;
    if (m == 3)
      c3 = 1;
    if (m == 4)
      c4 = 1;
    if (m == 5)
      c5 = 1;
    if (m == 6)
      c6 = 1;
    if (m == 7)
      c7 = 1;
    if (m == 8)
      c8 = 1;
    if (m == 9)
      c9 = 1;
  }
  if (num_digit_month == 2) {
    int first_digit_month = m / 10, second_digit_month = m % 10;
    if (first_digit_month == 0)
      c0 = 1;
    if (first_digit_month == 1)
      c1 = 1;
    if (first_digit_month == 2)
      c2 = 1;
    if (first_digit_month == 3)
      c3 = 1;
    if (first_digit_month == 4)
      c4 = 1;
    if (first_digit_month == 5)
      c5 = 1;
    if (first_digit_month == 6)
      c6 = 1;
    if (first_digit_month == 7)
      c7 = 1;
    if (first_digit_month == 8)
      c8 = 1;
    if (first_digit_month == 9)
      c9 = 1;
    if (second_digit_month == 0)
      c0 = 1;
    if (second_digit_month == 1)
      c1 = 1;
    if (second_digit_month == 2)
      c2 = 1;
    if (second_digit_month == 3)
      c3 = 1;
    if (second_digit_month == 4)
      c4 = 1;
    if (second_digit_month == 5)
      c5 = 1;
    if (second_digit_month == 6)
      c6 = 1;
    if (second_digit_month == 7)
      c7 = 1;
    if (second_digit_month == 8)
      c8 = 1;
    if (second_digit_month == 9)
      c9 = 1;
  }
  if (num_digit_year == 4) {
    int first_digit_year = y / 1000, second_digit_year = y / 100 % 10,
        third_digit_year = y / 10 % 10, fourt_digit_year = y % 10;
    if (first_digit_year == 0)
      c0 = 1;
    if (first_digit_year == 1)
      c1 = 1;
    if (first_digit_year == 2)
      c2 = 1;
    if (first_digit_year == 3)
      c3 = 1;
    if (first_digit_year == 4)
      c4 = 1;
    if (first_digit_year == 5)
      c5 = 1;
    if (first_digit_year == 6)
      c6 = 1;
    if (first_digit_year == 7)
      c7 = 1;
    if (first_digit_year == 8)
      c8 = 1;
    if (first_digit_year == 9)
      c9 = 1;
    if (second_digit_year == 0)
      c0 = 1;
    if (second_digit_year == 1)
      c1 = 1;
    if (second_digit_year == 2)
      c2 = 1;
    if (second_digit_year == 3)
      c3 = 1;
    if (second_digit_year == 4)
      c4 = 1;
    if (second_digit_year == 5)
      c5 = 1;
    if (second_digit_year == 6)
      c6 = 1;
    if (second_digit_year == 7)
      c7 = 1;
    if (second_digit_year == 8)
      c8 = 1;
    if (second_digit_year == 9)
      c9 = 1;
    if (third_digit_year == 0)
      c0 = 1;
    if (third_digit_year == 1)
      c1 = 1;
    if (third_digit_year == 2)
      c2 = 1;
    if (third_digit_year == 3)
      c3 = 1;
    if (third_digit_year == 4)
      c4 = 1;
    if (third_digit_year == 5)
      c5 = 1;
    if (third_digit_year == 6)
      c6 = 1;
    if (third_digit_year == 7)
      c7 = 1;
    if (third_digit_year == 8)
      c8 = 1;
    if (third_digit_year == 9)
      c9 = 1;
    if (fourt_digit_year == 0)
      c0 = 1;
    if (fourt_digit_year == 1)
      c1 = 1;
    if (fourt_digit_year == 2)
      c2 = 1;
    if (fourt_digit_year == 3)
      c3 = 1;
    if (fourt_digit_year == 4)
      c4 = 1;
    if (fourt_digit_year == 5)
      c5 = 1;
    if (fourt_digit_year == 6)
      c6 = 1;
    if (fourt_digit_year == 7)
      c7 = 1;
    if (fourt_digit_year == 8)
      c8 = 1;
    if (fourt_digit_year == 9)
      c9 = 1;
  }
  return c0 + c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8 + c9;
}

int digits_in_date(struct Grade arr[], int n) {
  int i, j, k = -1, num_of_success = -1, min = 0;
  double sum_ects;
  int s0 = 0, s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0, s6 = 0, s7 = 0, s8 = 0,
      s9 = 0;
  int a0 = 0, a1 = 0, a2 = 0, a3 = 0, a4 = 0, a5 = 0, a6 = 0, a7 = 0, a8 = 0,
      a9 = 0;

  for (i = 0; i < n; i++) {
    sum_ects = arr[i].subject.ects;
    for (j = i + 1; j < n; j++)
      if (strcmp(arr[j].namesurname, arr[i].namesurname) == 0)
        sum_ects += arr[j].subject.ects;
    if (sum_ects >= 30) {
      num_of_success++;
      if (num_of_success == 0)
        a0 = i;
      if (num_of_success == 1)
        a1 = i;
      if (num_of_success == 2)
        a2 = i;
      if (num_of_success == 3)
        a3 = i;
      if (num_of_success == 4)
        a4 = i;
      if (num_of_success == 5)
        a5 = i;
      if (num_of_success == 6)
        a6 = i;
      if (num_of_success == 7)
        a7 = i;
      if (num_of_success == 8)
        a8 = i;
      if (num_of_success == 9)
        a9 = i;

      k++;
      if (k == 0)
        s0 = check_digits(arr[i].date.day, arr[i].date.month, arr[i].date.year);
      if (k == 1)
        s1 = check_digits(arr[i].date.day, arr[i].date.month, arr[i].date.year);
      if (k == 2)
        s2 = check_digits(arr[i].date.day, arr[i].date.month, arr[i].date.year);
      if (k == 3)
        s3 = check_digits(arr[i].date.day, arr[i].date.month, arr[i].date.year);
      if (k == 4)
        s4 = check_digits(arr[i].date.day, arr[i].date.month, arr[i].date.year);
      if (k == 5)
        s5 = check_digits(arr[i].date.day, arr[i].date.month, arr[i].date.year);
      if (k == 6)
        s6 = check_digits(arr[i].date.day, arr[i].date.month, arr[i].date.year);
      if (k == 7)
        s7 = check_digits(arr[i].date.day, arr[i].date.month, arr[i].date.year);
      if (k == 8)
        s8 = check_digits(arr[i].date.day, arr[i].date.month, arr[i].date.year);
      if (k == 9)
        s9 = check_digits(arr[i].date.day, arr[i].date.month, arr[i].date.year);
    }
  }

  for (i = 0; i < n; i++) {
    if (num_of_success == 0) {
      min = s0;
      if (i == a0) {
        if (check_digits(arr[i].date.day, arr[i].date.month,
                         arr[i].date.year) == min) {
          for (j = i + 1; j < n; j++)
            arr[j - 1] = arr[j];
          n--;
          i--;
        }
      }
    }
    if (num_of_success == 1) {
      min = s0;
      if (s1 < min)
        min = s1;
      if (i == a0 || i == a1) {
        if (check_digits(arr[i].date.day, arr[i].date.month,
                         arr[i].date.year) == min) {
          for (j = i + 1; j < n; j++)
            arr[j - 1] = arr[j];
          n--;
          i--;
        }
      }
    }
    if (num_of_success == 2) {
      int min = 2000000;
      if (s0 < s1 && s0 < s2)
        min = s0;
      if (s1 < s0 && s1 < s2)
        min = s1;
      if (s2 < s0 && s2 < s1)
        min = s2;
      if (i == a0 || i == a1 || i == a2) {
        if (check_digits(arr[i].date.day, arr[i].date.month,
                         arr[i].date.year) == min) {
          for (j = i + 1; j < n; j++)
            arr[j - 1] = arr[j];
          n--;
          i--;
        }
      }
    }
    if (num_of_success == 3) {
      int min = 2000000;
      if (s0 < s1 && s0 < s2 && s0 < s3)
        min = s0;
      if (s1 < s0 && s1 < s2 && s1 < s3)
        min = s1;
      if (s2 < s0 && s2 < s1 && s2 < s3)
        min = s2;
      if (s3 < s0 && s3 < s1 && s3 < s2)
        min = s3;
      if (i == a0 || i == a1 || i == a2 || i == a3) {
        if (check_digits(arr[i].date.day, arr[i].date.month,
                         arr[i].date.year) == min) {
          for (j = i + 1; j < n; j++)
            arr[j - 1] = arr[j];
          n--;
          i--;
        }
      }
    }
    if (num_of_success == 4) {
      int min = 2000000;
      if (s0 < s1 && s0 < s2 && s0 < s3 && s0 < s4)
        min = s0;
      if (s1 < s0 && s1 < s2 && s1 < s3 && s1 < s4)
        min = s1;
      if (s2 < s0 && s2 < s1 && s2 < s3 && s2 < s4)
        min = s2;
      if (s3 < s0 && s3 < s1 && s3 < s2 && s3 < s4)
        min = s3;
      if (s4 < s0 && s4 < s1 && s4 < s2 && s4 < s3)
        min = s4;
      if (i == a0 || i == a1 || i == a2 || i == a3 || i == a4) {
        if (check_digits(arr[i].date.day, arr[i].date.month,
                         arr[i].date.year) == min) {
          for (j = i + 1; j < n; j++)
            arr[j - 1] = arr[j];
          n--;
          i--;
        }
      }
    }

    if (num_of_success == 5) {
      int min = 2000000;
      if (s0 < s1 && s0 < s2 && s0 < s3 && s0 < s4 && s0 < s5)
        min = s0;
      if (s1 < s0 && s1 < s2 && s1 < s3 && s1 < s4 && s1 < s5)
        min = s1;
      if (s2 < s0 && s2 < s1 && s2 < s3 && s2 < s4 && s2 < s5)
        min = s2;
      if (s3 < s0 && s3 < s1 && s3 < s2 && s3 < s4 && s3 < s5)
        min = s3;
      if (s4 < s0 && s4 < s1 && s4 < s2 && s4 < s3 && s4 < s5)
        min = s4;
      if (s5 < s0 && s5 < s1 && s5 < s2 && s5 < s3 && s5 < s4)
        min = s5;
      if (i == a0 || i == a1 || i == a2 || i == a3 || i == a4 || i == a5) {
        if (check_digits(arr[i].date.day, arr[i].date.month,
                         arr[i].date.year) == min) {
          for (j = i + 1; j < n; j++)
            arr[j - 1] = arr[j];
          n--;
          i--;
        }
      }
    }
  }

  return n;
}

int main() {
  struct Grade arr[3] = {
      {"Luc Manning", 10, {1, "Physics", 25}, {19, 1, 2020}},
      {"Kaia Feeney", 8, {1, "Math", 25}, {19, 1, 2020}},
      {"Luc Manning", 7, {2, "Linear Algebra", 10}, {11, 1, 2020}}};
  int vel = digits_in_date(arr, 3);
  int i;
  for (i = 0; i < vel; i++)
    printf("%s - %s\n", arr[i].namesurname, arr[i].subject.name_of_subject);

  return 0;
}
Ad

Answer

Here is my count digits function

convert to a string and then create a distribution 'histogram' in count[]

Then count the number of non 0 counts

int count_digits(int d, int m, int y)
{
    char dateStr[10];
    sprintf(dateStr,"%02d%02d%04d", d, m, y);
    int count[10] = { 0 };
    for (int i = 0; i < strlen(dateStr); i++) {
        char ch = dateStr[i];
        count[ch - '0']++;
    }
    int tally = 0;
    for (int i = 0; i < 10; i++)
    {
        if (count[i] > 0)
            tally++;
    }
    return tally;
}

saving about 380 lines

Ad
source: stackoverflow.com
Ad