Home  >  Article  >  Backend Development  >  C program example demonstrating the use of variable length arrays

C program example demonstrating the use of variable length arrays

WBOY
WBOYforward
2023-08-28 19:45:031345browse

C program example demonstrating the use of variable length arrays

Suppose we are responsible for building a library system for monitoring and querying various operations of the library. Now we are asked to implement three different commands that perform the following operations:

  • Using command 1, we can record the insertion of a book with y pages on bookshelf x.

  • Using command 2, we can print out the page number of the yth book on the bookshelf x.

  • Using command 3, we can print out the number of books on bookshelf x.

These commands are given as a 2D array in the format {command type, x, y}. If there is no y value, the default value is 0. We print the result of the given command.

So, if the input is as follows: Number of bookshelfs = 4, number of queries = 4, input array = {{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 problem, we will follow the following steps:

  • b := new array of size s
  • p := New array of size s
  • Initialize i := 0, when i
  • b[i] := 0
  • p[i] := New array
  • Initialize loopCount := 0, when loopCount
  • qtype := q_array[loopCount, 0]
  • If qtype is the same as 1, then −
    • x := q_array[loopCount, 1]
    • y := q_array[loopCount, 2]
    • b[x] := b[x] 1
    • p[x] := Release p[x ] object pointed to and returns size
    • b[x]
    • p[x, b[x] - 1] = y
  • else , when qtype is the 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 empty, then −
    • Release the memory obtained by b
  • Initialize i := 0, when i
      If p[i] is not empty, then−
      • Release the memory acquired by p[i]
    • If p is not empty, then −
      • Release the memory obtained by p
    Example

    Let’s take a look at the following implementation for 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><p>", p[x][y]);
          }
          else
          {
             int x;
             x = q_array[loopCount][1];
             printf("%d</p><p>", 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

    The above is the detailed content of C program example demonstrating the use of variable length arrays. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete