Created
February 25, 2015 22:13
-
-
Save amullins83/adb32964ad1d5b6e35a9 to your computer and use it in GitHub Desktop.
Revisions
-
amullins83 revised this gist
Feb 25, 2015 . 1 changed file with 0 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +0,0 @@ -
amullins83 created this gist
Feb 25, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ #Using m_vector This is an example showing how to use my dirt-simple [m_vector](https://github.com/amullins83/m_vector) library to create and delete data dynamically. The program comes from [this StackOverflow question](http://stackoverflow.com/questions/28728414/how-to-delete-x-lines-from-a-dinamically-allocated-array-in-c-by-using-a-subprog#28728414), where the OP seemed familiar with the C++ STL vector class, but did not know how to accomplish the same thing in C. I feel it best to keep the implementation and interface for a generic container separate from any particular application so that it is reusable. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,107 @@ #include <stdio.h> #include <malloc.h> #include "m_vector.h" #ifdef __cplusplus extern "C" { #endif #define TRUE 1 #define FALSE 0 void delete_array(M_VECTOR_DATA *element_ptr) { free(*(float **)element_ptr); } void citireM(MVector **a, size_t *n){ size_t i, j, m; scanf_s("%d", &m); scanf_s("%d", n); float *arr; (*a) = m_vector_sized_new(float *, m); (*a)->clear_func = delete_array; if (*a == NULL) { return; } for (i = 0; i < m; i++) { arr = malloc((*n)*sizeof(float)); m_vector_append_val(*a, arr); } for (i = 0; i < m; i++) for (j = 0; j < (*n); j++) scanf_s("%f", &m_vector_index(*a, float *, i)[j]); } void afisareV(MVector* v){ size_t i; for (i = 0; i < v->count; i++) printf("%d ", m_vector_index(v, int, i)); } void afisareA(MVector *a, size_t n){ size_t i, j; for (i = 0; i < a->count; i++){ for (j = 0; j < n; j++){ printf("%5.2f ", (m_vector_index(a, float *, i))[j]); } printf("\n"); } printf("\n"); } void stergere(MVector *a, MVector *v){ size_t i; for (i = 0; i < v->count; i++) { // The values in v had better be sorted! m_vector_remove_index(a, m_vector_index(v, int, i) - i); printf("\n"); } } int main(){ MVector *a; int entered_line = 0; MVector* v; size_t n, i; citireM(&a, &n); afisareA(a, n); v = m_vector_new(int); do { printf("Line to be deleted: "); scanf_s("%d", &entered_line); if (entered_line >= 0) { for (i = 0; i < v->count; i++) { if (entered_line < m_vector_index(v, int, i)) { break; } } m_vector_insert_val(v, i, entered_line); } } while (entered_line >= 0); printf("lines to be deleted\n"); afisareV(v); printf("\n"); stergere(a, v); afisareA(a, n); scanf_s("%d", &entered_line); } #ifdef __cplusplus }; #endif