Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question
### Dynamic Array Creation in C

#### Question 15

Write the C function that will create an instance of the structure for the dynamic array defined below and initialize the fields in the structure to reflect that space for 20 values have been allocated in the dynamic array. The function’s parameter must match the call shown below.

```c
typedef struct dynArrStruct {
    int *arr;     /* pointer to the dynamic array            */
    int allocated; /* amount of space currently allocated for the dynamic array */
    int inUse;    /* number of values currently stored in the dynamic array */
} dynArr;

void main() {
    dynArr *a1;
    a1 = create(); /* write the function to match this call */
}

/* Function Definition */
dynArrStruct *create(unsigned n) {
    dynArrStruct* matrix;
    matrix = malloc(sizeof(*matrix));
    matrix->allocated = n;
    matrix->inUse = 0;
    matrix->arr = malloc(sizeof(int *) * n);
    for(int i = 0; i < n; i++) {
        matrix->arr[i] = malloc(sizeof(int) * (n - i));
    }
    return matrix;
}
```

#### Explanation

- **Structure Definition**: A `typedef` struct named `dynArrStruct` is defined with three components:
  - `int *arr`: A pointer to the dynamic integer array.
  - `int allocated`: An integer representing the total space allocated for the array.
  - `int inUse`: An integer tracking the number of current elements stored in the array.

- **Main Function**: In `main()`, a pointer `a1` of type `dynArr` is declared and assigned the result of the `create()` function, which you must implement.

- **Create Function**: 
  - The function `create()` accepts an unsigned integer `n` as an argument, which determines the size of the dynamic array.
  - `dynArrStruct* matrix`: A pointer to `dynArrStruct` is declared.
  - `matrix` is allocated memory using `malloc()`.
  - `matrix->allocated` is set to `n`.
  - `matrix->inUse` is initialized to `0`, indicating no elements are currently stored.
  - Memory for the integer array `matrix->arr` is allocated.
  - An inner loop allocates diminishing amounts of additional memory for each element in the array
expand button
Transcribed Image Text:### Dynamic Array Creation in C #### Question 15 Write the C function that will create an instance of the structure for the dynamic array defined below and initialize the fields in the structure to reflect that space for 20 values have been allocated in the dynamic array. The function’s parameter must match the call shown below. ```c typedef struct dynArrStruct { int *arr; /* pointer to the dynamic array */ int allocated; /* amount of space currently allocated for the dynamic array */ int inUse; /* number of values currently stored in the dynamic array */ } dynArr; void main() { dynArr *a1; a1 = create(); /* write the function to match this call */ } /* Function Definition */ dynArrStruct *create(unsigned n) { dynArrStruct* matrix; matrix = malloc(sizeof(*matrix)); matrix->allocated = n; matrix->inUse = 0; matrix->arr = malloc(sizeof(int *) * n); for(int i = 0; i < n; i++) { matrix->arr[i] = malloc(sizeof(int) * (n - i)); } return matrix; } ``` #### Explanation - **Structure Definition**: A `typedef` struct named `dynArrStruct` is defined with three components: - `int *arr`: A pointer to the dynamic integer array. - `int allocated`: An integer representing the total space allocated for the array. - `int inUse`: An integer tracking the number of current elements stored in the array. - **Main Function**: In `main()`, a pointer `a1` of type `dynArr` is declared and assigned the result of the `create()` function, which you must implement. - **Create Function**: - The function `create()` accepts an unsigned integer `n` as an argument, which determines the size of the dynamic array. - `dynArrStruct* matrix`: A pointer to `dynArrStruct` is declared. - `matrix` is allocated memory using `malloc()`. - `matrix->allocated` is set to `n`. - `matrix->inUse` is initialized to `0`, indicating no elements are currently stored. - Memory for the integer array `matrix->arr` is allocated. - An inner loop allocates diminishing amounts of additional memory for each element in the array
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education