-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_arr.cxx
89 lines (69 loc) · 1.9 KB
/
insert_arr.cxx
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
void add_number_1(int *arr, int num, int size);
void add_number_2(int *arr, int num, int size);
/**
* Funcion principal
*/
int main(int argc, char *argv[])
{
int A[] = {12, 13, 14, 21, 43, 63, 87, 102, 207, 500};
int size = sizeof(A) / sizeof(A[0]);
int * array = (int *) malloc(sizeof(int) * size);
assert(array != NULL);
// copy the array
memcpy(array, A, size * sizeof(A[0]));
int num = 509;
add_number(array, num, size);
//add_number_2(array, num, size);
printf("New Array \n");
for(int i = 0; i < size + 1; i++)
{
printf("%d ", *(array + i));
}
printf("\n");
free(array);
return 0;
}
// ----------------------------------------------------------------------------
void add_number_1(int * arr, int num, int size)
{
assert(realloc(arr, sizeof(int)) != NULL);
int idx = 0;
while((*(arr + idx) < num) && (idx <= size)) idx++;
if (size > idx) {
for(int i = size; i > idx; i--)
{
*(arr + i) = *(arr + i - 1);
}
*(arr + idx) = num;
}
else
{
*(arr + idx-1) = num;
}
}
// ----------------------------------------------------------------------------
void add_number_2(int * arr, int num, int size)
{
assert(realloc(arr, sizeof(int)) != NULL);
int idx = 0;
while((*(arr + idx) < num) && (idx <= size)) idx++;
if (size > idx) {
int tmp_1, tmp_2;
tmp_1 = *(arr + idx);
tmp_2 = *(arr + idx+1);
for (int i = idx+1; i <= size; i++)
{
*(arr + i) = tmp_1;
tmp_1 = tmp_2;
tmp_2 = *(arr + i+1);
}
*(arr + idx) = num;
}
else {
*(arr + idx-1) = num;
}
}