Question

In: Computer Science

C programming - Implement a dynamic array, please read and implement the dynarray.c file with what...

C programming - Implement a dynamic array, please read and implement the dynarray.c file with what is given

dynarray.h

/*
* This file contains the definition of the interface for the dynamic array
* you'll implement. You can find descriptions of the dynamic array functions,
* including their parameters and their return values, in dynarray.c. You
* should not modify anything in this file.
*/

#ifndef __DYNARRAY_H
#define __DYNARRAY_H

/*
* Structure used to represent a dynamic array.
*/
struct dynarray;

/*
* Dynamic array interface function prototypes. Refer to dynarray.c for
* documentation about each of these functions.
*/
struct dynarray* dynarray_create();
void dynarray_free(struct dynarray* da);
int dynarray_size(struct dynarray* da);
void dynarray_insert(struct dynarray* da, void* val);
void dynarray_remove(struct dynarray* da, int idx);
void* dynarray_get(struct dynarray* da, int idx);
void dynarray_set(struct dynarray* da, int idx, void* val);

#endif

dynarray.c

#include

#include "dynarray.h"

/*
* This is the definition of the dynamic array structure you'll use for your
* implementation. Importantly, your dynamic array implementation will store
* each data element as a void* value. This will permit data of any type to
* be stored in your array. Because each individual element will be stored in
* your array as type void*, the data array needs to be an array of void*.
* Hence it is of type void**.
*
* You should not modify this structure.
*/
struct dynarray {
void** data;
int size;
int capacity;
};

/*
* This function should allocate and initialize a new, empty dynamic array and
* return a pointer to it. The array you allocate should have an initial
* capacity of 2.
*/
struct dynarray* dynarray_create() {
return NULL;
}

/*
* This function should free the memory associated with a dynamic array. In
* particular, while this function should up all memory used in the array
* itself (i.e. the underlying `data` array), it should not free any memory
* allocated to the pointer values stored in the array. In other words, this
* function does not need to *traverse* the array and free the individual
* elements. This is the responsibility of the caller.
*
* Params:
* da - the dynamic array to be destroyed. May not be NULL.
*/
void dynarray_free(struct dynarray* da) {
return;
}

/*
* This function should return the size of a given dynamic array (i.e. the
* number of elements stored in it, not the capacity).
*/
int dynarray_size(struct dynarray* da) {
return 0;
}

/*
* This function should insert a new value to a given dynamic array. For
* simplicity, this function should only insert elements at the *end* of the
* array. In other words, it should always insert the new element immediately
* after the current last element of the array. If there is not enough space
* in the dynamic array to store the element being inserted, this function
* should double the size of the array.
*
* Params:
* da - the dynamic array into which to insert an element. May not be NULL.
* val - the value to be inserted. Note that this parameter has type void*,
* which means that a pointer of any type can be passed.
*/
void dynarray_insert(struct dynarray* da, void* val) {
return;
}

/*
* This function should remove an element at a specified index from a dynamic
* array. All existing elements following the specified index should be moved
* forward to fill in the gap left by the removed element. In other words, if
* the element at index i is removed, then the element at index i+1 should be
* moved forward to index i, the element at index i+2 should be moved forward
* to index i+1, the element at index i+3 should be moved forward to index i+2,
* and so forth.
*
* Params:
* da - the dynamic array from which to remove an element. May not be NULL.
* idx - the index of the element to be removed. The value of `idx` must be
* between 0 (inclusive) and n (exclusive), where n is the number of
* elements stored in the array.
*/
void dynarray_remove(struct dynarray* da, int idx) {
return;
}

/*
* This function should return the value of an existing element a dynamic
* array. Note that this value should be returned as type void*.
*
* Params:
* da - the dynamic array from which to get a value. May not be NULL.
* idx - the index of the element whose value should be returned. The value
* of `idx` must be between 0 (inclusive) and n (exclusive), where n is the
* number of elements stored in the array.
*/
void* dynarray_get(struct dynarray* da, int idx) {
return NULL;
}

/*
* This function should update (i.e. overwrite) the value of an existing
* element in a dynamic array.
*
* Params:
* da - the dynamic array in which to set a value. May not be NULL.
* idx - the index of the element whose value should be updated. The value
* of `idx` must be between 0 (inclusive) and n (exclusive), where n is the
* number of elements stored in the array.
* val - the new value to be set. Note that this parameter has type void*,
* which means that a pointer of any type can be passed.
*/
void dynarray_set(struct dynarray* da, int idx, void* val) {
return;
}

