C langauge Complete the function that transforms a bag into a set in the followi
ID: 3712147 • Letter: C
Question
C langauge
Complete the function that transforms a bag into a set in the following code-- A set cannot have repeated instances of the same element. Your function void bag2set(struct DynArr *da) should call the functions that you implemented for the first part of HW2 in
In void bag2set(struct DynArr *da), you should also free the memory space allocated to the input bag, since the bag is not needed any more after exiting the function.
You may use the provided main function for testing your code. Note that you would need to fix the data type in dynArray.h as #define TYPE double
/* bag2set.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dynArray.h"
/* Converts the input bag into a set using dynamic arrays
param: da -- pointer to a bag
return value: void
result: after exiting the function da points to a set
*/
void bag2set(struct DynArr *da)
{
/* FIX ME */
}
/* An example how to test your bag2set() */
int main(int argc, char* argv[]){
int i;
struct DynArr da; /* bag */
initDynArr(&da, 100);
da.size = 10;
da.data[0] = 1.3;
for (i=1;i<da.size;i++){
da.data[i] = 1.2;
}
printf("Bag: ");
for (i=0;i<da.size;i++){
printf("%g ", da.data[i]);
}
printf(" ");
printf("Set: ");
bag2set(&da);
for (i=0;i<da.size;i++){
printf("%g ", da.data[i]);
}
printf(" ");
return 0;
}
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dynArray.h"
/* Converts the input bag into a set using dynamic arrays
param: da -- pointer to a bag
return value: void
result: after exiting the function da points to a set
*/
void bag2set(struct DynArr *da)
{
/* FIX ME */
}
/* An example how to test your bag2set() */
int main(int argc, char* argv[]){
int i;
struct DynArr da; /* bag */
initDynArr(&da, 100);
da.size = 10;
da.data[0] = 1.3;
for (i=1;i<da.size;i++){
da.data[i] = 1.2;
}
printf("Bag: ");
for (i=0;i<da.size;i++){
printf("%g ", da.data[i]);
}
printf(" ");
printf("Set: ");
bag2set(&da);
for (i=0;i<da.size;i++){
printf("%g ", da.data[i]);
}
printf(" ");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.