Question
OK SO I AM GIVEN TWO FILES AND I AM NOT SURE HOW THE VECTOR IS SUPPOSED TO FUNCTION. CAN SOMEONE ADD COMMENTS TO WHERE
OK SO I AM GIVEN TWO FILES AND I AM NOT SURE HOW THE VECTOR IS SUPPOSED TO FUNCTION. CAN SOMEONE ADD COMMENTS TO WHERE IT SAYS TO AND EXPLAIN WHAT VECTOR IS SUPPOSED TO DO OR THE IDEA OF IT.
VECTOR.C
/* Include the system headers we need */
#include
#include
/* Include our header */
#include "vector.h"
/* Define what our struct is */
struct _vector_t {
size_t size;
int *data;
};
/* Create a new vector */
vector_t *vector_new() {
vector_t *retval;
/* First, we need to allocate the memory for the struct */
retval = (vector_t *)malloc(1 * sizeof(vector_t));
/* Check our return value to make sure we got memory */
if(retval == NULL)
return NULL;
/* Why does the above statement cast the malloc's return value to 'vector_t *'
* instead of 'struct _vector_t *'? Does it matter?
*/
/* Now we need to initialize our data */
retval->size = 1;
retval->data = (int *)malloc(retval->size * sizeof(int));
/* Check our return value to make sure we got memory */
if(retval->data == NULL) {
free(retval);
return NULL;
}
retval->data[0] = 0;
/* Note that 'retval->size' could be written '(*retval).size', but the ->
* convention is easier to read
*/
/* and return... */
return retval;
}
/* Free up the memory allocated for the passed vector */
void vector_delete(vector_t *v) {
/* Remember, you need to free up ALL the memory that is allocated */
/* ADD THE COMMENTS FOR THESE */
free(v->data);
free(v);
}
/* Return the value in the vector */
int vector_get(vector_t *v, size_t loc) {
/* If we are passed a NULL pointer for our vector, complain about it and
* return 0.
*/
if(v == NULL) {
fprintf(stderr, "vector_get: passed a NULL vector. Returning 0. ");
return 0;
}
/* If the requested location is higher than we have allocated, return 0.
* Otherwise, return what is in the passed location.
*/
if(loc < v->size) {
return v->data[loc];
} else {
return 0;
}
}
/* Set a value in the vector */
void vector_set(vector_t *v, size_t loc, int value) {
/* What do you need to do if the location is greater than the size we have
* allocated? Remember that unset locations should contain a value of 0.
*/
/* ADD THE COMMENTS HERE */
if(v==NULL){
fprintf(stderr,"null vector ");
abort();
}
if (loc
v->data[loc]=value;
}
else{
int*temp =(int*)malloc(sizeof(int)*(loc+1));
if(!temp){
vector_delete(v);
}
int i;
for(i=0;i<(loc+1);i++){
if(i
temp[i] = v->data[i];
}
else{
temp[i] = value;
}
}
free(v->data);
v->data = temp;
v->size=loc+1;
}
}
AND HERE IS TEST.C
#include
#include
#include "vector.h"
int main(int argc, char **argv) {
vector_t *v;
printf("Calling vector_new() ");
v = vector_new();
printf("Calling vector_delete() ");
vector_delete(v);
printf("vector_new() again ");
v = vector_new();
printf("These should all return 0 (vector_get()): ");
printf("%d ", vector_get(v, 0));
printf("%d ", vector_get(v, 1));
printf("%d ", vector_get(v, 2));
printf("%d ", vector_get(v, 3));
printf("%d ", vector_get(v, 4));
printf("%d ", vector_get(v, 5));
printf("%d ", vector_get(v, 6));
printf("%d ", vector_get(v, 7));
printf("%d ", vector_get(v, 8));
printf("%d ", vector_get(v, 9));
printf("%d ", vector_get(v, 10));
printf("%d ", vector_get(v, 100));
printf("%d ", vector_get(v, 1000));
printf("%d ", vector_get(v, 10000));
printf("%d ", vector_get(v, 100000));
printf("%d ", vector_get(v, 1000000));
printf("%d ", vector_get(v, 10000000));
printf("Doing a bunch of vector_set()s ");
vector_set(v, 0, 98);
vector_set(v, 11, 15);
vector_set(v, 15, -23);
vector_set(v, 24, 65);
vector_set(v, 12, -123);
vector_set(v, 15, 21);
vector_set(v, 25, 43);
printf("These should be equal: ");
printf("98 = %d ", vector_get(v, 0));
printf("15 = %d ", vector_get(v, 11));
printf("65 = %d ", vector_get(v, 24));
printf("-123 = %d ", vector_get(v, 12));
printf("21 = %d ", vector_get(v, 15));
printf("43 = %d ", vector_get(v, 25));
printf("Test complete. ");
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started