
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Usage of Variable Length Arrays in C
Suppose we are in charge of building a library system that monitors and queries various operations at the library. We are now asked to implement three different commands that perform the following −
By using command 1, we can record the insertion of a book with y pages at shelf x.
By using command 2, we can print the page number of the y-th book at shelf x.
By using command 3, we can print the number of books on shelf x.
The commands are given to us as a 2D array in this format {command type, x, y}. If there is no y value, the value will default at 0. We print the results of the given commands.
So, if the input is like number of shelves = 4, queries = 4, input_arr = {{1, 3, 23}, {1, 4, 128}, {2, 3, 0}, {3, 4, 0}}; then the output will be
23 1
Command 1 inserts a book with 23 pages on shelf 3. Command 2 inserts a book with 128 pages on shelf 4. Command 3 prints the page number of book 0 on shelf 3. Command 4 prints the number of books on shelf 3.
To solve this, we will follow these steps −
- b := a new array of size s
- p := a new array of size s
- for initialize i := 0, when i < s, update (increase i by 1), do:
- b[i] := 0
- p[i] := a new array
- for initialize loopCount := 0, when loopCount < q, update (increase loopCount by 1), do −
- qtype := q_array[loopCount, 0]
- if qtype is same as 1, then −
- x := q_array[loopCount, 1]
- y := q_array[loopCount, 2]
- b[x] := b[x] + 1
- p[x] := deallocate the object pointed by p[x] and return a pointer of size
- b[x]
- p[x, b[x] - 1] = y
- otherwise when qtype is same as 2, then −
- x := q_array[loopCount, 1]
- y := q_array[loopCount, 2]
- print(p[x, y])
- Otherwise
- x := q_array[loopCount, 1]
- print(b[x])
- if b is not null, then −
- deallocate the memory acquired by b
- for initialize i := 0, when i < s, update (increase i by 1), do−
- if p[i] is not null, then−
- deallocate the memory acquired by p[i]
- if p is not null, then−
- deallocate the memory acquired by p
- if p[i] is not null, then−
Example
Let us see the following implementation to get better understanding −
#include <stdio.h> #include <stdlib.h> void solve(int s, int q, int q_array[][3]) { int* b; int** p; b = (int*)malloc(sizeof(int)*s); p = (int**)malloc(sizeof(int*)*s); for(int i = 0; i < s; i++) { b[i] = 0; p[i] = (int*)malloc(sizeof(int)); } int loopCount; for(loopCount = 0; loopCount < q; loopCount++) { int qtype; qtype = q_array[loopCount][0]; if (qtype == 1) { int x, y; x = q_array[loopCount][1]; y = q_array[loopCount][2]; b[x] += 1; p[x] = realloc(p[x], b[x]*sizeof(int)); p[x][b[x] - 1] = y; } else if (qtype == 2) { int x, y; x = q_array[loopCount][1]; y = q_array[loopCount][2]; printf("%d
", p[x][y]); } else { int x; x = q_array[loopCount][1]; printf("%d
", b[x]); } } if (b) free(b); for (int i = 0; i < s; i++) if (p[i]) free(p[i]); if (p) free(p); } int main() { int input_arr[][3] = {{1, 3, 23}, {1, 4, 128}, {2, 3, 0}, {3, 4, 0}}; solve(4, 4, input_arr); }
Input
int input_arr[][3] = {{1, 3, 23}, {1, 4, 128}, {2, 3, 0}, {3, 4, 0}}; solve(4, 4, input_arr);
Output
23 1