• + 3 comments

    Since oshie is inexperienced therefore he forgot to allocate memory to the 2 arrays he declared. Hence we need to allocate memories manually to the array. My method is "for array1" "We need to increase the number of book by 1 each time a new input is given therefore increase the total_number_of_books[x] (x is the shelves location)"

    "for array 2" "Since the array "total_number_of_pages" store the number of pages of a book at [shelves(row)][book(column)] location, each time we add a new book we need to extend the memory allocated to this array inorder to add the number of pages of the new book. since the shelves are fixed and only column needs to be extended therefore i used the realloc on columns only and everything worked fine" here is my solution.

    #include <stdio.h>
    #include <stdlib.h>
    
    /*
     * This stores the total number of books in each shelf.
     */
    int* total_number_of_books;
    
    /*
     * This stores the total number of pages in each book of each shelf.
     * The rows represent the shelves and the columns represent the books.
     */
    int** total_number_of_pages;
    
    int main()
    {
        int total_number_of_shelves;
        scanf("%d", &total_number_of_shelves);
        
        int total_number_of_queries;
        scanf("%d", &total_number_of_queries);
        total_number_of_books = (int*)calloc(total_number_of_shelves,sizeof(int));
        total_number_of_pages = (int**)calloc(total_number_of_shelves,sizeof(int*));
        for(int i=0;i<total_number_of_shelves;i++){
            total_number_of_pages[i]= (int*)calloc(total_number_of_books[i],sizeof(int));    
        }
    
        while (total_number_of_queries--) {
            int type_of_query;
            scanf("%d", &type_of_query);
            
            if (type_of_query == 1) {
                /*
                 * Process the query of first type here.
                 */
                int x, y;
                scanf("%d %d", &x, &y);
                total_number_of_books[x] = total_number_of_books[x]+1;
                total_number_of_pages[x]= (int*)realloc(total_number_of_pages[x],total_number_of_books[x]*sizeof(int));    
                total_number_of_pages[x][total_number_of_books[x]-1] = y;
            } else if (type_of_query == 2) {
                int x, y;
                scanf("%d %d", &x, &y);
                printf("%d\n", *(*(total_number_of_pages + x) + y));
            } else {
                int x;
                scanf("%d", &x);
                printf("%d\n", *(total_number_of_books + x));
            }
        }
    
        if (total_number_of_books) {
            free(total_number_of_books);
        }
        
        for (int i = 0; i < total_number_of_shelves; i++) {
            if (*(total_number_of_pages + i)) {
                free(*(total_number_of_pages + i));
            }
        }
        
        if (total_number_of_pages) {
            free(total_number_of_pages);
        }
        
        return 0;
    }