Skip to content

Commit

Permalink
11 | 10 | sharafat
Browse files Browse the repository at this point in the history
  • Loading branch information
SharafatKarim committed Aug 12, 2023
1 parent d2f6a51 commit d732d96
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions solutions/sharafat/11/10.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#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;
}
}

struct vector addition(struct vector *v1, struct vector *v2)
{
struct vector v3;
createVector(&v3, v1->size);
for (int i = 0; i < v1->size; i++)
{
v3.elements[i] = v1->elements[i] + v2->elements[i];
}
return v3;
}

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 v1, v2;
int vector_size;
printf("Enter vector size: ");
scanf("%d", &vector_size);

createVector(&v1, vector_size);
addElement(&v1);

createVector(&v2, vector_size);
addElement(&v2);

struct vector v3 = addition(&v1, &v2);
printf("Addition of two vectors: \n");
displayVector(&v3);
}

0 comments on commit d732d96

Please sign in to comment.