
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
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](https://content.bartleby.com/qna-images/question/672bf286-8abe-4b07-9ca1-0d5b2612956c/882d996a-e140-410a-9bf0-5c6774132580/ozd1js3_thumbnail.jpeg)
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

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution
Trending nowThis is a popular solution!
Step by stepSolved in 3 steps with 1 images

Knowledge Booster
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
- What is meant by a pointer variable? How is it used? What is a dynamic array? What is the relationship between a dynamic array and pointers?arrow_forwardC++ programingarrow_forwardWRITE A PROGRAM IN C++ You work for an loan analytics organization have been tasked with writing a program that simulates an analysis of loan applications. Code a modular program that uses parallel arrays to store loan application credit scores (values generated between 300 and 900), score ratings (poor, fair, good, very good, or exceptional based on credit score) and loan status (approved or declined applications based on credit score). The program must do the following: The program must first ask the user for the number of loan applications that will be in the simulation. This must be done by calling the getNumLoanApplications() function (definition below). After the input of the number of accounts, the program must use loops and random numbers to generate the credit score, score rating, and application result for each loan application in parallel arrays. These arrays must not be declared globally (major error). The following arrays must be declared and populated: creditScores[]…arrow_forward
- Would it be possible to use unique, shared or weak pointers in the code?arrow_forwardneed help in C++ Problem: You are asked to create a program for storing the catalog of movies at a DVD store using functions, files, and user-defined structures. The program should let the user read the movie through the file, add, remove, and output movies to the file. For this assignment, you must store the information about the movies in the catalog using a single vector. The vector's data type is a user-defined structure that you must define on functions.h following these rules: Identifier for the user-define structure: movie. Member variables of the structure "movie": name (string), year (int), and genre (string). Note: you must use the identifiers presented before when defining the user-defined structure. Your solution will NOT pass the unit test cases if you do not follow the instructions presented above. The main function is provided (you need to modify the code of the main function to call the user-defined functions described below). The following user-defined functions are…arrow_forwardA pointer variable is just what it sounds like. What is its purpose? Exactly what does it mean to have a "dynamic array"? Pointers and dynamic arrays have what relationship?arrow_forward
- 1) Define a global variable Emps that is an array of pointers to employee structs. 2) Change your createEmployee function to add the new employee to the Emps array. It should place it in the first slot that contains a NULL. 3) Write a listEmployees function that will call your display function on each employee in the array. 4) Change main to have a loop that looks for user commands. • If the user types HIRE, you should call createEmployee. If the user types LIST, you should call listEmployees. • If the user types QUIT, you should exit the loop, which will then cause the program to exit. 5) Write a findEmployee function that takes a string parameter and returns the employee pointer with that name or NULL if no such person exists. Change main to add a FIND name command that will find and display a single employee.arrow_forwardcreate using c++ One problem with dynamic arrays is that once the array is created using the new operator the size cannot be changed. For example, you might want to add or delete entries from the array similar to the behavior of a vector . This project asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings. The class should have the following A private member variable called dynamicArray that references a dynamic array of type string. A private member variable called size that holds the number of entries in the array. A default constructor that sets the dynamic array to NULL and sets size to 0. A function that returns size . A function named addEntry that takes a string as input. The function should create a new dynamic array one element larger than dynamicArray , copy all elements from dynamicArray into the new array, add the new string onto the end of the new array, increment size, delete the…arrow_forwardTo effectively utilize a shared virtual function across multiple forms and structures, what is the appropriate method for a program to invoke it when employing base class pointers or references?arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education

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)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON

Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education