Created
February 25, 2015 22:13
-
-
Save amullins83/adb32964ad1d5b6e35a9 to your computer and use it in GitHub Desktop.
Using m_vector in a real project
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 characters
#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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using m_vector
This is an example showing how to use my dirt-simple m_vector library to create and delete data dynamically. The program comes from this StackOverflow question, 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.