0

I just started learning dynamic arrays and have no clue how to solve this problem. I am trying to add an element to one of three arrays depending on their associative char. I am able to pass low level test cases but there is a run time error in 3/6 cases.

int main(void) 
{
    int count_of_jerseys,count_of_players;
    int satisfied = 0;

    int small_count = 0, *small_counter = &small_count;
    int *small = (int *)malloc(small_count * sizeof(int));

    int medium_count = 0, *medium_counter = &medium_count;
    int *medium = (int *)malloc(medium_count * sizeof(int));

    int large_count = 0, *large_counter = &large_count;
    int *large = (int *)malloc(large_count * sizeof(int));

    scanf("%d\n%d", &count_of_jerseys, &count_of_players);

    for (int x = 0; x < count_of_jerseys; ++x)
    {
        char b; scanf("\n%c", &b);
        if (b == 'S'){append((x+1), small, small_counter);}
        if (b == 'M'){append((x+1), medium, medium_counter);}
        if (b == 'L'){append((x+1), large, large_counter);}
    }

    for (int i = 0; i < count_of_players; ++i)
    {
        char temp_char;
        int temp_var;
        scanf("\n%c %d", &temp_char, &temp_var);
        if (temp_char == 'S')
        {
            if (array_searcher(small, small_counter, temp_var) == true){++satisfied;}
            else if (array_searcher(medium, medium_counter, temp_var) == true){++satisfied;}
            else if (array_searcher(large, large_counter, temp_var) == true){++satisfied;}
        }

        else if (temp_char == 'M')
        {
            if (array_searcher(medium, medium_counter, temp_var) == true){++satisfied;}
            else if (array_searcher(large, large_counter, temp_var) == true){++satisfied;}
        }

        else if (temp_char == 'L')
        {
            if (array_searcher(large, large_counter, temp_var) == true){++satisfied;}
        }
    }
    printf("\n%d", satisfied);
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I don't think this is the full code. In general, if you `malloc`, you should call `free` exactly only once on that pointer and no more. Need more context to help you. – Unmanned Player Jan 12 '21 at 20:50
  • You're allocating zero-length arrays. How do you expect to append elements to them? If `append()` reallocates the arrays, it's not updating the pointers in `main()`. – Barmar Jan 12 '21 at 20:54

0 Answers0