/
vector.c
61 lines (56 loc) · 1015 Bytes
/
vector.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
#include <stdlib.h>
#define ENLARGE_FACTOR 2
#define SHRINK_FACTOR 0.5
int count;//number of elements
int* ar;//array
int size;//size of the array
int* createArray(int size);
void addElement(int element);
void removeElement(int element);
int* resizeArray(int factor);
void printElements();
int main()
{
printf("Enter size:");
scanf("%d", &size);
ar = createArray(size);
printElements();
// ar = resizeArray(ENLARGE_FACTOR);
// printElements();
addElement(3);
addElement(4);
addElement(5);
addElement(6);
printElements();
}
void printElements()
{
printf("\nElements:");
for (int i = 0; i < size; ++i)
{
printf("%d\t", ar[i]);
}
printf("\n");
}
void addElement(int element)
{
if (count > (size / 2))
{
ar = resizeArray(ENLARGE_FACTOR);
}
ar[count] = element;
count++;
}
int* createArray(int size)
{
count = 0;
int* p = (int*)calloc(size, sizeof(int));
return p;
}
int* resizeArray(int factor)
{
size = size * factor;
int* p = (int*)realloc(ar, size);
return p;
}