Skip to content

Commit

Permalink
11 | 9 | sharafat
Browse files Browse the repository at this point in the history
  • Loading branch information
SharafatKarim committed Aug 12, 2023
1 parent 41cf0be commit d2f6a51
Showing 1 changed file with 68 additions and 3 deletions.
71 changes: 68 additions & 3 deletions solutions/sharafat/11/9.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,74 @@
#include <stdio.h>

#include <stdlib.h>
struct vector
{
int elements*;
int *elements;
int size;
};

// upcoming-soon
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);
}

0 comments on commit d2f6a51

Please sign in to comment.