Solutions

Expert Solution

dynarray.h needs not to be altered.

file dynarray.c:

#include <stdlib.h>
#include "dynarray.h"
/*
* This is the definition of the dynamic array structure you'll use for your
* implementation. Importantly, your dynamic array implementation will store
* each data element as a void* value. This will permit data of any type to
* be stored in your array. Because each individual element will be stored in
* your array as type void*, the data array needs to be an array of void*.
* Hence it is of type void**.
*
* You should not modify this structure.
*/
struct dynarray {
    void** data;
    int size;
    int capacity;
};

/*
* This function should allocate and initialize a new, empty dynamic array and
* return a pointer to it. The array you allocate should have an initial
* capacity of 2.
*/
struct dynarray* dynarray_create() {
    struct dynarray* arr = (struct dynarray*)malloc(sizeof(struct dynarray));
    arr->capacity=2;
    arr->size = 0;
    arr->data = (void**)malloc(sizeof(void*) * 2);
    return arr;
}

/*
* This function should free the memory associated with a dynamic array. In
* particular, while this function should up all memory used in the array
* itself (i.e. the underlying `data` array), it should not free any memory
* allocated to the pointer values stored in the array. In other words, this
* function does not need to *traverse* the array and free the individual
* elements. This is the responsibility of the caller.
*
* Params:
* da - the dynamic array to be destroyed. May not be NULL.
*/
void dynarray_free(struct dynarray* da) {
    free(da->data);
    return;
}

/*
* This function should return the size of a given dynamic array (i.e. the
* number of elements stored in it, not the capacity).
*/
int dynarray_size(struct dynarray* da) {
    return da->size;
}

/*
* This function should insert a new value to a given dynamic array. For
* simplicity, this function should only insert elements at the *end* of the
* array. In other words, it should always insert the new element immediately
* after the current last element of the array. If there is not enough space
* in the dynamic array to store the element being inserted, this function
* should double the size of the array.
*
* Params:
* da - the dynamic array into which to insert an element. May not be NULL.
* val - the value to be inserted. Note that this parameter has type void*,
* which means that a pointer of any type can be passed.
*/
void dynarray_insert(struct dynarray* da, void* val) {
    if( da->size == da->capacity )
    {
        // double the capacity
        da->capacity += da->capacity;
        // allocating new conintous memory
        da->data = (void**)realloc(da->data, sizeof(void*) * (da->capacity));
    }
    da->data[da->size] = val;
    da->size += 1;
    return;
}

/*
* This function should remove an element at a specified index from a dynamic
* array. All existing elements following the specified index should be moved
* forward to fill in the gap left by the removed element. In other words, if
* the element at index i is removed, then the element at index i+1 should be
* moved forward to index i, the element at index i+2 should be moved forward
* to index i+1, the element at index i+3 should be moved forward to index i+2,
* and so forth.
*
* Params:
* da - the dynamic array from which to remove an element. May not be NULL.
* idx - the index of the element to be removed. The value of `idx` must be
* between 0 (inclusive) and n (exclusive), where n is the number of
* elements stored in the array.
*/
void dynarray_remove(struct dynarray* da, int idx) {
    for(int i = idx; i < da->size-1; i++) {
        da->data[i] = da->data[i+1];
    }
    da->size --;
    if( da->size == da->capacity / 4 )
    {
        da->data = (void **)realloc(da->data, sizeof(void*) * (da->capacity/2));
        da->capacity /= 2;
    }
    return;
}

/*
* This function should return the value of an existing element a dynamic
* array. Note that this value should be returned as type void*.
*
* Params:
* da - the dynamic array from which to get a value. May not be NULL.
* idx - the index of the element whose value should be returned. The value
* of `idx` must be between 0 (inclusive) and n (exclusive), where n is the
* number of elements stored in the array.
*/
void* dynarray_get(struct dynarray* da, int idx) {
    return da->data[idx];
}

/*
* This function should update (i.e. overwrite) the value of an existing
* element in a dynamic array.
*
* Params:
* da - the dynamic array in which to set a value. May not be NULL.
* idx - the index of the element whose value should be updated. The value
* of `idx` must be between 0 (inclusive) and n (exclusive), where n is the
* number of elements stored in the array.
* val - the new value to be set. Note that this parameter has type void*,
* which means that a pointer of any type can be passed.
*/
void dynarray_set(struct dynarray* da, int idx, void* val) {
    da->data[idx] = val;
    return;
}

