In: Computer Science
consider the code;
typedef struct {
int size;
int *nums;
}Data;
Complete the function below that creates and returns a new data type that contains a nums array with size randomly chosen integers. Ensure proper error checking.
Data *getRandomData(int size) {
//PUT CODE HERE
//Create random ints
for(int i=0; i<size; i++)
d->nums[1] = (int)(rand()/(float)RAND_MAX*100);
return d;
CODE
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct {
int size;
int *nums;
} Data;
Data *getRandomData(int size) {
Data *d = (Data*) malloc(1 * sizeof(Data));
d->size = size;
d->nums = malloc(size * sizeof(int));
for(int i=0; i<size; i++) {
d->nums[i] = (int)(rand()/(float) RAND_MAX * 100);
}
return d;
}
int main(void) {
srand(time(0));
Data *d = getRandomData(3);
printf("Size: %d", d->size);
printf("\nElements: %d, %d, %d", d->nums[0], d->nums[1], d->nums[2]);
return 0;
}
OUTPUT