-
Notifications
You must be signed in to change notification settings - Fork 16
/
9.c
66 lines (46 loc) · 947 Bytes
/
9.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
62
63
64
65
66
#include <stdio.h>
struct vector
{
int *elements;
int size;
};
struct vector createVector(int size)
{
struct vector v;
v.elements = (int *)malloc(size * sizeof(int));
v.size = size;
return v;
}
void modifyElement(struct vector *v, int index, int newValue)
{
if (index >= 0 && index < v->size)
v->elements[index] = newValue;
}
void multiplyByScalar(struct vector *v, int scalar)
{
for (int i = 0; i < v->size; i++)
{
v->elements[i] *= scalar;
}
}
void displayVector(struct vector v)
{
printf("(");
for (int i = 0; i < v.size; i++)
{
printf("%d", v.elements[i]);
if (i != v.size - 1)
printf(", ");
}
printf(")\n");
}
int main()
{
struct vector v = createVector(5);
v.elements[0] = 10;
displayVector(v);
multiplyByScalar(&v, 2);
displayVector(v);
free(v.elements);
return 0;
}