Question

In: Computer Science

C-Language Modify the existing vector's contents, by erasing 200, then inserting 100 and 102 in the...

C-Language

Modify the existing vector's contents, by erasing 200, then inserting 100 and 102 in the shown locations. Use Vector ADT's erase() and insert() only. Sample output of below program:

100 101 102 103 

----------------------------------------------------------------------------------

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

// struct and typedef declaration for Vector ADT
typedef struct vector_struct {
   int* elements;
   unsigned int size;
} vector;

// Initialize vector with specified size
void vector_create(vector* v, unsigned int vectorSize) {
   int i;

   if (v == NULL) return;

   v->elements = (int*)malloc(vectorSize * sizeof(int));
   v->size = vectorSize;
   for (i = 0; i < v->size; ++i) {
      v->elements[i] = 0;
   }
}

// Resize the size of the vector
void vector_resize(vector* v, unsigned int vectorSize) {
   int oldSize;
   int i;
   if (v == NULL) return;

   oldSize = v->size;
   v->elements = (int*)realloc(v->elements, vectorSize * sizeof(int));
   v->size = vectorSize;
   for (i = oldSize; i < v->size; ++i) {
      v->elements[i] = 0;
   }
}

// Return pointer to element at specified index
int* vector_at(vector* v, unsigned int index) {
   if (v == NULL || index >= v->size) return NULL;

   return &(v->elements[index]);
}

// Insert new value at specified index
void vector_insert(vector* v, unsigned int index, int value) {
   int i;

   if (v == NULL || index > v->size) return;

   vector_resize(v, v->size + 1);
   for (i = v->size - 1; i > index; --i) {
      v->elements[i] = v->elements[i-1];
   }
   v->elements[index] = value;
}

// Insert new value at end of vector
void vector_push_back(vector* v, int value) {
   vector_insert(v, v->size, value);
}

// Erase (remove) value at specified index
void vector_erase(vector* v, unsigned int index) {
   int i;

   if (v == NULL || index >= v->size) return;

   for (i = index; i < v->size - 1; ++i) {
      v->elements[i] = v->elements[i+1];
   }
   vector_resize(v, v->size - 1);
}

// Return number of elements within vector
int vector_size(vector* v) {
   if (v == NULL) return -1;

   return v->size;
}

void PrintVectors(vector* numberList) {
   int i;

   for (i = 0; i < vector_size(numberList); ++i) {
      printf("%d ", *vector_at(numberList, i));
   }
   printf("\n");
}

int main(void) {
   vector numberList;
   vector_create(&numberList, 0);

   // Populate vector with 101 200 103
   vector_push_back(&numberList, 101);
   vector_push_back(&numberList, 200);
   vector_push_back(&numberList, 103);

   // Erase 200 then insert 100 and 102

   /* Your solution goes here */

   PrintVectors(&numberList);

   return 0;
}

Solutions

Expert Solution

The given program after the modification as per the requirement is given below and the code which was newly added is highlighted in bold:

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

// struct and typedef declaration for Vector ADT
typedef struct vector_struct {
int* elements;
unsigned int size;
} vector;

// Initialize vector with specified size
void vector_create(vector* v, unsigned int vectorSize) {
int i;

if (v == NULL) return;

v->elements = (int*)malloc(vectorSize * sizeof(int));
v->size = vectorSize;
for (i = 0; i < v->size; ++i) {
v->elements[i] = 0;
}
}

// Resize the size of the vector
void vector_resize(vector* v, unsigned int vectorSize) {
int oldSize;
int i;
if (v == NULL) return;

oldSize = v->size;
v->elements = (int*)realloc(v->elements, vectorSize * sizeof(int));
v->size = vectorSize;
for (i = oldSize; i < v->size; ++i) {
v->elements[i] = 0;
}
}

// Return pointer to element at specified index
int* vector_at(vector* v, unsigned int index) {
if (v == NULL || index >= v->size) return NULL;

return &(v->elements[index]);
}

// Insert new value at specified index
void vector_insert(vector* v, unsigned int index, int value) {
int i;

if (v == NULL || index > v->size) return;

vector_resize(v, v->size + 1);
for (i = v->size - 1; i > index; --i) {
v->elements[i] = v->elements[i-1];
}
v->elements[index] = value;
}

// Insert new value at end of vector
void vector_push_back(vector* v, int value) {
vector_insert(v, v->size, value);
}

// Erase (remove) value at specified index
void vector_erase(vector* v, unsigned int index) {
int i;

if (v == NULL || index >= v->size) return;

for (i = index; i < v->size - 1; ++i) {
v->elements[i] = v->elements[i+1];
}
vector_resize(v, v->size - 1);
}

