-
Notifications
You must be signed in to change notification settings - Fork 0
/
vectoradd.c
49 lines (35 loc) · 965 Bytes
/
vectoradd.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
#include <stdio.h>
#include <stdlib.h>
void vector_add(float* a, float* b, float* c, int n){
int i;
for(i = 0; i < n; i++){
c[i] = a[i] + b[i];
}
}
int main(){
int n;
printf("Enter the size of the vector: ");
scanf("%d", &n);
float *a = (float *)malloc(n * sizeof(float));
float *b = (float *)malloc(n * sizeof(float));
float *c = (float *)malloc(n * sizeof(float));
printf("Enter the elements of the first vector: ");
for(int i = 0; i < n; i++){
printf("Enter the element of a %d: ", i+1);
scanf("%f", &a[i]);
}
printf("Enter the elements of the second vector: ");
for(int i = 0; i < n; i++){
printf("Enter the element of b %d: ", i+1);
scanf("%f", &b[i]);
}
vector_add(a, b, c, n);
printf("The resultant vector is: ");
for(int i = 0; i < n; i++){
printf("%f ", c[i]);
}
free(a);
free(b);
free(c);
return 0;
}