Dynamic Array in C | Best Explanation(With code) | HackerRank

Statement

Snow Howler is the librarian at the central library of the city of HuskyLand. He must handle requests which come in the following forms:

  1. x y : Insert a book with  pages y at the end of the xth shelf.
  2. x y : Print the number of pages in the yth book on the xth shelf.
  3. x : Print the number of books on the xth shelf.

Snow Howler has got an assistant, Oshie, provided by the Department of Education. Although inexperienced, Oshie can handle all of the queries of types 2 and 3.

Help Snow Howler deal with all the queries of type 1.

Oshie has used two arrays:

int* total_number_of_books;
/*
 * This stores the total number of books on each shelf.
 */

int** total_number_of_pages;
/*
 * This stores the total number of pages in each book of each shelf.
 * The rows represent the shelves and the columns represent the books.
 */



Input Format


The first line contains an integer total_number_of_shelves, the number of shelves in the library.
The second line contains an integer total_number_of_queries, the number of requests.
Each of the following  total_number_of_queries lines contains a request in one of the three specified formats.


Output format


Write the logic for the requests of type 1. The logic for requests of types 2 and 3 are provided.

Note that we only need to code the logic of query type 1. 
Query of type 1 is => x y : Insert a book with  pages y at the end of the xth shelf.


Explanation


First of all you need to understand about all the variables which are being used here. In the query of type 1 we are dealing with 4 different entities:
  1. x : this is an integer type variable which will provide us with the shelf number at the end of which we need to insert a book.
  2. y : this is also an integer type variable which will tell us about the number of pages in the book which we are going to insert at the end of the xth shelf.
  3. total_number_of_books : this is an array of integer type (basically a pointer) which stores about the total number of books in any shelf.
  4. total_number_of_pages : this is a 2D array of integer type (basically a pointer to pointer) which stores the number of pages in particular book of any particular shelf.
Have a look on the image below for better understanding.



Consider the above image; 
  • There are total 3 shelves i.e. 0th, 1st and 2nd shelf.
  • The 0th shelf has total 3 books, 1st shelf has 2 books and 2nd shelf has 3 books. That's why the array total_number_of_books = [3,2,3].
  • The 0th shelf has books with 56 pages, 45 pages, 86 pages. First shelf has books with 48 pages and 54 pages. 2nd shelf has books with 48 pages, 54 pages, 68 pages. That's why the 2D array total_number_of_pages = [ [ 56, 45, 86 ], [ 48, 57 ], [ 48, 54, 68 ] ].


Performing Query [Step by Step Guide]


Our work is to Insert a book with  pages y at the end of the xth shelf.

Have a look on below image, suppose that we want to add a book with 98 pages at the end of 1st shelf.



Step 1) Increase the number of books by one at the 1st index in the array total_number_of_books.  As after inserting a book there will be 3 books in the 1st shelf so we'll have to increase the value in the total_number_of_books at 1st index.

*(total_number_of_books+1)+=1;




Step 2) Watch carefully that memory for all the shelves are pre-declared and that's why they are fixed and we'll have to realloc the memory for shelf 1 in order to insert a book in the end. So to increase the space in the shelf 1st we'll need to use realloc() on the array total_number_of_pages so that we can increase the size of array at the index 1 in the total_number_of_pages.

*(total_number_of_pages+1)=realloc(*(total_number_of_pages+1), *(total_number_of_books+1)*sizeof(int));



Step 3) Now we just need to add the book in this vacant place of total_number_of_pages.

*(*(total_number_of_pages+1)+*(total_number_of_books+1)-1)=y;

Note that we are subtracting 1 fron the total_number of_books in 1st shelf because array in c works on 0 indexing system which means that book at 3rd position will be at 2nd index.





Combined Code:



#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*)malloc(sizeof(int)*total_number_of_shelves);
    
    total_number_of_pages=(int**)malloc(sizeof(int*)*total_number_of_shelves);
    
    for(int i=0; i<total_number_of_shelves; i++){
        total_number_of_books[i]=0;
        total_number_of_pages[i]=(int*)malloc(sizeof(int));
    }
    
    while (total_number_of_queries--) {
        int type_of_query;
        scanf("%d", &type_of_query);
        
        /* We were asked to code this part only*/
        /* Solution strats from below */
        
        if (type_of_query == 1) {
            int x, y;
            scanf("%d %d", &x, &y);
            *(total_number_of_books+x)+=1;
            *(total_number_of_pages+x)=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;
            
        /*S olution ends here */
        /* Other queries were alredy coded by hackerrank */
        /* Thank you :) */

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



Our All Posts Are Protected By DMCA. So Don't Try To Copy Our Posts and any media used in this post. You are not allowed to use images in this post for your work or profit. Such acts are strictly prohibited.

Labels : #array in c ,#hackerrank ,#hackerrank c ,

Post a Comment