-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41cf0be
commit d2f6a51
Showing
1 changed file
with
68 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |