verified insertion of vectors into vectors; proved working

This commit is contained in:
Odweta
2023-12-21 11:35:28 +01:00
parent 3988892a5e
commit eb6b6c8343
3 changed files with 183 additions and 249 deletions
+14 -79
View File
@@ -4,91 +4,26 @@
#include <string.h>
int main(int argc, char **argv) {
printf("demonstration of the power of dynamically allocated arrays in c:\n-----------\n\n");
vector_t *vv = vectorInit(VECTOR);
vector_t *fv = vectorInit(FLOAT);
vectorPrint(vv);
printf("initializing an integer vector\n");
vector_t *v = vectorInit(INT);
vectorPush(fv, &(float){3.14});
vectorPush(fv, &(float){5.123});
printf("vector after initialization\n");
vectorPrint(v);
vectorPush(vv, fv);
vectorPush(vv, fv);
vectorPush(vv, fv);
printf("pushing an element of value 1 into vector\n");
int element = 1;
vectorPush(v, &element);
vectorPrint(v);
vectorPush(fv, &(float){156.1});
vectorPush(fv, &(float){6.124});
printf("pushing another element (value 2) into vector\n");
element = 2;
vectorPush(v, &element);
vectorPrint(v);
vectorInsertAt(vv, 1, fv);
printf("inserting 3 at index 1\n");
element = 3;
vectorInsertAt(v, 1, &element);
vectorPrint(v);
printf("popping the last value from the vector\n");
int popped = *((int *)vectorPop(v));
printf("popped: %d\n", popped);
printf("vector after popping\n");
vectorPrint(v);
vectorPrint(vv);
printf("initializing a second vector of type vector_t (multidimensional vector => vector of vectors)\n");
vector_t *w = vectorInit(VECTOR);
printf("vector after initialization\n");
vectorPrint(w);
printf("pushing the first vector into the newly created one\n");
vectorPush(w, v);
vectorPrint(w);
printf("once again\n");
vectorPush(w, v);
vectorPrint(w);
printf("popping\n");
vector_t *poppedv = (vector_t *)vectorPop(w);
printf("popped vector\n");
vectorPrint(poppedv);
printf("vector from which was popped after pop\n");
vectorPrint(w);
printf("initializing a char vector (variable-length string) with value \"Hello, world!\"\n");
vector_t *charVector = vectorInit(CHAR);
char *word = "Hello, world!";
for (int i = 0; i < strlen(word); i++) {
vectorPush(charVector, &word[i]);
}
printf("char vector as string\n");
vectorPrintAsString(charVector);
printf("char vector converted into a regular string\n");
char *newWord = vectorToString(charVector);
printf("new word: %s\n", newWord);
printf("setting a value in the char vector on index 10 to \"\" (nothing, effectively deleting the character, although the size of the vector stays the same!)\n");
vectorSetAt(charVector, 10, ""); // should change the vector to "Hello, word!" (size stays the same!)
vectorPrintAsString(charVector);
printf("files section\n---------------\n");
printf("initializing a file called test.txt with mode \"r\" (reading it)\n");
file_t* f = fileInit("test.txt", "r");
printf("printing the file contents\n");
filePrint(f);
printf("file length (line count): %d\n", f->length);
printf("file as vector\n");
vectorPrint(f->lines);
printf("file to string\n");
char* strFile = fileToString(f);
printf("%s", strFile);
printf("cleaning up vectors\n");
vectorCleanup(v);
vectorCleanup(w);
vectorCleanup(charVector);
printf("cleaning up files\n");
fileCleanup(f);
vectorCleanup(vv);
vectorCleanup(fv);
return 0;
}