// Return number of elements within vector
int vector_size(vector* v) {
if (v == NULL) return -1;

return v->size;
}

void PrintVectors(vector* numberList) {
int i;

for (i = 0; i < vector_size(numberList); ++i) {
printf("%d ", *vector_at(numberList, i));
}
printf("\n");
}

int main(void) {
vector numberList;
vector_create(&numberList, 0);

// Populate vector with 101 200 103
vector_push_back(&numberList, 101);
vector_push_back(&numberList, 200);
vector_push_back(&numberList, 103);
  
// Erase 200 then insert 100 and 102

/* Your solution goes here */
vector_erase(&numberList, 1); //newly added code
vector_insert(&numberList, 0, 100); //newly added code
vector_insert(&numberList, 2, 102); //newly added code

  
PrintVectors(&numberList);

return 0;
}

OUTPUT:

100 101 102 103


Related Solutions

Method in C language for inserting in descending order in a single linked list. It has...
Method in C language for inserting in descending order in a single linked list. It has a head and tail variable.
C++ please Instructions Download and modify the Lab5.cpp program. Currently the program will read the contents...
C++ please Instructions Download and modify the Lab5.cpp program. Currently the program will read the contents of a file and display each line in the file to the screen. It will also display the line number followed by a colon. You will need to change the program so that it only display 24 lines from the file and waits for the user to press the enter key to continue. Do not use the system(“pause”) statement. Download Source Lab 5 File:...
C language Modify the getAvg() function to support passing an array by reference. The parameters of...
C language Modify the getAvg() function to support passing an array by reference. The parameters of this function should be pointer to a float array, and the size of the array. Returns the average of the array. Build a function called getScoreFromUser() with the following properties: Asks the user to enter the score scans into a local float variable returns the float value that the user entered. Build and present a menu to a user like the following and enclose...
Change the program to modify the output file by making each sentence a new paragraph (inserting...
Change the program to modify the output file by making each sentence a new paragraph (inserting two carriage returns between every sentence. :) Don't over-think this, but you must have worked through and understand how the program works now in order to modify it. Remember, you want the carriage returns between every SENTENCE, not every LINE. How would one do this? I'm not to sure how to make it make a new line after a sentence. Any help will be...
Change the program to modify the output file by making each sentence a new paragraph (inserting...
Change the program to modify the output file by making each sentence a new paragraph (inserting two carriage returns between every sentence. :) Don't over-think this, but you must have worked through and understand how the program works now in order to modify it. Remember, you want the carriage returns between every SENTENCE, not every LINE. How would one do this? I'm not to sure how to make it make a new line after a sentence. Any help will be...
At 100°C, The molality of solute is 2.13 m. If Kb is 0.52 K/m, the boiling point of solution will be (a) 102°C (b) 103°C (c) 101 °C (d) 100°C
At 100°C, The molality of solute is 2.13 m. If Kb is 0.52 K/m, the boiling point of solution will be ________ (a) 102°C (b) 103°C (c) 101°C (d) 100°C
C++ language There are 100 integers in the inputfile.txt. Read each integer in the inputfile.txt and...
C++ language There are 100 integers in the inputfile.txt. Read each integer in the inputfile.txt and store each one in the array named myArray. After that, complete the code in the function printArray(...) to output the elements in the array with the following format: - Each output line should consist of 10 integers - Each integer should be separated by | The output should be exactly as shown Array contains the following elements 12 | 32 | 34 | 56...
Modify/write your Newton-Raphson program in c language for single nonlinear equation to solve the following nonlinear...
Modify/write your Newton-Raphson program in c language for single nonlinear equation to solve the following nonlinear system f1(x,y)=x3+y−1 = 0 f2(x,y)=y3−x+1 = 0 using the Newton-Raphson method with initial solutions x(0) = 0.5 and y(0) = 0.5 and the convergence criterion max(|f1(x, y)|, |f2(x, y)|) < ε = 10−6.
If the temperature of an ideal gas is raised from 100°C to 200°C, while the pressure...
If the temperature of an ideal gas is raised from 100°C to 200°C, while the pressure remains constant, the volume 1.) goes to 1/2 of the original volume 2.) doubles 3.) remains the same 4.) increases by a factor of 100 5.) none of these
Suppose that C = 800, I = 300, G = 200, and X = -100.
Suppose that C = 800, I = 300, G = 200, and X = -100. a. Calculate GDP. b. Calculate each of the four components of GDP. c. Suppose G increases to 300 and GDP increases to 1,500. What is the new government component?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT