-
Notifications
You must be signed in to change notification settings - Fork 16
/
9.c
74 lines (66 loc) · 1.34 KB
/
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
67
68
69
70
71
72
73
74
#include <stdio.h>
#include <stdlib.h>
struct vector
{
int *elements;
int size;
};
void createVector(struct vector *v, int size)
{
v->elements = (int *)malloc(size * sizeof(int));
v -> size = size;
}
void addElement(struct vector *v)
{
int element;
for (int i = 0; i < v->size; i++)
{
printf("Enter element %d: ", i + 1);
scanf("%d", &element);
v->elements[i] = element;
}
}
void modifyElement(struct vector *v)
{
int index;
int newValue;
printf("Enter index: ");
scanf("%d", &index);
printf("Enter new value: ");
scanf("%d", &newValue);
if (index >= 0 && index < v->size)
v->elements[index] = newValue;
}
void multiplyByScalar(struct vector *v)
{
int scalar;
printf("Enter scalar: ");
scanf("%d", &scalar);
for (int i = 0; i < v->size; i++)
{
v->elements[i] *= scalar;
}
}
void displayVector(struct vector *v)
{
printf("Vector: ");
for (int i = 0; i < v->size; i++)
{
printf("%d ", v->elements[i]);
}
printf("\n");
}
int main()
{
struct vector v;
int vector_size;
printf("Enter vector size: ");
scanf("%d", &vector_size);
createVector(&v, vector_size);
addElement(&v);
displayVector(&v);
modifyElement(&v);
displayVector(&v);
multiplyByScalar(&v);
displayVector(&v);
}