0% found this document useful (0 votes)
77 views2 pages

Dynamic Array Management in C

This C program contains code to process queries about books stored on shelves. It takes input for the number of shelves and queries. Based on the query type, it will either increment the number of books for a shelf, print the number of pages for a specific book, or print the total number of books for a shelf. Memory is dynamically allocated for arrays to store the total number of books and pages for each shelf.

Uploaded by

Shiva Mandadapu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views2 pages

Dynamic Array Management in C

This C program contains code to process queries about books stored on shelves. It takes input for the number of shelves and queries. Based on the query type, it will either increment the number of books for a shelf, print the number of pages for a specific book, or print the total number of books for a shelf. Memory is dynamically allocated for arrays to store the total number of books and pages for each shelf.

Uploaded by

Shiva Mandadapu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#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);

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);
int* total_number_of_books=
(int*)malloc(total_number_of_shelves*(sizeof(int)));
int*
total_number_of_pages=(int*)malloc(total_number_of_shelves*(*(total_number_of_books
+x))*(sizeof(int)));
total_number_of_books[x]++;
int i;

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

You might also like