Test the files using a main file. Some of the functions are used in program below:

main.c

#include <stdio.h>
#include <stdlib.h>
#include "dynarray.h"

int main()
{
    struct dynarray* A = dynarray_create();
    int n;
    scanf("%d", &n);
    for(int i=0; i<n; i++)
    {
        int *val;
        val = (int*)malloc(sizeof(int));
        scanf("%d", val);
        void* v = val;
        dynarray_insert(A, v);
    }
    printf("\n");
    for(int i=0;i<n;i++)
    {
        printf("%d ", *(int*)dynarray_get(A, i));
    }
    printf("\n");
    return 0;
}

Compile using gcc:

gcc main.c dynarray.c

Run the generated a.out or a.exe using

./a.out

Related Solutions

Implement a queue - C programming, please read the instruction carefully and implement queue.c using dynamic...
Implement a queue - C programming, please read the instruction carefully and implement queue.c using dynamic array structure given dynarray.h and dynarray.c below The second ADT you'll implement for this assignment is a queue. For this assignment, the interface for the queue (i.e. the structures and the prototypes of functions a user of the queue interacts with) is already defined for you in the file queue.h. Your first task in this assignment is to implement definitions for the functions that...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t use vectors), and provide an implementation for the following operations on books in the array 1)isEmpty() returns true if the array is empty, otherwise false 2)isFull() returns true if the array is full, otherwise false 3)listSize() prints the number of books in the array 4)print() prints the content of the array 5)insert(Book) asks the user to enter new book info, and it adds the...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t use vectors), and provide an implementation for the following operations on books in the array 1)isEmpty() returns true if the array is empty, otherwise false 2)isFull() returns true if the array is full, otherwise false 3)listSize() prints the number of books in the array 4)print() prints the content of the array 5)insert(Book) asks the user to enter new book info, and it adds the...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a dummy list for a company which stores following information about its customers. Customer ID Customer Name Gender Total items purchased Item category 20% discount in percentage of total purchase amount. Use dynamic array to save at least 20 items by dividing them into 3 different categories. Make a dummy list of items that company sells by dividing them into two categorizes. Items has following...
Why do we need a dynamic stack and How to implement a dynamic array stack? (...
Why do we need a dynamic stack and How to implement a dynamic array stack? ( Please answer in Java)
C Programming file.c takes in one input argument that denotes a file to be read. It...
C Programming file.c takes in one input argument that denotes a file to be read. It needs to convert the contents of that file into a character array (char *) and then into a an unsigned character array (unsigned char *). Please fix and or complete the program, and explain any of the changes too: ---- #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) { FILE *f; f = fopen(argv[1], "r"); if( !f ) { exit(1); }...
Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for...
Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for a polynomial. Much of this work can be modelled on the C++ dynamic array of ints List that we discussed in class. This class does not need the method the overloaded += operator. Your class should have the following methods: Write one constructor that takes an integer n for the degree of a term and a double coefficient c for the coefficient of the...
For this assignment you will implement a dynamic array. You are to build a class called...
For this assignment you will implement a dynamic array. You are to build a class called MyDynamicArray. Your dynamic array class should manage the storage of an array that can grow and shrink. The public methods of your class should be the following: MyDynamicArray(); Default Constructor. The array should be of size 2. MyDynamicArray(int s); For this constructor the array should be of size s. ~MyDynamicArray(); Destructor for the class. int& operator[](int i); Traditional [] operator. Should print a message...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. PLEASE READ CAREFULLY. alot of people give...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. PLEASE READ CAREFULLY. alot of people give me either partial answers, or incorrect skeleton. PLEASE YOU CAN'T CHANGE WHAT IS THERE, YOU CAN ONLY ADD. void readFile(Candidate candidates[]) – reads the elections.txt file, fills the candidates[] array. Hint: use substr() and find() functions. Set Score to 0. void List(Candidate candidates[]) – prints the array of Candidate structs. One candidate per one line, include all fields. Use setw() to display nice looking...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. PLEASE READ CAREFULLY. alot of people give...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. PLEASE READ CAREFULLY. alot of people give me either partial answers, or incorrect skeleton. PLEASE YOU CAN'T CHANGE WHAT IS THERE, YOU CAN ONLY ADD. void readFile(Candidate candidates[]) – reads the elections.txt file, fills the candidates[] array. Hint: use substr() and find() functions. Set Score to 0. void List(Candidate candidates[]) – prints the array of Candidate structs. One candidate per one line, include all fields. Use setw() to display nice looking...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT