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 than space for 20 values have been allocated in the dyna The function's parameter must match the call shown below. typedef struct dynArrStruct int int int } dynArr; /* pointer to the dynamic array /* amount of space currently allocated for the dynamic array / /* number of values currently stored in the dynamic array arr; allocated; inuse;

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
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
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Array
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education