Advertisement
Guest User

Untitled

a guest
Aug 18th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.73 KB | None | 0 0
  1. int main()
  2. {
  3.     int i;
  4.     int current_total_book;                 /* holds the size of total book */
  5.    
  6.     int total_number_of_shelves;
  7.     scanf("%d", &total_number_of_shelves);
  8.    
  9.     int total_number_of_queries;
  10.     scanf("%d", &total_number_of_queries);
  11.    
  12.     /* initial size of total book */
  13.     current_total_book = 110;
  14.     /* initializing the dynamic arrays */
  15.     total_number_of_pages = malloc( total_number_of_shelves * sizeof(int*) );
  16.     for( i = 0; i < total_number_of_shelves; i++ ){
  17.         total_number_of_pages[i] = malloc( current_total_book * sizeof(int) );
  18.     }
  19.     total_number_of_books = malloc( total_number_of_shelves * sizeof(int) );
  20.  
  21.     while (total_number_of_queries--) {
  22.         int type_of_query;
  23.         scanf("%d", &type_of_query);
  24.        
  25.         if (type_of_query == 1) {
  26.             /*
  27.              * Process the query of first type here.
  28.              */
  29.             int x, y;
  30.             scanf("%d %d", &x, &y);
  31.             int nb;
  32.             total_number_of_books[x]++;
  33.             nb = total_number_of_books[x];
  34.             /* if the total_number is more than or equals current_total,
  35.              * resize it by DOUBLE of last current_total using realloc()
  36.              * (note that the data is still existing after resizing)
  37.              */
  38.             if( total_number_of_books[x] >= current_total_book ){
  39.                 current_total_book *= 2;
  40.                 total_number_of_pages[x] = realloc( total_number_of_pages[x], current_total_book * sizeof( int ) );
  41.             }
  42.             /* put the number of pages y into a shelf x,
  43.              * while also increasing the total number of books in shelf x
  44.              */
  45.             total_number_of_pages[x][nb-1] = y;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement