Question

In: Computer Science

Please complete the following functions using C. ------------------------------------------------------------ #include #include "dynarray.h" /* * This is the...

Please complete the following functions using 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

Hi following are the function implementations............................................

struct dynarray{
void** data;
int size ;
int capacity;
};

struct dynarry* dynarra_create(){
struct dynarray * array= malloc(sizeof(struct dynarray));
array->data=malloc(2 *sizeof(void *));
array->capacity=2;
return array;
};

void dynarray_free(struct dynarray* da){
free(*(da->data));
}

int dynarray_size(struct dynarray* da){
return da->size;
}

// how to double the size of array
void dynarray_insert(struct dynarray * da,void *val){
da->size=da->size+1;
//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.
if(da->size > da->capacity){

void** temp=malloc(2*da->capacity*sizeof(void *));
for(int i=0;i<=da->size;i++){
*(temp +i)= *(da->data+i);
}
da->data=temp;
}

*(da->data + da->size)=val;
}

void dynarray_remove(struct dynarray* da,int idx){
while(idx <= da->size){
*(da->data+idx)=*(da->data+idx+1);
idx=idx+1;
}
da->size=da->size-1;
}

void* dynarray_get(struct dynarray* da,int idx){
return *(da->data+idx);
}

void dynarry_set(struct dynarray* da,int idx,void*val){
*(da->data +idx)=val;
}


Related Solutions

Please complete the following functions in "queue.c" using C. This must use the dynamic array provided...
Please complete the following functions in "queue.c" using C. This must use the dynamic array provided in "dynarray.c" -------------------------------------------------------------------------------------------- //queue.c #include <stdlib.h> #include "queue.h" #include "dynarray.h" /* * This is the structure that will be used to represent a queue. This * structure specifically contains a single field representing a dynamic array * that should be used as the underlying data storage for the queue. * * You should not modify this structure. */ struct queue { struct dynarray* array;...
C++ Download the attached program and complete the functions. (Refer to comments) main.cpp ~ #include #include...
C++ Download the attached program and complete the functions. (Refer to comments) main.cpp ~ #include #include #define END_OF_LIST -999 using namespace std; /* * */ int exercise_1() { int x = 100; int *ptr; // Assign the pointer variable, ptr, to the address of x. Then print out // the 'value' of x and the 'address' of x. (See Program 10-2) return 0; } int exercise_2() { int x = 100; int *ptr; // Assign ptr to the address of...
Please Complete this C Code using the gcc compiler. Please include comments to explain each added...
Please Complete this C Code using the gcc compiler. Please include comments to explain each added line. /*This program computes the Intersection over Union of two rectangles as a percent: IoU = [Area(Intersection of R1 and R2) * 100 ] / [Area(R1) + Area(R2) - Area(Intersection of R1 and R2)] The answer will be specified as a percent: a number between 0 and 100. For example, if the rectangles do not overlap, IoU = 0%. If they are at the...
Data Structure using C++ Complete and test the Sudoku problem main.cpp #include <iostream> #include <cmath> #include...
Data Structure using C++ Complete and test the Sudoku problem main.cpp #include <iostream> #include <cmath> #include "sudoku.h" using namespace std; int main() { cout << "See Programming Exercise 18." << endl; } sudoku.cpp #include <iostream> #include "sudoku.h" using namespace std; sudoku::sudoku() { cout << "Write the definition. ." << endl; } sudoku::sudoku(int g[][9]) { cout << "Write the definition." << endl; } void sudoku::initializeSudokuGrid() { cout << "Write the definition." << endl; } void sudoku::initializeSudokuGrid(int g[][9]) { cout << "Write...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell {...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell { int val; Cell *next; }; int main() { int MAX = 10; Cell *c = NULL; Cell *HEAD = NULL; srand (time(NULL)); for (int i=0; i<MAX; i++) { // Use dynamic memory allocation to create a new Cell then initialize the // cell value (val) to rand(). Set the next pointer to the HEAD and // then update HEAD. } print_cells(HEAD); }
Please complete following c++ code asap using following prototypes complete each missing part / Linked list...
Please complete following c++ code asap using following prototypes complete each missing part / Linked list operations int getLength() const {return length;} void insertNode(College); bool deleteNode(string); void displayList() const; bool searchList(string, College &) const; }; main.cpp /*   Build and procees a sorted linked list of College objects. The list is sorted in ascending order by the college code. Assume that the college code is unique. */ #include <iostream> #include <fstream> #include <string> #include "LinkedList.h" using namespace std; void buildList(const string...
Complete the following TODO: parts of the code in C++ #include <iostream> #include <string> #include <limits>...
Complete the following TODO: parts of the code in C++ #include <iostream> #include <string> #include <limits> #include <vector> using namespace std; // // CLASS: NODE // class Node{ public: int value = 0; // our node holds an integer Node *next = nullptr; // our node has a pointer to the next Node Node(int i){ // contructor for our Node class value = i; // store a copy of argument "i" in "value" next = nullptr; // be sure next...
Please complete the following code in C using the comments as instructions. Further instructions are below...
Please complete the following code in C using the comments as instructions. Further instructions are below the code. challenge.c // goal: print the environment variables to the file "env.txt", one per line // (If envp is NULL, the file should be empty, opening in write mode will do that.) // example: // inputs: // envp/environ = {"E1=2","E2=7",NULL} // outputs: // env.txt as a string would be "E1=2\nE2=7\n" // example: // inputs: // envp/environ = {NULL} or NULL // outputs: //...
Complete the provided C++ program, by adding the following functions. Use prototypes and put your functions...
Complete the provided C++ program, by adding the following functions. Use prototypes and put your functions below main. 1. Write a function harmonicMeans that repeatedly asks the user for two int values until at least one of them is 0. For each pair, the function should calculate and display the harmonic mean of the numbers. The harmonic mean of the numbers is the inverse of the average of the inverses. The harmonic mean of x and y can be calculated...
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include...
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #define THREADS 10 // Number of Thread //bridge declared with array of character and integer value void Bridge(char array[], int value); // Global Variable int North = 1; //For North Number int South = 1; //For South Number pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; // Setting Up MUTEX for lock //Thread for North farmer void NorthFarmer(){ pthread_mutex_lock(&mutex1); char array[5] = "North"; // North printf("%s Tunbridge...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT