Skip to content

Instantly share code, notes, and snippets.

@amullins83
Created February 25, 2015 22:13
Show Gist options
  • Select an option

  • Save amullins83/adb32964ad1d5b6e35a9 to your computer and use it in GitHub Desktop.

Select an option

Save amullins83/adb32964ad1d5b6e35a9 to your computer and use it in GitHub Desktop.

Revisions

  1. amullins83 revised this gist Feb 25, 2015. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions README
    Original file line number Diff line number Diff line change
    @@ -1,3 +0,0 @@
    #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.
  2. amullins83 created this gist Feb 25, 2015.
    3 changes: 3 additions & 0 deletions README
    Original 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.
    107 changes: 107 additions & 0 deletions delete_matrix_rows.c
    Original 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